From 6a50049af754e5c82a082acf871f4247269da9c3 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Fri, 15 Mar 2019 19:37:04 -0400 Subject: [PATCH] Use toml instead of ini for broker config --- piker/brokers/config.py | 16 ++++++++++------ piker/brokers/questrade.py | 11 +++++------ 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/piker/brokers/config.py b/piker/brokers/config.py index f1c8a90d..bbf4d407 100644 --- a/piker/brokers/config.py +++ b/piker/brokers/config.py @@ -3,13 +3,14 @@ Broker configuration mgmt. """ import os import configparser +import toml import click from ..log import get_logger log = get_logger('broker-config') _config_dir = click.get_app_dir('piker') -_file_name = 'brokers.ini' +_file_name = 'brokers.toml' def _override_config_dir( @@ -25,18 +26,17 @@ def get_broker_conf_path(): def load( path: str = None -) -> (configparser.ConfigParser, str): +) -> (dict, str): """Load broker config. """ path = path or get_broker_conf_path() - config = configparser.ConfigParser() - config.read(path) + config = toml.load(path) log.debug(f"Read config file {path}") return config, path def write( - config: configparser.ConfigParser, + config: dict, # toml config as dict path: str = None, ) -> None: """Write broker config to disk. @@ -49,6 +49,10 @@ def write( log.debug(f"Creating config dir {_config_dir}") os.makedirs(dirname) + if not config: + raise ValueError( + "Watch out you're trying to write a blank config!") + log.debug(f"Writing config file {path}") with open(path, 'w') as cf: - return config.write(cf) + return toml.dump(config, cf) diff --git a/piker/brokers/questrade.py b/piker/brokers/questrade.py index 01643a57..0856537b 100644 --- a/piker/brokers/questrade.py +++ b/piker/brokers/questrade.py @@ -56,7 +56,7 @@ def refresh_token_on_err(tries=3): client = api.client if not client._has_access.is_set(): - log.warning("WAITING ON ACCESS LOCK") + log.warning("Waiting on access lock") await client._has_access.wait() for i in range(1, tries): @@ -210,7 +210,7 @@ class Client: """ def __init__( self, - config: configparser.ConfigParser, + config: dict, ): self._sess = asks.Session() self.api = _API(self) @@ -539,7 +539,9 @@ def get_config( """ log.debug("Reloading access config data") conf, path = config.load(config_path) - if force_from_user: + if force_from_user or ( + not conf.get('questrade') or not conf['questrade'].get('refresh_token') + ): log.warn(f"Forcing manual token auth from user") _token_from_user(conf) @@ -554,9 +556,6 @@ async def get_client( """Spawn a broker client for making requests to the API service. """ conf, path = get_config(config_path) - if not conf.has_section('questrade'): - raise ValueError( - f"No `questrade` section could be found in {path}") log.debug(f"Loaded config:\n{colorize_json(dict(conf['questrade']))}") client = Client(conf) await client.ensure_access(ask_user=ask_user)