]> git.ipfire.org Git - pakfire.git/commitdiff
config: Write a new configuration parser
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 1 Dec 2016 15:45:28 +0000 (16:45 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 1 Dec 2016 15:45:28 +0000 (16:45 +0100)
This is always initialised when the new module is loaded
and things like logging use it to set themselves up.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/__init__.py
src/pakfire/config.py
src/pakfire/logger.py

index a15ff90479372f5bcaac5994acd05b971e8270f4..b3abd80e2cc63d483f00bb2bbfe169621676718a 100644 (file)
 ###############################################################################
 
 from .constants import PAKFIRE_VERSION as __version__
+from .logger import setup_logging
 
-# Initialize logging as soon as the module is loaded
-from . import logger
-log = logger.setup_logging()
-
-from . import base
-
-Pakfire        = base.Pakfire
-PakfireBuilder = base.PakfireBuilder
-PakfireServer  = base.PakfireServer
-
-# Log when loaded
+log = setup_logging()
 log.debug("Pakfire %s initialised" % __version__)
+
+from .base import Pakfire, PakfireBuilder, PakfireServer
index c0baf40aecd333312aa2557a1364822ade5914b2..4ea47440faf8e8566db57e6c817d88894bddd43d 100644 (file)
 
 import configparser
 import io
+import logging
 import os
 import socket
 
-import logging
-log = logging.getLogger("pakfire")
+log = logging.getLogger("pakfire.config")
+log.propagate = 1
 
-from . import logger
 from .system import system
 
 from .constants import *
 from .i18n import _
 
+class Config(object):
+       def __init__(self, *files):
+               self._config = configparser.ConfigParser(
+                       interpolation=configparser.ExtendedInterpolation()
+               )
+
+               # Read any passed configuration files
+               for f in files:
+                       self.read(f)
+
+       def read(self, path):
+               """
+                       Reads configuration from the given file
+               """
+               if not path.startswith("/"):
+                       path = os.path.join(CONFIG_DIR, path)
+
+               log.debug("Reading configuration from %s" % path)
+
+               with open(path) as f:
+                       self._config.readfp(f)
+
+       def get(self, section, option, default=None):
+               return self._config.get(section, option, fallback=default)
+
+       def get_bool(self, section, option, default=None):
+               return self._config.getboolean(section, option, fallback=default)
+
+       def dump(self):
+               """
+                       Dump the configuration that was read
+
+                       (Only in debugging mode)
+               """
+               log.debug(_("Configuration:"))
+
+               for section in self._config.sections():
+                       log.debug("  " + _("Section: %s") % section)
+
+                       for option in self._config[section]:
+                               value = self.get(section, option)
+
+                               log.debug("    %-20s: %s" % (option, value))
+
+
+# Read initial configuration
+config = Config("general.conf")
+
 class _Config(object):
        files = []
 
@@ -138,10 +186,6 @@ class _Config(object):
                        except KeyError:
                                self._config[section] = items
 
-               # Update the logger, because the logging configuration may
-               # have been altered.
-               logger.setup_logging(self)
-
        def set(self, section, key, value):
                try:
                        self._config[section][key] = value
@@ -215,10 +259,6 @@ class _Config(object):
                return self.get_section("distro")
 
 
-class Config(_Config):
-       files = ["general.conf", "distro.conf"]
-
-
 class ConfigBuilder(_Config):
        files = ["general.conf", "builder.conf"]
 
index e9809b52affe40d349813a82f1a14e877de73568..3561e97e5386c3beb229c1ad56da12f92ae8f22d 100644 (file)
@@ -24,10 +24,16 @@ import time
 import logging
 import logging.handlers
 
-def setup_logging(debug=False):
+from .config import config
+
+def setup_logging(debug=None):
        """
                This function initialized the logger that is enabled immediately
        """
+       # If debug is None, we read it from the configuration
+       if debug is None:
+               debug = config.get_bool("log", "debug", False)
+
        l = logging.getLogger("pakfire")
        l.propagate = 0