comparison piecrust/data/providersdata.py @ 854:08e02c2a2a1a

core: Keep refactoring, this time to prepare for generator sources. - Make a few APIs simpler. - Content pipelines create their own jobs, so that generator sources can keep aborting in `getContents`, but rely on their pipeline to generate pages for baking.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 04 Jun 2017 23:34:28 -0700
parents 71c4f43d8fc1
children 45ad976712ec
comparison
equal deleted inserted replaced
853:f070a4fc033c 854:08e02c2a2a1a
1 import re 1 import re
2 import collections.abc 2 import collections.abc
3 from piecrust.configuration import ConfigurationError
4 from piecrust.dataproviders.base import (
5 DataProvider, build_data_provider)
3 6
4 7
5 re_endpoint_sep = re.compile(r'[\/\.]') 8 re_endpoint_sep = re.compile(r'[\/\.]')
6 9
7 10
25 def _load(self): 28 def _load(self):
26 if self._dict is not None: 29 if self._dict is not None:
27 return 30 return
28 31
29 self._dict = {} 32 self._dict = {}
30 for source in self._page.app.sources + self._page.app.generators: 33 for source in self._page.app.sources:
31 if source.data_endpoint: 34 pname = source.config.get('data_type')
32 endpoint_bits = re_endpoint_sep.split(source.data_endpoint) 35 pendpoint = source.config.get('data_endpoint')
33 endpoint = self._dict 36 if not pname or not pendpoint:
34 for e in endpoint_bits[:-1]: 37 continue
35 if e not in endpoint: 38
36 endpoint[e] = {} 39 endpoint_bits = re_endpoint_sep.split(pendpoint)
37 endpoint = endpoint[e] 40 endpoint = self._dict
38 override = endpoint.get(endpoint_bits[-1]) 41 for e in endpoint_bits[:-1]:
39 provider = source.buildDataProvider(self._page, override) 42 if e not in endpoint:
40 if provider is not None: 43 endpoint[e] = {}
41 endpoint[endpoint_bits[-1]] = provider 44 endpoint = endpoint[e]
45 existing = endpoint.get(endpoint_bits[-1])
46
47 if existing is None:
48 provider = build_data_provider(pname, source, self._page)
49 endpoint[endpoint_bits[-1]] = provider
50 elif isinstance(existing, DataProvider):
51 if existing.PROVIDER_NAME != pname:
52 raise ConfigurationError(
53 "Can't combine data providers '%s' and '%' on "
54 "endpoint '%s'." %
55 (existing.PROVIDER_NAME, pname, pendpoint))
56 existing._addSource(source)
57 else:
58 raise ConfigurationError(
59 "Endpoint '%s' can't be used for a data provider because "
60 "it's already used for something else." % pendpoint)