view piecrust/taxonomies.py @ 440:32c7c2d219d2

performance: Refactor how data is managed to reduce copying. * Make use of `collections.abc.Mapping` to better identify things that are supposed to look like dictionaries. * Instead of handling "overlay" of data in a dict tree in each different data object, make all objects `Mapping`s and handle merging at a higher level with the new `MergedMapping` object. * Since this new object is read-only, remove the need for deep-copying of app and page configurations. * Split data classes into separate modules.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 28 Jun 2015 08:22:39 -0700
parents e7b865f8f335
children
line wrap: on
line source

from piecrust.sources.pageref import PageRef, PageNotFoundError


class Taxonomy(object):
    def __init__(self, app, name, config):
        self.app = app
        self.name = name
        self.term_name = config.get('term', name)
        self.is_multiple = config.get('multiple', False)
        self.page_ref = config.get('page')
        self._source_page_refs = {}

    @property
    def setting_name(self):
        if self.is_multiple:
            return self.name
        return self.term_name

    def resolvePagePath(self, source):
        pr = self.getPageRef(source)
        try:
            return pr.path
        except PageNotFoundError:
            return None

    def getPageRef(self, source):
        if source.name in self._source_page_refs:
            return self._source_page_refs[source.name]

        ref_path = source.getTaxonomyPageRef(self.name)
        page_ref = PageRef(self.app, ref_path)
        self._source_page_refs[source.name] = page_ref
        return page_ref