Exemplo n.º 1
0
def load(path_specs,
         forced_type=None,
         merge=MS_DICTS,
         marker='*',
         ignore_missing=False,
         **kwargs):
    """
    Load single or multiple config files or multiple config files specified in
    given paths pattern.

    :param path_specs:
        Configuration file path or paths or its pattern such as '/a/b/*.json'
    :param forced_type: Forced configuration parser type
    :param merge: Merging strategy to use
    :param marker: Globbing marker to detect paths patterns
    :param kwargs: Backend specific optional arguments, e.g. {"indent": 2} for
        JSON loader/dumper backend
    :return: Dict-like object (instance of
        anyconfig.mergeabledict.MergeableDict by default) supports merge
        operations.
    """
    if marker in path_specs or U.is_iterable(path_specs):
        return multi_load(path_specs, forced_type, merge, marker,
                          ignore_missing, **kwargs)
    else:
        return single_load(path_specs, forced_type, ignore_missing, **kwargs)
def create_from(x):
    """
    Try creating a MergeableDict instance[s] from a dict or any other objects.
    """
    if is_MergeableDict_or_dict(x):
        return MergeableDict((k, create_from(v)) for k, v in x.iteritems())
    elif U.is_iterable(x):
        return type(x)(create_from(v) for v in x)
    else:
        return x
def convert_to(x):
    """
    Convert a MergeableDict instances to a dict object.

    Borrowed basic idea and implementation from bunch.unbunchify.
    (bunch is distributed under MIT license same as this module.)
    """
    if is_MergeableDict_or_dict(x):
        return dict((k, convert_to(v)) for k, v in x.iteritems())
    elif U.is_iterable(x):
        return type(x)(convert_to(v) for v in x)
    else:
        return x
Exemplo n.º 4
0
def load(path_specs, forced_type=None, merge=MS_DICTS, marker='*'):
    """
    Load single or multiple config files or multiple config files specified in
    given paths pattern.

    :param path_specs:
        Configuration file path or paths or its pattern such as '/a/b/*.json'
    :param forced_type: Forced configuration parser type
    :param merge: Merging strategy to use
    :param marker: Globbing marker to detect paths patterns
    """
    if marker in path_specs or U.is_iterable(path_specs):
        return multi_load(path_specs, forced_type, merge, marker)
    else:
        return single_load(path_specs, forced_type)
Exemplo n.º 5
0
def load(path_specs, forced_type=None, merge=MS_DICTS, marker='*',
         ignore_missing=False, **kwargs):
    """
    Load single or multiple config files or multiple config files specified in
    given paths pattern.

    :param path_specs:
        Configuration file path or paths or its pattern such as '/a/b/*.json'
    :param forced_type: Forced configuration parser type
    :param merge: Merging strategy to use
    :param marker: Globbing marker to detect paths patterns
    :param kwargs: Backend specific optional arguments, e.g. {"indent": 2} for
        JSON loader/dumper backend
    :return: Dict-like object (instance of
        anyconfig.mergeabledict.MergeableDict by default) supports merge
        operations.
    """
    if marker in path_specs or U.is_iterable(path_specs):
        return multi_load(path_specs, forced_type, merge, marker,
                          ignore_missing, **kwargs)
    else:
        return single_load(path_specs, forced_type, ignore_missing, **kwargs)
    def update_w_merge(self, other, merge_lists=False):
        """Merge members recursively.

        :param merge_lists: Merge not only dicts but also lists. For example,

            [1, 2, 3], [3, 4] ==> [1, 2, 3, 4]
            [1, 2, 2], [2, 4] ==> [1, 2, 2, 4]
        """
        if is_MergeableDict_or_dict(other):
            for k, v in other.iteritems():
                if k in self and is_MergeableDict_or_dict(v) and \
                        is_MergeableDict_or_dict(self[k]):
                    # update recursively.
                    self[k].update_w_merge(v, merge_lists)
                else:
                    if merge_lists and U.is_iterable(v):
                        v0 = self.get(k, None)
                        if v0 is None:
                            self[k] = [x for x in list(v)]
                        else:
                            self[k] += [x for x in list(v) if x not in v0]
                    else:
                        self[k] = v