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__)
self.init_logging()
self._read_config()
+ self._perform_config()
self._init_log_cfgfiles()
self.reinit_logging()
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__":
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):
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'):