Beispiel #1
0
    def __init__(self, engine, name, match, group_by, order='slug'):
        self.engine = engine
        self.name = name
        self.match = match
        self.group_by = group_by
        self.order = order

        self.collections = {}

        # Load shared config for collections
        collection_meta = engine.config['collections'][name].get('meta', {})
        self.meta = jules._BundleMeta(engine.config.get('bundle_defaults', {}), collection_meta)
Beispiel #2
0
    def collect(self, rule, group_by_property, bundle):
        """Collect the bungle if it is a proper candidate for the collection.

        rule: The rule to match the bundle by.
            is - match if the grouper property is equal to the group value
            in - match if the grouper property contains the group value
        group_by_property: The meta property on which to group bundles by
        bundle: The bundle to collect, if it matches the rules, into one
            of the collections being kept by theis collector.
        """

        try:
            value = bundle.meta[group_by_property]
        except KeyError:
            pass
        else:
            if rule == 'is':
                values = [value]
            elif rule == 'in':
                values = value

            key_pattern = self.engine.config['collections'][self.name].get('key_pattern', '{value}')
            for value in values:
                key = key_pattern.format(property=group_by_property, value=value)
                if not value in self.collections:
                    # Add if new, merge with a YAML file, but stop if we'd overwrite a bundle
                    replace = False
                    meta = self.meta
                    try:
                        existing = self.engine.get_bundle(key=key)
                    except ValueError:
                        pass
                    else:
                        if ['yaml'] == list(existing.parts):
                            meta = jules._BundleMeta(meta, existing.meta)
                            replace = True
                    collection = Collection(key, rule, group_by_property, value, meta)
                    self.engine.add_bundles({
                        key: collection,
                    }, replace=replace)
                    self.collections[value] = collection
                else:
                    collection = self.collections[value]
                collection[bundle.key] = bundle
            # All bundles need a copy of this collection
            for colkey, collection in self.collections.items():
                for value in values:
                    self.engine.context['collections'].setdefault(group_by_property, set()).add(collection)
                    if value == collection.value:
                        bundle.meta['collections'].setdefault(group_by_property, set()).add(collection)
                        collection.add_bundle(bundle)