diff --git a/packages/flamenco-worker-python/flamenco_worker/cli.py b/packages/flamenco-worker-python/flamenco_worker/cli.py index 631716b4ae0e2b158a151797a84c21b1bbc43f21..33aa0b33e9f7991b1dad7a665376f97715f196b7 100644 --- a/packages/flamenco-worker-python/flamenco_worker/cli.py +++ b/packages/flamenco-worker-python/flamenco_worker/cli.py @@ -1,20 +1,8 @@ """Commandline interface entry points.""" import argparse -import collections -import configparser import logging import logging.config -import os - -DEFAULT_CONFIG = { - 'flamenco-worker': collections.OrderedDict([ - ('manager_url', 'http://flamenco-manager/'), - ('job_types', 'sleep blender_render_simple'), - ('worker_id', ''), - ('worker_secret', ''), - ]) -} def main(): @@ -61,40 +49,21 @@ def main(): log.debug('Starting') # Load configuration - confparser = configparser.ConfigParser() - confparser.read_dict(DEFAULT_CONFIG) - - if args.config: - log.info('Loading configuration from %s', args.config) - confparser.read(args.config, encoding='utf8') - else: - from . import config as config_module - config_files = [config_module.GLOBAL_CONFIG_FILE, - config_module.HOME_CONFIG_FILE] - log.info('Loading configuration from %s', ', '.join(config_files)) - confparser.read(config_files, encoding='utf8') + from . import config - from .config import CONFIG_SECTION - if args.verbose: - import sys - log.info('Effective configuration:') - to_show = configparser.ConfigParser() - to_show.read_dict(confparser) - if to_show.get(CONFIG_SECTION, 'worker_secret'): - to_show.set(CONFIG_SECTION, 'worker_secret', '-hidden-') - to_show.write(sys.stderr) + confparser = config.load_config(args.config, args.verbose) from . import worker, upstream fmanager = upstream.FlamencoManager( - manager_url=confparser.get(CONFIG_SECTION, 'manager_url'), + manager_url=confparser.get(config.CONFIG_SECTION, 'manager_url'), ) fworker = worker.FlamencoWorker( manager=fmanager, - job_types=confparser.get(CONFIG_SECTION, 'job_types').split(), - worker_id=confparser.get(CONFIG_SECTION, 'worker_id'), - worker_secret=confparser.get(CONFIG_SECTION, 'worker_secret'), + job_types=confparser.get(config.CONFIG_SECTION, 'job_types').split(), + worker_id=confparser.get(config.CONFIG_SECTION, 'worker_id'), + worker_secret=confparser.get(config.CONFIG_SECTION, 'worker_secret'), ) try: fworker.startup() diff --git a/packages/flamenco-worker-python/flamenco_worker/config.py b/packages/flamenco-worker-python/flamenco_worker/config.py index d5b698892024ce0939117925e1bf778a55d84344..4987ec9fbf55d828f029e39c408042a02884b3a1 100644 --- a/packages/flamenco-worker-python/flamenco_worker/config.py +++ b/packages/flamenco-worker-python/flamenco_worker/config.py @@ -1,5 +1,6 @@ """Writes configuration to a config file in the home directory.""" +import collections import configparser import os.path import logging @@ -8,6 +9,15 @@ HOME_CONFIG_FILE = os.path.expanduser('~/.flamenco-worker.cfg') GLOBAL_CONFIG_FILE = 'flamenco-worker.cfg' CONFIG_SECTION = 'flamenco-worker' +DEFAULT_CONFIG = { + 'flamenco-worker': collections.OrderedDict([ + ('manager_url', 'http://flamenco-manager/'), + ('job_types', 'sleep blender_render_simple'), + ('worker_id', ''), + ('worker_secret', ''), + ]) +} + log = logging.getLogger(__name__) @@ -30,3 +40,32 @@ def merge_with_home_config(new_conf: dict): os.replace(tmpname, HOME_CONFIG_FILE) log.info('Updated configuration file %s', HOME_CONFIG_FILE) + + +def load_config(config_file: str = None, + show_effective_config: bool = False) -> configparser.ConfigParser: + """Loads one or more configuration files.""" + + confparser = configparser.ConfigParser() + confparser.read_dict(DEFAULT_CONFIG) + + if config_file: + log.info('Loading configuration from %s', config_file) + loaded = confparser.read(config_file, encoding='utf8') + else: + config_files = [GLOBAL_CONFIG_FILE, HOME_CONFIG_FILE] + log.info('Loading configuration from %s', ', '.join(config_files)) + loaded = confparser.read(config_files, encoding='utf8') + + log.info('Succesfully loaded: %s', loaded) + + if show_effective_config: + import sys + log.info('Effective configuration:') + to_show = configparser.ConfigParser() + to_show.read_dict(confparser) + if to_show.get(CONFIG_SECTION, 'worker_secret'): + to_show.set(CONFIG_SECTION, 'worker_secret', '-hidden-') + to_show.write(sys.stderr) + + return confparser