Mercurial > hgrepos > Python > libs > ConfigMix
comparison configmix/config.py @ 381:fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Wed, 10 Nov 2021 01:37:36 +0100 |
| parents | bb4a90fb58e0 |
| children | 24db29162d09 |
comparison
equal
deleted
inserted
replaced
| 380:bb4a90fb58e0 | 381:fe3dfd687621 |
|---|---|
| 99 else: | 99 else: |
| 100 return default | 100 return default |
| 101 else: | 101 else: |
| 102 return varvalue | 102 return varvalue |
| 103 | 103 |
| 104 def getfirstvarl(self, *paths, **kwds): | |
| 105 default = kwds.pop("default", _MARKER) | |
| 106 for path in paths: | |
| 107 if isinstance(path, (list, tuple)): | |
| 108 try: | |
| 109 varvalue = self.getvarl(*path) | |
| 110 except KeyError: | |
| 111 pass | |
| 112 else: | |
| 113 return varvalue | |
| 114 elif isinstance(path, dict): | |
| 115 try: | |
| 116 namespace = path["namespace"] | |
| 117 p = path["path"] | |
| 118 except KeyError: | |
| 119 raise TypeError("a paths dict item must have a `path'" | |
| 120 " and a `namespace' key") | |
| 121 else: | |
| 122 try: | |
| 123 varvalue = self.getvarl(*p, namespace=namespace) | |
| 124 except KeyError: | |
| 125 pass | |
| 126 else: | |
| 127 return varvalue | |
| 128 else: | |
| 129 raise TypeError("a paths item must be a dict, list or tuple") | |
| 130 if default is _MARKER: | |
| 131 raise KeyError( | |
| 132 "none of the given variables found: %r" % (paths,)) | |
| 133 else: | |
| 134 return default | |
| 135 | |
| 104 def getvar(self, varname, default=_MARKER): | 136 def getvar(self, varname, default=_MARKER): |
| 105 """Get a variable of the form ``[ns:][[key1.]key2.]name`` - including | 137 """Get a variable of the form ``[ns:][[key1.]key2.]name`` - including |
| 106 variables from other namespaces. | 138 variables from other namespaces. |
| 107 | 139 |
| 108 No variable interpolation is done and no filters are applied. | 140 No variable interpolation is done and no filters are applied. |
| 158 except KeyError: | 190 except KeyError: |
| 159 if default is _MARKER: | 191 if default is _MARKER: |
| 160 raise | 192 raise |
| 161 else: | 193 else: |
| 162 return default | 194 return default |
| 195 | |
| 196 def getfirstvarl_s(self, *paths, **kwds): | |
| 197 default = kwds.pop("default", _MARKER) | |
| 198 for path in paths: | |
| 199 if isinstance(path, (list, tuple)): | |
| 200 try: | |
| 201 obj = self.getvarl(*path) | |
| 202 except KeyError: | |
| 203 pass | |
| 204 else: | |
| 205 return self.substitute_variables_in_obj(obj) | |
| 206 | |
| 207 elif isinstance(path, dict): | |
| 208 try: | |
| 209 namespace = path["namespace"] | |
| 210 p = path["path"] | |
| 211 except KeyError: | |
| 212 raise TypeError("a paths dict item must have a `path'" | |
| 213 " and a `namespace' key") | |
| 214 else: | |
| 215 try: | |
| 216 obj = self.getvarl(*p, namespace=namespace) | |
| 217 except KeyError: | |
| 218 pass | |
| 219 else: | |
| 220 return self.substitute_variables_in_obj(obj) | |
| 221 else: | |
| 222 raise TypeError("a paths item must be a dict, list or tuple") | |
| 223 if default is _MARKER: | |
| 224 raise KeyError( | |
| 225 "none of the given variables found: %r" % (paths,)) | |
| 226 else: | |
| 227 return default | |
| 163 | 228 |
| 164 def getvar_s(self, varname, default=_MARKER): | 229 def getvar_s(self, varname, default=_MARKER): |
| 165 """Get a variable - including variables from other namespaces. | 230 """Get a variable - including variables from other namespaces. |
| 166 | 231 |
| 167 `varname` is interpreted as in :meth:`.getvar`. But variables | 232 `varname` is interpreted as in :meth:`.getvar`. But variables |
