view piecrust/sources/mixins.py @ 853:f070a4fc033c

core: Continue PieCrust3 refactor, simplify pages. The asset pipeline is still the only function pipeline at this point. * No more `QualifiedPage`, and several other pieces of code deleted. * Data providers are simpler and more focused. For instance, the page iterator doesn't try to support other types of items. * Route parameters are proper known source metadata to remove the confusion between the two. * Make the baker and pipeline more correctly manage records and record histories. * Add support for record collapsing and deleting stale outputs in the asset pipeline.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 21 May 2017 00:06:59 -0700
parents 4850f8c21b6e
children fddaf43424e2
line wrap: on
line source

import os.path
import logging
from piecrust import osutil
from piecrust.sources.base import ContentItem


logger = logging.getLogger(__name__)

assets_suffix = '-assets'


class SimpleAssetsSubDirMixin:
    def _getRelatedAssetsContents(self, item, relationship):
        if not item.metadata.get('__has_assets', False):
            return None

        assets = {}
        assets_dir = item.spec + assets_suffix
        for f in osutil.listdir(assets_dir):
            fpath = os.path.join(assets_dir, f)
            name, _ = os.path.splitext(f)
            if name in assets:
                raise Exception("Multiple assets are named '%s'." %
                                name)
            assets[name] = ContentItem(fpath, {'__is_asset': True})
        return assets

    def _onFinalizeContent(self, parent_group, items, groups):
        assetsGroups = []
        for g in groups:
            if not g.spec.endswith(assets_suffix):
                continue
            match = g.spec[:-len(assets_suffix)]
            item = next(filter(lambda i: i.spec == match), None)
            if item:
                item.metadata['__has_assets'] = True
                assetsGroups.append(g)
        for g in assetsGroups:
            groups.remove(g)