From d7423a9582098e0aec4079d2d16400a02d9d33af Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= <sybren@stuvel.eu>
Date: Thu, 22 Dec 2016 10:49:39 +0100
Subject: [PATCH] Worker: Moved loading of config to config module.

---
 .../flamenco_worker/cli.py                    | 43 +++----------------
 .../flamenco_worker/config.py                 | 39 +++++++++++++++++
 2 files changed, 45 insertions(+), 37 deletions(-)

diff --git a/packages/flamenco-worker-python/flamenco_worker/cli.py b/packages/flamenco-worker-python/flamenco_worker/cli.py
index 631716b4..33aa0b33 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 d5b69889..4987ec9f 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
-- 
GitLab