Mercurial > hgrepos > Python > libs > ConfigMix
comparison configmix/__init__.py @ 172:8138d56d7cd3
".load" and ".safe_load" get a keyword parameter "defaults" that allows the provision of a configuration dictionary with default settings
| author | Franz Glasner <f.glasner@feldmann-mg.com> |
|---|---|
| date | Fri, 26 Apr 2019 14:30:52 +0200 |
| parents | 1ff11462a5c1 |
| children | b3ba2b0265b5 |
comparison
equal
deleted
inserted
replaced
| 171:1ff11462a5c1 | 172:8138d56d7cd3 |
|---|---|
| 39 comments | 39 comments |
| 40 | 40 |
| 41 """ | 41 """ |
| 42 | 42 |
| 43 | 43 |
| 44 def load(*files): | 44 def load(*files, **kwargs): |
| 45 """Load the given configuration files, merge them in the given order | 45 """Load the given configuration files, merge them in the given order |
| 46 and return the resulting configuration dictionary. | 46 and return the resulting configuration dictionary. |
| 47 | 47 |
| 48 :param files: the filenames of the configuration files to read and merge | 48 :param files: the filenames of the configuration files to read and merge |
| 49 :param defaults: optional configuration dictionary with some default | |
| 50 settings where the settings from `files` are merged | |
| 51 into | |
| 52 :type defaults: a configuration dictionary or `None` | |
| 49 :returns: the configuration | 53 :returns: the configuration |
| 50 :rtype: ~configmix.config.Configuration | 54 :rtype: ~configmix.config.Configuration |
| 51 | 55 |
| 52 """ | 56 """ |
| 57 defaults = kwargs.get("defaults") | |
| 53 if not files: | 58 if not files: |
| 54 return Configuration() | 59 if defaults is None: |
| 55 else: | 60 return Configuration() |
| 56 ex = merge(None, _load_cfg_from_file(files[0])) | 61 else: |
| 57 for f in files[1:]: | 62 return Configuration(defaults) |
| 63 else: | |
| 64 if defaults is None: | |
| 65 start = 1 | |
| 66 ex = merge(None, _load_cfg_from_file(files[0])) | |
| 67 else: | |
| 68 start = 0 | |
| 69 ex = merge(None, defaults) | |
| 70 for f in files[start:]: | |
| 58 ex = merge(_load_cfg_from_file(f), ex) | 71 ex = merge(_load_cfg_from_file(f), ex) |
| 59 return Configuration(ex) | 72 return Configuration(ex) |
| 60 | 73 |
| 61 | 74 |
| 62 def safe_load(*files): | 75 def safe_load(*files, **kwargs): |
| 63 """Analogous to :func:`load` but do merging with :func:`safe_merge` | 76 """Analogous to :func:`load` but do merging with :func:`safe_merge` |
| 64 instead of :func:`merge` | 77 instead of :func:`merge` |
| 65 | 78 |
| 66 """ | 79 """ |
| 80 defaults = kwargs.get("defaults") | |
| 67 if not files: | 81 if not files: |
| 68 return Configuration() | 82 if defaults is None: |
| 69 else: | 83 return Configuration() |
| 84 else: | |
| 85 return Configuration(defaults) | |
| 86 else: | |
| 87 if defaults is None: | |
| 88 start = 1 | |
| 89 ex = merge(None, _load_cfg_from_file(files[0])) | |
| 90 else: | |
| 91 start = 0 | |
| 92 ex = merge(None, defaults) | |
| 70 ex = safe_merge(None, _load_cfg_from_file(files[0])) | 93 ex = safe_merge(None, _load_cfg_from_file(files[0])) |
| 71 for f in files[1:]: | 94 for f in files[start:]: |
| 72 ex = safe_merge(_load_cfg_from_file(f), ex) | 95 ex = safe_merge(_load_cfg_from_file(f), ex) |
| 73 return Configuration(ex) | 96 return Configuration(ex) |
| 74 | 97 |
| 75 | 98 |
| 76 def _load_yaml(filename): | 99 def _load_yaml(filename): |
