[tor-commits] [sbws/master] Use config default paths

pastly at torproject.org pastly at torproject.org
Thu Aug 9 14:21:20 UTC 2018


commit 818f72ed6575ed45b4549dd96a24100630f1b4a2
Author: juga0 <juga at riseup.net>
Date:   Wed Jul 18 20:06:58 2018 +0000

    Use config default paths
    
    and create minimal user config when it does not exist.
---
 sbws/util/config.py | 67 ++++++++++++++++++++++++++++++++---------------------
 1 file changed, 41 insertions(+), 26 deletions(-)

diff --git a/sbws/util/config.py b/sbws/util/config.py
index 2898d1b..3171ad2 100644
--- a/sbws/util/config.py
+++ b/sbws/util/config.py
@@ -1,3 +1,6 @@
+"""Util functions to manage sbws configuration files."""
+import shutil
+
 from configparser import (ConfigParser, ExtendedInterpolation)
 from configparser import InterpolationMissingOptionError
 import os
@@ -6,7 +9,8 @@ import logging.config
 from urllib.parse import urlparse
 from string import Template
 from tempfile import NamedTemporaryFile
-from sbws.globals import PKG_DIR
+from sbws.globals import (DEFAULT_CONFIG_PATH, DEFAULT_LOG_CONFIG_PATH,
+                          MINIMUM_USER_CONFIG_PATH, USER_CONFIG_PATH)
 
 _ALPHANUM = 'abcdefghijklmnopqrstuvwxyz'
 _ALPHANUM += _ALPHANUM.upper()
@@ -21,6 +25,12 @@ _LOG_LEVELS = ['debug', 'info', 'warning', 'error', 'critical']
 log = logging.getLogger(__name__)
 
 
+def _create_user_config_file(fname):
+    """Copy minimal user config to user config path."""
+    if not os.path.isfile(fname):
+        shutil.copyfile(MINIMUM_USER_CONFIG_PATH, fname)
+
+
 def _read_config_file(conf, fname):
     assert os.path.isfile(fname)
     log.debug('Reading config file %s', fname)
@@ -29,52 +39,57 @@ def _read_config_file(conf, fname):
     return conf
 
 
+def _extend_config(conf, fpath):
+    """Extend ConfigParser from file configuration."""
+    conf = _read_config_file(conf, fpath)
+    return conf
+
+
 def _get_default_config():
+    """Return ConfigParser with default configuration."""
     conf = ConfigParser(interpolation=ExtendedInterpolation())
-    fname = os.path.join(PKG_DIR, 'config.default.ini')
-    assert os.path.isfile(fname)
-    conf = _read_config_file(conf, fname)
-    return conf
+    return _read_config_file(conf, DEFAULT_CONFIG_PATH)
 
 
 def _get_user_config(args, conf=None):
+    """Get user configuration.
+    If a custom path is passed, search for user configuration there.
+    In any case, create a minimal user configuration in user configuration path
+    , in case it needs to be overwritten.
+    """
     if not conf:
         conf = ConfigParser(interpolation=ExtendedInterpolation())
     else:
         assert isinstance(conf, ConfigParser)
-    fname = os.path.join(args.directory, 'config.ini')
-    if not os.path.isfile(fname):
-        return conf
-    conf = _read_config_file(conf, fname)
-    return conf
-
-
-def _get_default_logging_config(args, conf=None):
+    if args.config:
+        if not os.path.isfile(args.config):
+            log.warning('Configuration file %s not found, '
+                        'using default configuration, and creating a minimal '
+                        'configuration in %s.', args.config, args.config)
+            _create_user_config_file(args.config)
+            return conf
+        return _extend_config(conf, args.config)
+    _create_user_config_file(USER_CONFIG_PATH)
+    return _extend_config(conf, USER_CONFIG_PATH)
+
+
+def _get_default_logging_config(conf=None):
+    """Get default logging configuration."""
     if not conf:
         conf = ConfigParser(interpolation=ExtendedInterpolation())
     else:
         assert isinstance(conf, ConfigParser)
-    fname = os.path.join(PKG_DIR, 'config.log.default.ini')
-    assert os.path.isfile(fname)
-    conf = _read_config_file(conf, fname)
-    return conf
+    return _extend_config(conf, DEFAULT_LOG_CONFIG_PATH)
 
 
 def get_config(args):
+    """Get ConfigParser interpolating all configuration files."""
     conf = _get_default_config()
-    conf = _get_default_logging_config(args, conf=conf)
+    conf = _get_default_logging_config(conf=conf)
     conf = _get_user_config(args, conf=conf)
     return conf
 
 
-def get_user_example_config():
-    conf = ConfigParser(interpolation=ExtendedInterpolation())
-    fname = os.path.join(PKG_DIR, 'config.example.ini')
-    assert os.path.isfile(fname)
-    conf = _read_config_file(conf, fname)
-    return conf
-
-
 def _can_log_to_file(conf):
     '''
     Checks all the known reasons for why we might not be able to log to a file,





More information about the tor-commits mailing list