changeset 186:e61fbae61402

sources: Pass any current mode to `_populateMetadata` when finding pages. Page sources like the `prose` source may need to open an existing page's file to read stuff from it. This won't work if the metadata is populated as part of finding a path to create a page (like when running `chef prepare`). We pass the mode to `_populateMetadata` so the underlying class now knows the current context in which it is called.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 04 Jan 2015 15:48:29 -0800
parents 139179dc7abd
children d5b7c2a4ec9d
files piecrust/sources/base.py piecrust/sources/prose.py
diffstat 2 files changed, 18 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/piecrust/sources/base.py	Sun Jan 04 14:59:12 2015 -0800
+++ b/piecrust/sources/base.py	Sun Jan 04 15:48:29 2015 -0800
@@ -309,7 +309,8 @@
             yield CachedPageFactory(p)
 
 
-class SimplePageSource(PageSource, IListableSource):
+class SimplePageSource(PageSource, IListableSource, IPreparingSource,
+                       SimplePaginationSourceMixin):
     def __init__(self, app, name, config):
         super(SimplePageSource, self).__init__(app, name, config)
         self.fs_endpoint = config.get('fs_endpoint', name)
@@ -353,7 +354,7 @@
                 path = '%s.%s' % (path, self.default_auto_format)
             rel_path = os.path.relpath(path, self.fs_endpoint_path)
             rel_path = rel_path.replace('\\', '/')
-            self._populateMetadata(rel_path, metadata)
+            self._populateMetadata(rel_path, metadata, mode)
             return rel_path, metadata
 
         if ext == '':
@@ -366,7 +367,7 @@
             if os.path.isfile(path):
                 rel_path = os.path.relpath(path, self.fs_endpoint_path)
                 rel_path = rel_path.replace('\\', '/')
-                self._populateMetadata(rel_path, metadata)
+                self._populateMetadata(rel_path, metadata, mode)
                 return rel_path, metadata
 
         return None, None
@@ -403,6 +404,12 @@
         name, _ = os.path.splitext(filename)
         return name
 
+    def setupPrepareParser(self, parser, app):
+        parser.add_argument('uri', help='The URI for the new page.')
+
+    def buildMetadata(self, args):
+        return {'path': args.uri}
+
     def _makeSlug(self, rel_path):
         slug, ext = os.path.splitext(rel_path)
         slug = slug.replace('\\', '/')
@@ -422,24 +429,16 @@
                 f[-1] != '~' and  # Vim temp files and what-not
                 f not in ['Thumbs.db'])  # Windows bullshit
 
-    def _populateMetadata(self, rel_path, metadata):
+    def _populateMetadata(self, rel_path, metadata, mode=None):
         pass
 
 
-class DefaultPageSource(SimplePageSource,
-                        IPreparingSource, IListableSource,
-                        SimplePaginationSourceMixin):
+class DefaultPageSource(SimplePageSource):
     SOURCE_NAME = 'default'
 
     def __init__(self, app, name, config):
         super(DefaultPageSource, self).__init__(app, name, config)
 
-    def setupPrepareParser(self, parser, app):
-        parser.add_argument('uri', help='The URI for the new page.')
-
-    def buildMetadata(self, args):
-        return {'path': args.uri}
-
 
 class SourceFactoryIterator(object):
     def __init__(self, source):
--- a/piecrust/sources/prose.py	Sun Jan 04 14:59:12 2015 -0800
+++ b/piecrust/sources/prose.py	Sun Jan 04 15:48:29 2015 -0800
@@ -2,7 +2,8 @@
 import os.path
 import logging
 from piecrust.sources.base import (
-        SimplePageSource, SimplePaginationSourceMixin)
+        SimplePageSource, SimplePaginationSourceMixin,
+        MODE_CREATING)
 
 
 logger = logging.getLogger(__name__)
@@ -16,12 +17,12 @@
         super(ProseSource, self).__init__(app, name, config)
         self.config_recipe = config.get('config', {})
 
-    def _populateMetadata(self, rel_path, metadata):
-        metadata['config'] = self._makeConfig(rel_path)
+    def _populateMetadata(self, rel_path, metadata, mode=None):
+        metadata['config'] = self._makeConfig(rel_path, mode)
 
-    def _makeConfig(self, rel_path):
+    def _makeConfig(self, rel_path, mode):
         c = dict(self.config_recipe)
-        if c.get('title') == '%first_line%':
+        if c.get('title') == '%first_line%' and mode != MODE_CREATING:
             path = os.path.join(self.fs_endpoint_path, rel_path)
             c['title'] = get_first_line(path)
         return c