changeset 577:ff404adfcf45

sources: Add method to get a page factory from a path.
author Ludovic Chabant <ludovic@chabant.com>
date Wed, 30 Dec 2015 14:45:57 -0800
parents 0c74a6c4533d
children 683be25cbdb2
files piecrust/sources/base.py piecrust/sources/default.py piecrust/sources/posts.py
diffstat 3 files changed, 45 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/piecrust/sources/base.py	Sat Dec 19 18:07:21 2015 -0800
+++ b/piecrust/sources/base.py	Wed Dec 30 14:45:57 2015 -0800
@@ -109,6 +109,9 @@
     def buildPageFactories(self):
         raise NotImplementedError()
 
+    def buildPageFactory(self, path):
+        raise NotImplementedError()
+
     def resolveRef(self, ref_path):
         """ Returns the full path and source metadata given a source
             (relative) path, like a ref-spec.
--- a/piecrust/sources/default.py	Sat Dec 19 18:07:21 2015 -0800
+++ b/piecrust/sources/default.py	Wed Dec 30 14:45:57 2015 -0800
@@ -58,6 +58,17 @@
                 self._populateMetadata(fac_path, metadata)
                 yield PageFactory(self, fac_path, metadata)
 
+    def buildPageFactory(self, path):
+        if not path.startswith(self.fs_endpoint_path):
+            raise Exception("Page path '%s' isn't inside '%s'." % (
+                    path, self.fs_enpoint_path))
+        rel_path = path[len(self.fs_endpoint_path):].lstrip('\\/')
+        slug = self._makeSlug(rel_path)
+        metadata = {'slug': slug}
+        fac_path = rel_path.replace('\\', '/')
+        self._populateMetadata(fac_path, metadata)
+        return PageFactory(self, fac_path, metadata)
+
     def resolveRef(self, ref_path):
         path = os.path.normpath(
                 os.path.join(self.fs_endpoint_path, ref_path.lstrip("\\/")))
--- a/piecrust/sources/posts.py	Sat Dec 19 18:07:21 2015 -0800
+++ b/piecrust/sources/posts.py	Wed Dec 30 14:45:57 2015 -0800
@@ -10,6 +10,7 @@
 from piecrust.sources.interfaces import (
         IPreparingSource, IInteractiveSource, InteractiveField)
 from piecrust.sources.mixins import SimplePaginationSourceMixin
+from piecrust.uriutil import multi_replace
 
 
 logger = logging.getLogger(__name__)
@@ -35,6 +36,36 @@
         metadata = self._parseMetadataFromPath(ref_path)
         return path, metadata
 
+    def buildPageFactory(self, path):
+        if not path.startswith(self.fs_endpoint_path):
+            raise Exception("Page path '%s' isn't inside '%s'." % (
+                    path, self.fs_endpoint_path))
+        rel_path = path[len(self.fs_endpoint_path):].lstrip('\\/')
+        pat = self.PATH_FORMAT % {
+                'year': 'YEAR',
+                'month': 'MONTH',
+                'day': 'DAY',
+                'slug': 'SLUG',
+                'ext': 'EXT'}
+        pat = re.escape(pat)
+        pat = multi_replace(pat, {
+                'YEAR': '(\d{4})',
+                'MONTH': '(\d{2})',
+                'DAY': '(\d{2})',
+                'SLUG': '(.*)',
+                'EXT': '(.*)'})
+        m = re.match(pat, rel_path)
+        if m is None:
+            raise Exception("'%s' isn't a proper %s page path." % (
+                    rel_path, self.SOURCE_NAME))
+        return self._makeFactory(
+                rel_path,
+                m.group(4),
+                int(m.group(1)),
+                int(m.group(2)),
+                int(m.group(3)))
+
+
     def findPageFactory(self, metadata, mode):
         year = metadata.get('year')
         month = metadata.get('month')