Mercurial > hgrepos > Python > libs > ConfigMix
comparison configmix/__init__.py @ 110:29cf359ddf4d
Remove the "_first" parameter from "merge" and "safe_merge" by splitting into two functions
| author | Franz Glasner <hg@dom66.de> |
|---|---|
| date | Sat, 24 Mar 2018 16:06:08 +0100 |
| parents | 2196362c0467 |
| children | d51a18e5b0e3 |
comparison
equal
deleted
inserted
replaced
| 109:057d87d030f1 | 110:29cf359ddf4d |
|---|---|
| 80 else: | 80 else: |
| 81 result[k] = deepcopy(v) | 81 result[k] = deepcopy(v) |
| 82 return result | 82 return result |
| 83 | 83 |
| 84 | 84 |
| 85 def merge(user, default, _first=True): | 85 def merge(user, default): |
| 86 """A simple (YAML-)tree-merge. | 86 """Logically merge the configuration in `user` into `default`. |
| 87 | 87 |
| 88 :param ~configmix.config.Configuration user: | 88 :param ~configmix.config.Configuration user: |
| 89 the new configuration that will be merged into `default` | 89 the new configuration that will be logically merged |
| 90 into `default` | |
| 90 :param ~configmix.config.Configuration default: | 91 :param ~configmix.config.Configuration default: |
| 91 the base configuration where `user` is merged into | 92 the base configuration where `user` is logically merged into |
| 92 :param bool _first: an intexrnal argument for controlling recursion | 93 :returns: `user` with the necessary amendments from `default`. |
| 94 If `user` is ``None`` then `default` is returned. | |
| 95 | |
| 96 .. note:: The configuration in `default` is not changed but the | |
| 97 configuration given in `user` is changed **inplace**. | |
| 93 | 98 |
| 94 From http://stackoverflow.com/questions/823196/yaml-merge-in-python | 99 From http://stackoverflow.com/questions/823196/yaml-merge-in-python |
| 95 | 100 |
| 96 .. note:: `_first` is an internal argument. | |
| 97 | |
| 98 """ | 101 """ |
| 99 if _first and (user is None): | 102 if user is None: |
| 100 return default | 103 return default |
| 101 if isinstance(user, dict) and isinstance(default, dict): | 104 if isinstance(user, dict) and isinstance(default, dict): |
| 102 for k, v in default.items(): | 105 for k, v in default.items(): |
| 103 if k not in user: | 106 if k not in user: |
| 104 user[k] = v | 107 user[k] = v |
| 105 else: | 108 else: |
| 106 user[k] = merge(user[k], v, False) | 109 user[k] = _merge(user[k], v) |
| 107 return user | 110 return user |
| 108 | 111 |
| 109 | 112 |
| 110 def safe_merge(user, default, _first=True): | 113 def _merge(user, default): |
| 111 """A more safe version of :func:`merge()` that makes shallow copies of | 114 """Recursion helper for :meth:`merge` |
| 115 | |
| 116 """ | |
| 117 if isinstance(user, dict) and isinstance(default, dict): | |
| 118 for k, v in default.items(): | |
| 119 if k not in user: | |
| 120 user[k] = v | |
| 121 else: | |
| 122 user[k] = _merge(user[k], v) | |
| 123 return user | |
| 124 | |
| 125 | |
| 126 def safe_merge(user, default): | |
| 127 """A more safe version of :func:`merge` that makes shallow copies of | |
| 112 the returned container objects. | 128 the returned container objects. |
| 113 | 129 |
| 114 .. note:: `_first` is an internal argument. | |
| 115 | |
| 116 """ | 130 """ |
| 117 if _first and (user is None): | 131 if user is None: |
| 118 return copy.copy(default) | 132 return copy.copy(default) |
| 119 user = copy.copy(user) | 133 user = copy.copy(user) |
| 120 if isinstance(user, dict) and isinstance(default, dict): | 134 if isinstance(user, dict) and isinstance(default, dict): |
| 121 for k, v in default.items(): | 135 for k, v in default.items(): |
| 122 if k not in user: | 136 if k not in user: |
| 123 user[k] = copy.copy(v) | 137 user[k] = copy.copy(v) |
| 124 else: | 138 else: |
| 125 user[k] = merge(user[k], v, False) | 139 user[k] = _merge(user[k], v) |
| 126 return user | 140 return user |
