From: Frank Brehm Date: Fri, 17 Mar 2017 11:20:31 +0000 (+0100) Subject: Adding performing configuration X-Git-Tag: 0.1.2~254 X-Git-Url: https://git.uhu-banane.de/?a=commitdiff_plain;h=7ee20bedd2379de4391aa1bf760cbe32b9c9cf92;p=pixelpark%2Fadmin-tools.git Adding performing configuration --- diff --git a/pp_lib/cfg_app.py b/pp_lib/cfg_app.py index 0a7225d..23dfac1 100644 --- a/pp_lib/cfg_app.py +++ b/pp_lib/cfg_app.py @@ -36,13 +36,13 @@ from .global_version import __version__ as __global_version__ from .errors import FunctionNotImplementedError, PpAppError -from .common import pp, terminal_can_colors, to_bytes +from .common import pp, terminal_can_colors, to_bytes, to_bool from .merge import merge_structure from .app import PpApplication -__version__ = '0.4.1' +__version__ = '0.4.2' LOG = logging.getLogger(__name__) @@ -122,6 +122,7 @@ class PpConfigApplication(PpApplication): self.init_logging() self._read_config() + self._perform_config() self._init_log_cfgfiles() self.reinit_logging() @@ -410,6 +411,55 @@ class PpConfigApplication(PpApplication): if self.verbose > 1: LOG.debug("Evaluated config total:\n{}".format(self.cfg)) + # ------------------------------------------------------------------------- + def _perform_config(self): + """Execute some actions after reading the configuration.""" + + if 'general' in self.cfg and 'verbose' in self.cfg['general']: + v = self.cfg['general']['verbose'] + if to_bool(v): + try: + v = int(v) + except ValueError: + v = 1 + pass + except TypeError: + v = 1 + pass + if v > self.verbose: + self.verbose = v + root_logger = logging.getLogger() + root_logger.setLevel(logging.DEBUG) + + self.perform_config() + + # ------------------------------------------------------------------------- + def perform_config(self): + + """ + Execute some actions after reading the configuration. + + This method should be explicitely called by all perform_config() + methods in descendant classes. + """ + + pass + + # ------------------------------------------------------------------------- + def post_init(self): + """ + Method to execute before calling run(). Here could be done some + finishing actions after reading in commandline parameters, + configuration a.s.o. + + This method could be overwritten by descendant classes, these + methhods should allways include a call to post_init() of the + parent class. + + """ + + self.initialized = True + # ============================================================================= if __name__ == "__main__": diff --git a/pp_lib/common.py b/pp_lib/common.py index b7ee8c0..370d3e7 100644 --- a/pp_lib/common.py +++ b/pp_lib/common.py @@ -14,15 +14,24 @@ import logging import re import pprint import platform +import locale # Third party modules import six # Own modules -__version__ = '0.2.2' +__version__ = '0.3.1' + +LOG = logging.getLogger(__name__) + +RE_YES = re.compile(r'^\s*(?:y(?:es)?|true)\s*$', re.IGNORECASE) +RE_NO = re.compile(r'^\s*(?:no?|false|off)\s*$', re.IGNORECASE) +PAT_TO_BOOL_TRUE = locale.nl_langinfo(locale.YESEXPR) +RE_TO_BOOL_TRUE = re.compile(PAT_TO_BOOL_TRUE) +PAT_TO_BOOL_FALSE = locale.nl_langinfo(locale.NOEXPR) +RE_TO_BOOL_FALSE = re.compile(PAT_TO_BOOL_FALSE) -log = logging.getLogger(__name__) # ============================================================================= def pp(value, indent=4, width=99, depth=None): @@ -101,6 +110,72 @@ def terminal_can_colors(debug=False): return has_colors +# ============================================================================= +def to_bool(value): + """ + Converter from string to boolean values (e.g. from configurations) + """ + + if not value: + return False + + try: + v_int = int(value) + except ValueError: + pass + except TypeError: + pass + else: + if v_int == 0: + return False + else: + return True + + global PAT_TO_BOOL_TRUE + global RE_TO_BOOL_TRUE + global PAT_TO_BOOL_FALSE + global RE_TO_BOOL_FALSE + + c_yes_expr = locale.nl_langinfo(locale.YESEXPR) + if c_yes_expr != PAT_TO_BOOL_TRUE: + PAT_TO_BOOL_TRUE = c_yes_expr + RE_TO_BOOL_TRUE = re.compile(PAT_TO_BOOL_TRUE) + # LOG.debug("Current pattern for 'yes': %r.", c_yes_expr) + + c_no_expr = locale.nl_langinfo(locale.NOEXPR) + if c_no_expr != PAT_TO_BOOL_FALSE: + PAT_TO_BOOL_FALSE = c_no_expr + RE_TO_BOOL_FALSE = re.compile(PAT_TO_BOOL_FALSE) + # LOG.debug("Current pattern for 'no': %r.", c_no_expr) + + v_str = '' + if isinstance(value, str): + v_str = value + if six.PY2: + if isinstance(value, unicode): + v_str = value.encode('utf-8') + elif six.PY3 and isinstance(value, bytes): + v_str = value.decode('utf-8') + else: + v_str = str(value) + + match = RE_YES.search(v_str) + if match: + return True + match = RE_TO_BOOL_TRUE.search(v_str) + if match: + return True + + match = RE_NO.search(v_str) + if match: + return False + match = RE_TO_BOOL_FALSE.search(v_str) + if match: + return False + + return bool(value) + + # ============================================================================= def to_unicode(obj, encoding='utf-8'):