Mercurial > hgrepos > Python > libs > ConfigMix
comparison configmix/config.py @ 666:0eff8441c4b9 v0.21
Implement iteration support that yields properly jailed configurations for each container item
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Fri, 03 Jun 2022 02:17:12 +0200 |
| parents | 2b1c7a68f913 |
| children | f39b96e2bb2a |
comparison
equal
deleted
inserted
replaced
| 665:9f0842a942b2 | 666:0eff8441c4b9 |
|---|---|
| 1097 jc = _JailedConfiguration(*rootpath) | 1097 jc = _JailedConfiguration(*rootpath) |
| 1098 if bind_root: | 1098 if bind_root: |
| 1099 jc.rebind(self) | 1099 jc.rebind(self) |
| 1100 return jc | 1100 return jc |
| 1101 | 1101 |
| 1102 def iter_jailed(self, rootpath=None, root=None): | |
| 1103 """Iterator that yields properly jailed configurations. | |
| 1104 | |
| 1105 `rootpath` or `root` must refer to a `list` or `dict` container. | |
| 1106 | |
| 1107 """ | |
| 1108 return self.jailed(rootpath=rootpath, root=root).iter_jailed() | |
| 1109 | |
| 1102 | 1110 |
| 1103 class _JailedConfiguration(CoercingMethodsMixin): | 1111 class _JailedConfiguration(CoercingMethodsMixin): |
| 1104 | 1112 |
| 1105 """A jailed and restricted variant of :class:`Configuration`. | 1113 """A jailed and restricted variant of :class:`Configuration`. |
| 1106 | 1114 |
| 1279 return iter(self._base.getvarl_s(*self._path)) | 1287 return iter(self._base.getvarl_s(*self._path)) |
| 1280 | 1288 |
| 1281 def __len__(self): | 1289 def __len__(self): |
| 1282 """Length support for containers""" | 1290 """Length support for containers""" |
| 1283 return len(self._base.getvarl_s(*self._path)) | 1291 return len(self._base.getvarl_s(*self._path)) |
| 1292 | |
| 1293 def iter_jailed(self): | |
| 1294 """Iteration support for containers which yields properly jailed | |
| 1295 sub-jails. | |
| 1296 | |
| 1297 Only supported for type `list` or type `dict` jails. | |
| 1298 | |
| 1299 """ | |
| 1300 container = self._base.getvarl(*self._path) | |
| 1301 if isinstance(container, dict): | |
| 1302 for k in container: | |
| 1303 yield self.jailed(rootpath=(k, )) | |
| 1304 elif isinstance(container, list): | |
| 1305 len_container = len(container) | |
| 1306 idx = 0 | |
| 1307 while idx < len_container: | |
| 1308 yield self.jailed(rootpath=(idx, )) | |
| 1309 idx += 1 | |
| 1310 else: | |
| 1311 raise TypeError( | |
| 1312 "jailed iterators only supported for lists and dicts") | |
| 1284 | 1313 |
| 1285 if PY2: | 1314 if PY2: |
| 1286 | 1315 |
| 1287 def __nonzero__(self): | 1316 def __nonzero__(self): |
| 1288 """Map- and list-style evaluation in boolean context""" | 1317 """Map- and list-style evaluation in boolean context""" |
