comparison configmix/__init__.py @ 221:6f0f39a9a46f

configmix.load() and .safe_load() got a new keyword argument "extras" to be merged in as last configuration dictionary
author Franz Glasner <fzglas.hg@dom66.de>
date Thu, 09 May 2019 09:37:51 +0200
parents 2034da70f8fd
children 90dd0d04b926
comparison
equal deleted inserted replaced
220:2034da70f8fd 221:6f0f39a9a46f
51 :param files: the filenames of the configuration files to read and merge 51 :param files: the filenames of the configuration files to read and merge
52 :keyword defaults: optional configuration dictionary with some default 52 :keyword defaults: optional configuration dictionary with some default
53 settings where the settings from `files` are merged 53 settings where the settings from `files` are merged
54 into 54 into
55 :type defaults: dict-alike or None 55 :type defaults: dict-alike or None
56 :keyword extras: optional configuration dictionary that will applied
57 last
58
59 Use this for example to overwrite configuration file
60 settings from commandline arguments.
61 :type extras: dict-alike or None
56 :returns: the configuration 62 :returns: the configuration
57 :rtype: ~configmix.config.Configuration 63 :rtype: ~configmix.config.Configuration
58 64
59 """ 65 """
60 defaults = kwargs.get("defaults") 66 defaults = kwargs.get("defaults")
67 extras = kwargs.get("extras")
61 if defaults is None: 68 if defaults is None:
62 ex = Configuration() 69 ex = Configuration()
63 else: 70 else:
64 ex = merge(None, Configuration(defaults)) 71 ex = merge(None, Configuration(defaults))
65 for f in files: 72 for f in files:
66 ex = merge(_load_cfg_from_file(f), ex) 73 ex = merge(_load_cfg_from_file(f), ex)
74 if extras:
75 ex = merge(Configuration(extras), ex)
67 return Configuration(ex) 76 return Configuration(ex)
68 77
69 78
70 def safe_load(*files, **kwargs): 79 def safe_load(*files, **kwargs):
71 """Analogous to :func:`load` but do merging with :func:`safe_merge` 80 """Analogous to :func:`load` but do merging with :func:`safe_merge`
72 instead of :func:`merge` 81 instead of :func:`merge`
73 82
74 """ 83 """
75 defaults = kwargs.get("defaults") 84 defaults = kwargs.get("defaults")
85 extras = kwargs.get("extras")
76 if defaults is None: 86 if defaults is None:
77 ex = Configuration() 87 ex = Configuration()
78 else: 88 else:
79 ex = safe_merge(None, Configuration(defaults)) 89 ex = safe_merge(None, Configuration(defaults))
80 for f in files: 90 for f in files:
81 ex = safe_merge(_load_cfg_from_file(f), ex) 91 ex = safe_merge(_load_cfg_from_file(f), ex)
92 if extras:
93 ex = safe_merge(Configuration(extras), ex)
82 return Configuration(ex) 94 return Configuration(ex)
83 95
84 96
85 def _load_yaml(filename): 97 def _load_yaml(filename):
86 from . import yaml 98 from . import yaml