Fix issue where config dir was not created by default

Fixes #15
kivy_mainline_and_py3.8
K0nstantine 2018-03-05 19:05:36 -05:00
parent 09b16bf3e7
commit 61206f5903
1 changed files with 9 additions and 4 deletions

View File

@ -1,20 +1,19 @@
""" """
Broker configuration mgmt. Broker configuration mgmt.
""" """
from os import path from os import path, makedirs
import configparser import configparser
import click import click
from ..log import get_logger from ..log import get_logger
log = get_logger('broker-config') log = get_logger('broker-config')
_broker_conf_path = path.join(click.get_app_dir('piker'), 'brokers.ini') _config_dir = click.get_app_dir('piker')
_broker_conf_path = path.join(_config_dir, 'brokers.ini')
def load() -> (configparser.ConfigParser, str): def load() -> (configparser.ConfigParser, str):
"""Load broker config. """Load broker config.
Create a ``broker.ini`` file if one dne.
""" """
config = configparser.ConfigParser() config = configparser.ConfigParser()
read = config.read(_broker_conf_path) read = config.read(_broker_conf_path)
@ -24,7 +23,13 @@ def load() -> (configparser.ConfigParser, str):
def write(config: configparser.ConfigParser) -> None: def write(config: configparser.ConfigParser) -> None:
"""Write broker config to disk. """Write broker config to disk.
Create a ``brokers.ini`` file if one does not exist.
""" """
if not path.isdir(_config_dir):
log.debug(f"Creating config dir {_config_dir}")
makedirs(_config_dir)
log.debug(f"Writing config file {_broker_conf_path}") log.debug(f"Writing config file {_broker_conf_path}")
with open(_broker_conf_path, 'w') as cf: with open(_broker_conf_path, 'w') as cf:
return config.write(cf) return config.write(cf)