From: Michael Tremer Date: Thu, 1 Dec 2016 15:45:28 +0000 (+0100) Subject: config: Write a new configuration parser X-Git-Tag: 0.9.28~1285^2~1448 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e725182ce7b8ab83d420c49ebbca8fac046c5f27;p=pakfire.git config: Write a new configuration parser 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 --- diff --git a/src/pakfire/__init__.py b/src/pakfire/__init__.py index a15ff9047..b3abd80e2 100644 --- a/src/pakfire/__init__.py +++ b/src/pakfire/__init__.py @@ -20,16 +20,9 @@ ############################################################################### 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 diff --git a/src/pakfire/config.py b/src/pakfire/config.py index c0baf40ae..4ea47440f 100644 --- a/src/pakfire/config.py +++ b/src/pakfire/config.py @@ -21,18 +21,66 @@ 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"] diff --git a/src/pakfire/logger.py b/src/pakfire/logger.py index e9809b52a..3561e97e5 100644 --- a/src/pakfire/logger.py +++ b/src/pakfire/logger.py @@ -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