changeset 833:cb39c0dbe5f0

Merge pull request #40 from GitHub.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 05 Feb 2017 22:42:18 -0800
parents c30574f0dadc (current diff) 61878590bf96 (diff)
children dca51cd8147a
files piecrust/sources/base.py
diffstat 5 files changed, 39 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/piecrust/baking/single.py	Sun Feb 05 22:37:25 2017 -0800
+++ b/piecrust/baking/single.py	Sun Feb 05 22:42:18 2017 -0800
@@ -1,6 +1,5 @@
 import os.path
 import queue
-import shutil
 import logging
 import threading
 import urllib.parse
@@ -167,14 +166,7 @@
                 logger.debug("Copying page assets to: %s" % out_assets_dir)
                 _ensure_dir_exists(out_assets_dir)
 
-                page_pathname, _ = os.path.splitext(qualified_page.path)
-                in_assets_dir = page_pathname + ASSET_DIR_SUFFIX
-                for fn in os.listdir(in_assets_dir):
-                    full_fn = os.path.join(in_assets_dir, fn)
-                    if os.path.isfile(full_fn):
-                        dest_ap = os.path.join(out_assets_dir, fn)
-                        logger.debug("  %s -> %s" % (full_fn, dest_ap))
-                        shutil.copy(full_fn, dest_ap)
+                qualified_page.source.buildAssetor(qualified_page, sub_uri).copyAssets(out_assets_dir)
 
             # Figure out if we have more work.
             has_more_subs = False
--- a/piecrust/data/assetor.py	Sun Feb 05 22:37:25 2017 -0800
+++ b/piecrust/data/assetor.py	Sun Feb 05 22:42:18 2017 -0800
@@ -1,5 +1,6 @@
 import os
 import os.path
+import shutil
 import logging
 from piecrust import ASSET_DIR_SUFFIX
 from piecrust.uriutil import multi_replace
@@ -31,12 +32,7 @@
     return base_url.rstrip('/') + '/'
 
 
-class Assetor(object):
-    debug_render_doc = """Helps render URLs to files in the current page's
-                          asset folder."""
-    debug_render = []
-    debug_render_dynamic = ['_debugRenderAssetNames']
-
+class AssetorBase(object):
     def __init__(self, page, uri):
         self._page = page
         self._uri = uri
@@ -68,8 +64,23 @@
     def _cacheAssets(self):
         if self._cache is not None:
             return
+            
+        self._cache = dict(self.findAssets())
+        
+    def findAssets(self):
+        raise NotImplementedError()
 
-        self._cache = {}
+    def copyAssets(self, dest_dir):
+        raise NotImplementedError()
+
+class Assetor(AssetorBase):
+    debug_render_doc = """Helps render URLs to files in the current page's
+                          asset folder."""
+    debug_render = []
+    debug_render_dynamic = ['_debugRenderAssetNames']
+
+    def findAssets(self):
+        assets = {}
         name, ext = os.path.splitext(self._page.path)
         assets_dir = name + ASSET_DIR_SUFFIX
         if not os.path.isdir(assets_dir):
@@ -84,12 +95,23 @@
                 continue
 
             name, ext = os.path.splitext(fn)
-            if name in self._cache:
+            if name in assets:
                 raise UnsupportedAssetsError(
                         "Multiple asset files are named '%s'." % name)
-            self._cache[name] = (base_url + fn, full_fn)
+            assets[name] = (base_url + fn, full_fn)
 
         cpi = self._page.app.env.exec_info_stack.current_page_info
         if cpi is not None:
             cpi.render_ctx.current_pass_info.used_assets = True
-
+        
+        return assets
+            
+    def copyAssets(self, dest_dir):
+        page_pathname, _ = os.path.splitext(self._page.path)
+        in_assets_dir = page_pathname + ASSET_DIR_SUFFIX
+        for fn in os.listdir(in_assets_dir):
+            full_fn = os.path.join(in_assets_dir, fn)
+            if os.path.isfile(full_fn):
+                dest_ap = os.path.join(dest_dir, fn)
+                logger.debug("  %s -> %s" % (full_fn, dest_ap))
+                shutil.copy(full_fn, dest_ap)
--- a/piecrust/data/builder.py	Sun Feb 05 22:37:25 2017 -0800
+++ b/piecrust/data/builder.py	Sun Feb 05 22:42:18 2017 -0800
@@ -1,6 +1,5 @@
 import logging
 from werkzeug.utils import cached_property
-from piecrust.data.assetor import Assetor
 from piecrust.data.base import MergedMapping
 from piecrust.data.linker import PageLinkerData
 from piecrust.data.pagedata import PageData
@@ -40,7 +39,7 @@
     paginator = Paginator(page, pgn_source,
                           page_num=ctx.page_num,
                           pgn_filter=ctx.pagination_filter)
-    assetor = Assetor(page, first_uri)
+    assetor = page.source.buildAssetor(page, first_uri)
     linker = PageLinkerData(page.source, page.rel_path)
     data = {
             'piecrust': pc_data,
--- a/piecrust/data/paginationdata.py	Sun Feb 05 22:37:25 2017 -0800
+++ b/piecrust/data/paginationdata.py	Sun Feb 05 22:42:18 2017 -0800
@@ -45,7 +45,7 @@
             self._setValue('date', page.datetime.strftime(date_format))
         self._setValue('mtime', page.path_mtime)
 
-        assetor = Assetor(page, page_url)
+        assetor = page.source.buildAssetor(page, page_url)
         self._setValue('assets', assetor)
 
         segment_names = page.config.get('segments')
--- a/piecrust/sources/base.py	Sun Feb 05 22:37:25 2017 -0800
+++ b/piecrust/sources/base.py	Sun Feb 05 22:42:18 2017 -0800
@@ -2,6 +2,7 @@
 import logging
 from werkzeug.utils import cached_property
 from piecrust.page import Page
+from piecrust.data.assetor import Assetor
 
 
 REALM_USER = 0
@@ -133,3 +134,6 @@
     def finalizeConfig(self, page):
         pass
 
+    def buildAssetor(self, page, uri):
+        return Assetor(page, uri)
+