changeset 866:d9059257743c

refactor: Make the linker work again.
author Ludovic Chabant <ludovic@chabant.com>
date Mon, 12 Jun 2017 22:10:50 -0700
parents 1bb0d973dc69
children 757fba54bfd3
files piecrust/data/builder.py piecrust/data/linker.py piecrust/sources/base.py piecrust/sources/default.py piecrust/sources/fs.py piecrust/sources/posts.py piecrust/sources/prose.py
diffstat 7 files changed, 76 insertions(+), 47 deletions(-) [+]
line wrap: on
line diff
--- a/piecrust/data/builder.py	Thu Jun 08 23:32:19 2017 -0700
+++ b/piecrust/data/builder.py	Mon Jun 12 22:10:50 2017 -0700
@@ -32,7 +32,7 @@
     paginator = Paginator(pgn_source, page, sub_num,
                           pgn_filter=ctx.pagination_filter)
     assetor = Assetor(page)
-    linker = Linker(page)
+    linker = Linker(page.source, page.content_item)
     data = {
         'piecrust': pc_data,
         'page': config_data,
--- a/piecrust/data/linker.py	Thu Jun 08 23:32:19 2017 -0700
+++ b/piecrust/data/linker.py	Mon Jun 12 22:10:50 2017 -0700
@@ -24,41 +24,39 @@
         'children': '_debugRenderChildren',
         'root': '_debugRenderRoot'}
 
-    def __init__(self, page):
-        self._page = page
-        self._content_item = page.content_item
-        self._source = page.source
-        self._app = page.app
+    def __init__(self, source, content_item):
+        self._source = source
+        self._content_item = content_item
 
-        self._parent = _unloaded
+        self._parent_group = _unloaded
         self._ancestors = None
         self._siblings = None
         self._children = None
 
     @property
     def parent(self):
-        if self._parent is _unloaded:
-            pi = self._source.getRelatedContents(self._content_item,
-                                                 REL_LOGICAL_PARENT_ITEM)
-            if pi is not None:
-                pipage = self._app.getPage(self._source, pi)
-                self._parent = PaginationData(pipage)
-            else:
-                self._parent = None
-        return self._parent
+        a = self.ancestors
+        if a:
+            return a[0]
+        return None
 
     @property
     def ancestors(self):
         if self._ancestors is None:
-            cur_item = self._content_item
+            self._ensureParentGroup()
+
+            src = self._source
+            app = src.app
+
+            cur_group = self._parent_group
             self._ancestors = []
-            while True:
-                pi = self._source.getRelatedContents(
-                    cur_item, REL_LOGICAL_PARENT_ITEM)
+            while cur_group:
+                pi = src.getRelatedContents(cur_group,
+                                            REL_LOGICAL_PARENT_ITEM)
                 if pi is not None:
-                    pipage = self._app.getPage(self._source, pi)
+                    pipage = app.getPage(src, pi)
                     self._ancestors.append(PaginationData(pipage))
-                    cur_item = pi
+                    cur_group = src.getParentGroup(pi)
                 else:
                     break
         return self._ancestors
@@ -66,38 +64,62 @@
     @property
     def siblings(self):
         if self._siblings is None:
+            self._ensureParentGroup()
+
+            src = self._source
+            app = src.app
+
             self._siblings = []
-            parent_group = self._source.getParentGroup(self._content_item)
-            for i in self._source.getContents(parent_group):
+            for i in src.getContents(self._parent_group):
                 if not i.is_group:
-                    ipage = self._app.getPage(self._source, i)
+                    ipage = app.getPage(src, i)
                     self._siblings.append(PaginationData(ipage))
         return self._siblings
 
     @property
     def children(self):
         if self._children is None:
+            src = self._source
+            app = src.app
+
             self._children = []
-            child_group = self._source.getRelatedContents(
-                self._content_item, REL_LOGICAl_CHILD_GROUP)
+            child_group = src.getRelatedContents(self._content_item,
+                                                 REL_LOGICAl_CHILD_GROUP)
             if child_group:
-                for i in self._source.getContents(child_group):
-                    ipage = self._app.getPage(self._source, i)
+                for i in src.getContents(child_group):
+                    ipage = app.getPage(src, i)
                     self._children.append(PaginationData(ipage))
         return self._children
 
+    def forpath(self, path):
+        # TODO: generalize this for sources that aren't file-system based.
+        item = self._source.findContent({'slug': path})
+        return Linker(self._source, item)
+
+    def childrenof(self, path):
+        # TODO: generalize this for sources that aren't file-system based.
+        src = self._source
+        app = src.app
+        group = src.findGroup(path)
+        if group is not None:
+            for i in src.getContents(group):
+                if not i.is_group:
+                    ipage = app.getPage(src, i)
+                    yield PaginationData(ipage)
+        return None
+
+    def _ensureParentGroup(self):
+        if self._parent_group is _unloaded:
+            src = self._source
+            item = self._content_item
+            self._parent_group = src.getParentGroup(item)
+
     def _debugRenderAncestors(self):
-        return [i.name for i in self.ancestors]
+        return [i.title for i in self.ancestors]
 
     def _debugRenderSiblings(self):
-        return [i.name for i in self.siblings]
+        return [i.title for i in self.siblings]
 
     def _debugRenderChildren(self):
-        return [i.name for i in self.children]
+        return [i.title for i in self.children]
 
-    def _debugRenderRoot(self):
-        r = self.root
-        if r is not None:
-            return r.name
-        return None
-
--- a/piecrust/sources/base.py	Thu Jun 08 23:32:19 2017 -0700
+++ b/piecrust/sources/base.py	Mon Jun 12 22:10:50 2017 -0700
@@ -124,6 +124,9 @@
     def getRelatedContents(self, item, relationship):
         raise NotImplementedError()
 
+    def findGroup(self, rel_spec):
+        raise NotImplementedError()
+
     def findContent(self, route_params):
         raise NotImplementedError()
 
--- a/piecrust/sources/default.py	Thu Jun 08 23:32:19 2017 -0700
+++ b/piecrust/sources/default.py	Mon Jun 12 22:10:50 2017 -0700
@@ -31,7 +31,7 @@
         return self._doCreateItemMetadata(path)
 
     def _finalizeContent(self, parent_group, items, groups):
-        SimpleAssetsSubDirMixin._removeAssetGroups(groups)
+        SimpleAssetsSubDirMixin._removeAssetGroups(self, groups)
 
     def _doCreateItemMetadata(self, path):
         slug = self._makeSlug(path)
--- a/piecrust/sources/fs.py	Thu Jun 08 23:32:19 2017 -0700
+++ b/piecrust/sources/fs.py	Mon Jun 12 22:10:50 2017 -0700
@@ -139,6 +139,13 @@
     def _finalizeContent(self, parent_group, items, groups):
         pass
 
+    def findGroup(self, rel_spec):
+        path = os.path.join(self.fs_endpoint_path, rel_spec)
+        if os.path.isdir(path):
+            metadata = self._createGroupMetadata(path)
+            return ContentGroup(path, metadata)
+        return None
+
     def getRelatedContents(self, item, relationship):
         if relationship == REL_LOGICAL_PARENT_ITEM:
             # If we want the logical parent item of a folder, we find a
@@ -165,12 +172,6 @@
 
         return None
 
-    def findContent(self, route_params):
-        rel_path = route_params['path']
-        path = os.path.join(self.fs_endpoint_path, rel_path)
-        metadata = self._createItemMetadata(path)
-        return ContentItem(path, metadata)
-
     def getSupportedRouteParameters(self):
         return [
             RouteParameter('path', RouteParameter.TYPE_PATH)]
--- a/piecrust/sources/posts.py	Thu Jun 08 23:32:19 2017 -0700
+++ b/piecrust/sources/posts.py	Mon Jun 12 22:10:50 2017 -0700
@@ -37,7 +37,7 @@
         return self.__class__.PATH_FORMAT
 
     def _finalizeContent(self, groups):
-        SimpleAssetsSubDirMixin._removeAssetGroups(groups)
+        SimpleAssetsSubDirMixin._removeAssetGroups(self, groups)
 
     def getParentGroup(self, item):
         return None
@@ -48,6 +48,9 @@
                 self, item)
         return FSContentSource.getRelatedContents(self, item, relationship)
 
+    def findGroup(self, spec):
+        return None
+
     def findContent(self, route_params):
         year = route_params.get('year')
         month = route_params.get('month')
--- a/piecrust/sources/prose.py	Thu Jun 08 23:32:19 2017 -0700
+++ b/piecrust/sources/prose.py	Mon Jun 12 22:10:50 2017 -0700
@@ -17,7 +17,7 @@
         metadata = super()._doCreateItemMetadata(path)
         config = metadata.setdefault('config', {})
         config.update(self._makeConfig(path))
-        return config
+        return metadata
 
     def _makeConfig(self, path):
         c = copy.deepcopy(self.config_recipe)