view piecrust/data/providersdata.py @ 852:4850f8c21b6e

core: Start of the big refactor for PieCrust 3.0. * Everything is a `ContentSource`, including assets directories. * Most content sources are subclasses of the base file-system source. * A source is processed by a "pipeline", and there are 2 built-in pipelines, one for assets and one for pages. The asset pipeline is vaguely functional, but the page pipeline is completely broken right now. * Rewrite the baking process as just running appropriate pipelines on each content item. This should allow for better parallelization.
author Ludovic Chabant <ludovic@chabant.com>
date Wed, 17 May 2017 00:11:48 -0700
parents 71c4f43d8fc1
children 08e02c2a2a1a
line wrap: on
line source

import re
import collections.abc


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 + 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