diff 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
line wrap: on
line diff
--- a/piecrust/data/providersdata.py	Sun May 21 00:06:59 2017 -0700
+++ b/piecrust/data/providersdata.py	Sun Jun 04 23:34:28 2017 -0700
@@ -1,5 +1,8 @@
 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'[\/\.]')
@@ -27,15 +30,31 @@
             return
 
         self._dict = {}
-        for source in self._page.app.sources + self._page.app.generators:
-            if source.data_endpoint:
-                endpoint_bits = re_endpoint_sep.split(source.data_endpoint)
-                endpoint = self._dict
-                for e in endpoint_bits[:-1]:
-                    if e not in endpoint:
-                        endpoint[e] = {}
-                    endpoint = endpoint[e]
-                override = endpoint.get(endpoint_bits[-1])
-                provider = source.buildDataProvider(self._page, override)
-                if provider is not None:
-                    endpoint[endpoint_bits[-1]] = provider
+        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)