Use toml instead of ini for broker config

kivy_mainline_and_py3.8
Tyler Goodlet 2019-03-15 19:37:04 -04:00
parent 2d688668ff
commit 6a50049af7
2 changed files with 15 additions and 12 deletions

View File

@ -3,13 +3,14 @@ Broker configuration mgmt.
""" """
import os import os
import configparser import configparser
import toml
import click import click
from ..log import get_logger from ..log import get_logger
log = get_logger('broker-config') log = get_logger('broker-config')
_config_dir = click.get_app_dir('piker') _config_dir = click.get_app_dir('piker')
_file_name = 'brokers.ini' _file_name = 'brokers.toml'
def _override_config_dir( def _override_config_dir(
@ -25,18 +26,17 @@ def get_broker_conf_path():
def load( def load(
path: str = None path: str = None
) -> (configparser.ConfigParser, str): ) -> (dict, str):
"""Load broker config. """Load broker config.
""" """
path = path or get_broker_conf_path() path = path or get_broker_conf_path()
config = configparser.ConfigParser() config = toml.load(path)
config.read(path)
log.debug(f"Read config file {path}") log.debug(f"Read config file {path}")
return config, path return config, path
def write( def write(
config: configparser.ConfigParser, config: dict, # toml config as dict
path: str = None, path: str = None,
) -> None: ) -> None:
"""Write broker config to disk. """Write broker config to disk.
@ -49,6 +49,10 @@ def write(
log.debug(f"Creating config dir {_config_dir}") log.debug(f"Creating config dir {_config_dir}")
os.makedirs(dirname) 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}") log.debug(f"Writing config file {path}")
with open(path, 'w') as cf: with open(path, 'w') as cf:
return config.write(cf) return toml.dump(config, cf)

View File

@ -56,7 +56,7 @@ def refresh_token_on_err(tries=3):
client = api.client client = api.client
if not client._has_access.is_set(): if not client._has_access.is_set():
log.warning("WAITING ON ACCESS LOCK") log.warning("Waiting on access lock")
await client._has_access.wait() await client._has_access.wait()
for i in range(1, tries): for i in range(1, tries):
@ -210,7 +210,7 @@ class Client:
""" """
def __init__( def __init__(
self, self,
config: configparser.ConfigParser, config: dict,
): ):
self._sess = asks.Session() self._sess = asks.Session()
self.api = _API(self) self.api = _API(self)
@ -539,7 +539,9 @@ def get_config(
""" """
log.debug("Reloading access config data") log.debug("Reloading access config data")
conf, path = config.load(config_path) 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") log.warn(f"Forcing manual token auth from user")
_token_from_user(conf) _token_from_user(conf)
@ -554,9 +556,6 @@ async def get_client(
"""Spawn a broker client for making requests to the API service. """Spawn a broker client for making requests to the API service.
""" """
conf, path = get_config(config_path) 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']))}") log.debug(f"Loaded config:\n{colorize_json(dict(conf['questrade']))}")
client = Client(conf) client = Client(conf)
await client.ensure_access(ask_user=ask_user) await client.ensure_access(ask_user=ask_user)