def to_container(obj=None, ac_ordered=False, ac_merge=m9dicts.MS_DICTS, ac_namedtuple=False, ac_ntpl_cls_key=NTPL_CLS_KEY, **options): r""" Factory function to create a dict-like object[s] supports merge operation from a dict or any other objects. .. seealso:: :func:`m9dicts.make` :param obj: A dict or other object[s] or None :param ordered: Create an instance of OrderedMergeableDict instead of MergeableDict If it's True. Please note that OrderedMergeableDict class will be chosen for namedtuple objects regardless of this argument always to keep keys (fields) order. :param merge: Specify strategy from MERGE_STRATEGIES of how to merge results loaded from multiple configuration files. :param _ntpl_cls_key: Special keyword to embedded the class name of namedtuple object to the dict-like object created. It's a hack and not elegant but I don't think there are another ways to make same namedtuple object from objects created from it. :param options: Optional keyword arguments for m9dicts.convert_to, will be converted to the above ac_\* options respectively as needed. """ opts = dict(ordered=ac_ordered, merge=ac_merge, _ntpl_cls_key=ac_ntpl_cls_key) return m9dicts.make(obj, **opts)
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': 0}, {'c': 2}, {'c': 3}], 'd': {'e': "bbb", 'f': 3}} :param a: the target dictionary :param b: the dictionary to import :return: dict """ md = m9dicts.make(a, merge=m9dicts.MS_DICTS_AND_LISTS) md.update(b) return md
def __init__(self): self.opts = self.parse_opts() config_files = [ '/etc/papercut/papercut.yaml', os.path.expanduser('~/.papercut/papercut.yaml') ] if self.opts.config: config_files = [] for c in self.opts.config: if os.path.exists(c): config_files.append(c) else: print( "WARNING: configuration file %s: no such file or directory" % c, file=sys.stderr) configs = [CONFIG_DEFAULT] for f in config_files: c = self.read_config(f) configs.append(m9dicts.make(c)) cfg_merged = self.merge_configs(configs) cfg_merged = self.path_keys(cfg_merged) self.config = ConfigurationWrapper(cfg_merged) self.check_config()
def merge_configs(self, configs): '''Merges a list of configuration dicts into one final configuration dict''' cfg = m9dicts.make() for config in configs: cfg.update(config, merge=m9dicts.MS_DICTS_AND_LISTS) return cfg
def run(ctx, config, verbosity, extra_vars, args=None): ctx.obj = {} ctx.obj['original_cwd'] = os.getcwd() os.chdir('/') configuration = {} configuration['extra_vars'] = m9dicts.make() for i in extra_vars: configuration['extra_vars'].update(json.loads(i)) ctx.obj['configuration'] = configuration
def __init__(self): self.opts = self.parse_opts() config_files = [ '/etc/papercut/papercut.yaml', os.path.expanduser('~/.papercut/papercut.yaml') ] if self.opts.config: config_files = [] for c in self.opts.config: if os.path.exists(c): config_files.append(c) else: print("WARNING: configuration file %s: no such file or directory" % c, file=sys.stderr) configs = [CONFIG_DEFAULT] for f in config_files: c = self.read_config(f) configs.append(m9dicts.make(c)) cfg_merged = self.merge_configs(configs) cfg_merged = self.path_keys(cfg_merged) self.config = ConfigurationWrapper(cfg_merged) self.check_config()