Mercurial > piecrust2
view piecrust/data/providersdata.py @ 924:1bb704434ee2
formatting: Remove segment parts, you can use template tags instead.
Segment parts were used to switch formatters insides a given content segment,
but that's also achievable with template tags like `pcformat` in Jinja to
some degree. It's not totally the same but removing it simplifies the code
and improves performance.
| author | Ludovic Chabant <ludovic@chabant.com> |
|---|---|
| date | Sun, 01 Oct 2017 20:36:04 -0700 |
| parents | 08e02c2a2a1a |
| children | 45ad976712ec |
line wrap: on
line source
import re import collections.abc from piecrust.configuration import ConfigurationError from piecrust.dataproviders.base import ( DataProvider, build_data_provider) re_endpoint_sep = re.compile(r'[\/\.]') class DataProvidersData(collections.abc.Mapping): def __init__(self, page): self._page = page self._dict = None def __getitem__(self, name): self._load() return self._dict[name] def __iter__(self): self._load() return iter(self._dict) def __len__(self): self._load() return len(self._dict) def _load(self): if self._dict is not None: return self._dict = {} for source in self._page.app.sources: pname = source.config.get('data_type') pendpoint = source.config.get('data_endpoint') if not pname or not pendpoint: continue endpoint_bits = re_endpoint_sep.split(pendpoint) endpoint = self._dict for e in endpoint_bits[:-1]: if e not in endpoint: endpoint[e] = {} endpoint = endpoint[e] existing = endpoint.get(endpoint_bits[-1]) if existing is None: provider = build_data_provider(pname, source, self._page) endpoint[endpoint_bits[-1]] = provider elif isinstance(existing, DataProvider): if existing.PROVIDER_NAME != pname: raise ConfigurationError( "Can't combine data providers '%s' and '%' on " "endpoint '%s'." % (existing.PROVIDER_NAME, pname, pendpoint)) existing._addSource(source) else: raise ConfigurationError( "Endpoint '%s' can't be used for a data provider because " "it's already used for something else." % pendpoint)
