Exemplo n.º 1
0
def get_default_config_filename():
    """Returns the configuration filepath.

    If PEYOTL_CONFIG_FILE is in the env that is the preferred choice; otherwise ~/.peyotl/config is preferred.
    If the preferred file does not exist, then the packaged peyotl/default.conf from the installation of peyotl is
    used.
    A RuntimeError is raised if that fails.
    """
    global _CONFIG_FN
    if _CONFIG_FN is not None:
        return _CONFIG_FN
    with _CONFIG_FN_LOCK:
        if _CONFIG_FN is not None:
            return _CONFIG_FN
        if 'PEYOTL_CONFIG_FILE' in os.environ:
            cfn = os.path.abspath(os.environ['PEYOTL_CONFIG_FILE'])
        else:
            cfn = os.path.expanduser("~/.peyotl/config")
        if not os.path.isfile(cfn):
            # noinspection PyProtectedMember
            if 'PEYOTL_CONFIG_FILE' in os.environ:
                from peyotl.utility.get_logger import warn_from_util_logger
                msg = 'Filepath "{}" specified via PEYOTL_CONFIG_FILE={} was not found'.format(
                    cfn, os.environ['PEYOTL_CONFIG_FILE'])
                warn_from_util_logger(msg)
            from pkg_resources import Requirement, resource_filename
            pr = Requirement.parse('peyotl')
            cfn = resource_filename(pr, 'peyotl/default.conf')
        if not os.path.isfile(cfn):
            raise RuntimeError(
                'The peyotl configuration file cascade failed looking for "{}"'
                .format(cfn))
        _CONFIG_FN = os.path.abspath(cfn)
    return _CONFIG_FN
Exemplo n.º 2
0
 def __init__(self,
              raw_config_obj=None,
              config_filename='<programmatically configured>',
              overrides=None):
     """If `raw_config_obj` is None, the default peyotl cascade for finding a configuration
     file will be used. overrides is a 2-level dictionary of section/param entries that will
     be used instead of the setting.
     The two default dicts will be used if the setting is not in overrides or the config object.
     "dominant" and "fallback" refer to whether the rank higher or lower than the default
     value in a get.*setting.*() call
     """
     # noinspection PyProtectedMember
     from peyotl.utility.get_logger import _logging_env_conf_overrides
     self._config_filename = config_filename
     self._raw = raw_config_obj
     if overrides is None:
         overrides = {}
     leco = _logging_env_conf_overrides()
     if leco:
         for k, v in leco['logging'].items():
             if k in overrides.setdefault('logging', {}):
                 # noinspection PyProtectedMember
                 from peyotl.utility.get_logger import warn_from_util_logger
                 m = 'Override keys ["logging"]["{}"] was overruled by an environmental variable'.format(
                     k)
                 warn_from_util_logger(m)
             overrides['logging'][k] = v
     self._override = overrides
Exemplo n.º 3
0
def get_default_config_filename():
    """Returns the configuration filepath.

    If PEYOTL_CONFIG_FILE is in the env that is the preferred choice; otherwise ~/.peyotl/config is preferred.
    If the preferred file does not exist, then the packaged peyotl/default.conf from the installation of peyotl is
    used.
    A RuntimeError is raised if that fails.
    """
    global _CONFIG_FN
    if _CONFIG_FN is not None:
        return _CONFIG_FN
    with _CONFIG_FN_LOCK:
        if _CONFIG_FN is not None:
            return _CONFIG_FN
        if 'PEYOTL_CONFIG_FILE' in os.environ:
            cfn = os.path.abspath(os.environ['PEYOTL_CONFIG_FILE'])
        else:
            cfn = os.path.expanduser("~/.peyotl/config")
        if not os.path.isfile(cfn):
            # noinspection PyProtectedMember
            if 'PEYOTL_CONFIG_FILE' in os.environ:
                from peyotl.utility.get_logger import warn_from_util_logger
                msg = 'Filepath "{}" specified via PEYOTL_CONFIG_FILE={} was not found'.format(cfn, os.environ[
                    'PEYOTL_CONFIG_FILE'])
                warn_from_util_logger(msg)
            from pkg_resources import Requirement, resource_filename
            pr = Requirement.parse('peyotl')
            cfn = resource_filename(pr, 'peyotl/default.conf')
        if not os.path.isfile(cfn):
            raise RuntimeError('The peyotl configuration file cascade failed looking for "{}"'.format(cfn))
        _CONFIG_FN = os.path.abspath(cfn)
    return _CONFIG_FN
Exemplo n.º 4
0
 def __init__(self,
              raw_config_obj=None,
              config_filename='<programmatically configured>',
              overrides=None):
     """If `raw_config_obj` is None, the default peyotl cascade for finding a configuration
     file will be used. overrides is a 2-level dictionary of section/param entries that will
     be used instead of the setting.
     The two default dicts will be used if the setting is not in overrides or the config object.
     "dominant" and "fallback" refer to whether the rank higher or lower than the default
     value in a get.*setting.*() call
     """
     # noinspection PyProtectedMember
     from peyotl.utility.get_logger import _logging_env_conf_overrides
     self._config_filename = config_filename
     self._raw = raw_config_obj
     if overrides is None:
         overrides = {}
     leco = _logging_env_conf_overrides()
     if leco:
         for k, v in leco['logging'].items():
             if k in overrides.setdefault('logging', {}):
                 # noinspection PyProtectedMember
                 from peyotl.utility.get_logger import warn_from_util_logger
                 m = 'Override keys ["logging"]["{}"] was overruled by an environmental variable'.format(k)
                 warn_from_util_logger(m)
             overrides['logging'][k] = v
     self._override = overrides
Exemplo n.º 5
0
def _warn_missing_setting(section, param, config_filename, warn_on_none_level=logging.WARN):
    if warn_on_none_level is None:
        return
    # noinspection PyProtectedMember
    from peyotl.utility.get_logger import warn_from_util_logger
    from peyotl.utility.str_util import is_str_type
    if config_filename:
        if not is_str_type(config_filename):
            f = ' "{}" '.format('", "'.join(config_filename))
        else:
            f = ' "{}" '.format(config_filename)
    else:
        f = ' '
    mf = 'Config file {f} does not contain option "{o}"" in section "{s}"'
    msg = mf.format(f=f, o=param, s=section)
    warn_from_util_logger(msg)
Exemplo n.º 6
0
def _warn_missing_setting(section,
                          param,
                          config_filename,
                          warn_on_none_level=logging.WARN):
    if warn_on_none_level is None:
        return
    # noinspection PyProtectedMember
    from peyotl.utility.get_logger import warn_from_util_logger
    from peyotl.utility.str_util import is_str_type
    if config_filename:
        if not is_str_type(config_filename):
            f = ' "{}" '.format('", "'.join(config_filename))
        else:
            f = ' "{}" '.format(config_filename)
    else:
        f = ' '
    mf = 'Config file {f} does not contain option "{o}"" in section "{s}"'
    msg = mf.format(f=f, o=param, s=section)
    warn_from_util_logger(msg)