def test_checking_config_equality():
    config = Config({'a': 1, 'b': 2})
    other_config = Config({'a': 1, 'b': 2})
    plain_dict = {'a': 1, 'b': 2}
    m9dict_value = anyconfig.to_container({'a': 1, 'b': 2})

    assert config == other_config
    assert config == plain_dict
    assert config == m9dict_value
Пример #2
0
    def __init__(self, config=None, parent=None, schema=None):
        if config is None:
            config = get_empty_config()
        elif isinstance(config, dict) and hasattr(anyconfig, 'to_container'):
            config = anyconfig.to_container(config)

        self._wrapped = config
        self.parent = parent
        self.schema = schema

        if self.schema is not None:
            self.validate()
Пример #3
0
    def __init__(self, config=None, parent=None, schema=None):
        if config is None:
            config = get_empty_config()
        elif isinstance(config, dict) and hasattr(anyconfig, 'to_container'):
            config = anyconfig.to_container(config)

        self._wrapped = config
        self.parent = parent
        self.schema = schema

        if self.schema is not None:
            self.validate()
Пример #4
0
def try_to_load_config_from_files(conf_path=None):
    """
    Load configurations from given `conf_path`.
    """
    cnf = anyconfig.to_container()
    cnf.update(**DEFAULTS)

    if conf_path:
        try:
            diff = anyconfig.load(conf_path)
            cnf.update(diff)
        except (IOError, OSError):
            pass

    return cnf
Пример #5
0
    def _combine(self, configs):
        """ Perform a prioritized recursive merge of serveral source files
        and returns a new dict.

        The merge order is based on the index of the list, meaning that
        elements at the end of the list will be merged last, and have greater
        precedence than elements at the beginning.  The result is then merged
        ontop of the defaults.

        :param configs: A list containing the yaml files to load.
        :return: dict
        """

        default = self._get_defaults()
        conf = anyconfig.to_container(default, ac_merge=MERGE_STRATEGY)
        conf.update(
            anyconfig.load(
                configs, ignore_missing=True, ac_merge=MERGE_STRATEGY))

        return m9dicts.convert_to(conf)
Пример #6
0
    def _combine(self, configs):
        """ Perform a prioritized recursive merge of serveral source files
        and returns a new dict.

        The merge order is based on the index of the list, meaning that
        elements at the end of the list will be merged last, and have greater
        precedence than elements at the beginning.  The result is then merged
        ontop of the defaults.

        :param configs: A list containing the yaml files to load.
        :return: dict
        """

        default = self._get_defaults()
        conf = anyconfig.to_container(default, ac_merge=MERGE_STRATEGY)
        conf.update(
            anyconfig.load(
                configs, ignore_missing=True, ac_merge=MERGE_STRATEGY))

        return m9dicts.convert_to(conf)
Пример #7
0
def load_site_ctxs(ctxs):
    """
    Load context (conf) files from ``ctxs``.

    :param ctxs: List of context file[s], glob patterns of context files
        or dirs :: [str]
    """
    conf = anyconfig.to_container()
    for ctxpath in ctxs:
        diff = load_site_ctx(ctxpath)

        if diff:
            conf.update(diff)
        else:
            logging.warn("No config loaded from: %s", ctxpath)

    if not conf:
        raise EmptyConfigError("No config available from: " + ','.join(ctxs))

    return conf
Пример #8
0
def load_site_ctxs(ctxs):
    """
    Load context (conf) files from ``ctxs``.

    :param ctxs: List of context file[s], glob patterns of context files
        or dirs :: [str]
    """
    conf = anyconfig.to_container()
    for ctxpath in ctxs:
        diff = load_site_ctx(ctxpath)

        if diff:
            conf.update(diff)
        else:
            logging.warn("No config loaded from: %s", ctxpath)

    if not conf:
        raise EmptyConfigError("No config available from: " + ','.join(ctxs))

    return conf
Пример #9
0
def merge_dicts(a, b):
    """
    Merges the values of B into A and returns a new dict.  Uses the same merge
    strategy as ``config._combine``.

    ::

        dict a

        b:
           - c: 0
           - c: 2
        d:
           e: "aaa"
           f: 3

        dict b

        a: 1
        b:
           - c: 3
        d:
           e: "bbb"

    Will give an object such as::

        {'a': 1, 'b': [{'c': 3}], 'd': {'e': "bbb", 'f': 3}}


    :param a: the target dictionary
    :param b: the dictionary to import
    :return: dict
    """
    conf = anyconfig.to_container(a, ac_merge=MERGE_STRATEGY)
    conf.update(b)

    return conf
Пример #10
0
def merge_dicts(a, b):
    """
    Merges the values of B into A and returns a new dict.  Uses the same merge
    strategy as ``config._combine``.

    ::

        dict a

        b:
           - c: 0
           - c: 2
        d:
           e: "aaa"
           f: 3

        dict b

        a: 1
        b:
           - c: 3
        d:
           e: "bbb"

    Will give an object such as::

        {'a': 1, 'b': [{'c': 3}], 'd': {'e': "bbb", 'f': 3}}


    :param a: the target dictionary
    :param b: the dictionary to import
    :return: dict
    """
    conf = anyconfig.to_container(a, ac_merge=MERGE_STRATEGY)
    conf.update(b)

    return conf
Пример #11
0
def get_empty_config():
    empty_config = anyconfig.to_container({})
    return empty_config
Пример #12
0
def get_empty_config():
    if hasattr(anyconfig, 'to_container'):
        empty_config = anyconfig.to_container({})
    else:
        empty_config = {}
    return empty_config
Пример #13
0
def get_empty_config():
    if hasattr(anyconfig, 'to_container'):
        empty_config = anyconfig.to_container({})
    else:
        empty_config = {}
    return empty_config