Mercurial > piecrust2
view piecrust/data/base.py @ 979:45ad976712ec
tests: Big push to get the tests to pass again.
- Lots of fixes everywhere in the code.
- Try to handle debug logging in the multiprocessing worker pool when running in pytest. Not perfect, but usable for now.
- Replace all `.md` test files with `.html` since now a auto-format extension always sets the format.
- Replace `out` with `outfiles` in most places since now blog archives are added to the bake output and I don't want to add expected outputs for blog archives everywhere.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sun, 29 Oct 2017 22:51:57 -0700 |
parents | 1d5f02778723 |
children | 8adc27285d93 |
line wrap: on
line source
import collections.abc class MergedMapping(collections.abc.Mapping): """ Provides a dictionary-like object that's really the aggregation of multiple dictionary-like objects. """ def __init__(self, dicts, path=''): self._dicts = dicts self._path = path def __getattr__(self, name): try: return self[name] except KeyError: raise AttributeError("No such attribute: %s" % self._subp(name)) def __getitem__(self, name): values = [] for d in self._dicts: try: val = getattr(d, name) values.append(val) continue except AttributeError: pass try: val = d[name] values.append(val) continue except KeyError: pass if len(values) == 0: raise KeyError("No such item: %s" % self._subp(name)) if len(values) == 1: return values[0] for val in values: if not isinstance(val, (dict, collections.abc.Mapping)): raise Exception( "Template data for '%s' contains an incompatible mix " "of data: %s" % ( self._subp(name), ', '.join([str(type(v)) for v in values]))) return MergedMapping(values, self._subp(name)) def __iter__(self): keys = set() for d in self._dicts: keys |= set(d.keys()) return iter(keys) def __len__(self): keys = set() for d in self._dicts: keys |= set(d.keys()) return len(keys) def _subp(self, name): return '%s/%s' % (self._path, name) def _prependMapping(self, d): self._dicts.insert(0, d) def _appendMapping(self, d): self._dicts.append(d)