diff piecrust/data/assetor.py @ 862:fddaf43424e2

refactor: Get the page assets to work again in the server.
author Ludovic Chabant <ludovic@chabant.com>
date Thu, 08 Jun 2017 23:09:34 -0700
parents f070a4fc033c
children 757fba54bfd3
line wrap: on
line diff
--- a/piecrust/data/assetor.py	Thu Jun 08 08:52:45 2017 -0700
+++ b/piecrust/data/assetor.py	Thu Jun 08 23:09:34 2017 -0700
@@ -2,6 +2,7 @@
 import os.path
 import shutil
 import logging
+import collections.abc
 from piecrust import ASSET_DIR_SUFFIX
 from piecrust.sources.base import REL_ASSETS
 from piecrust.uriutil import multi_replace
@@ -14,26 +15,13 @@
     pass
 
 
-def build_base_url(app, uri, rel_assets_path):
-    base_url_format = app.config.get('site/base_asset_url_format')
-    rel_assets_path = rel_assets_path.replace('\\', '/')
-
-    # Remove any extension since we'll be copying assets into the 1st
-    # sub-page's folder.
-    pretty = app.config.get('site/pretty_urls')
-    if not pretty:
-        uri, _ = os.path.splitext(uri)
-
-    base_url = multi_replace(
-        base_url_format,
-        {
-            '%path%': rel_assets_path,
-            '%uri%': uri})
-
-    return base_url.rstrip('/') + '/'
+class _AssetInfo:
+    def __init__(self, content_item, uri):
+        self.content_item = content_item
+        self.uri = uri
 
 
-class Assetor:
+class Assetor(collections.abc.Mapping):
     debug_render_doc = """Helps render URLs to files in the current page's
                           asset folder."""
     debug_render = []
@@ -46,21 +34,21 @@
     def __getattr__(self, name):
         try:
             self._cacheAssets()
-            return self._cache[name][0]
+            return self._cache[name].uri
         except KeyError:
             raise AttributeError()
 
     def __getitem__(self, key):
         self._cacheAssets()
-        return self._cache[key][0]
+        return self._cache[key].uri
 
     def __iter__(self):
         self._cacheAssets()
-        return map(lambda i: i[0], self._cache.values())
+        return self._cache.keys()
 
-    def allNames(self):
+    def __len__(self):
         self._cacheAssets()
-        return list(self._cache.keys())
+        return len(self._cache)
 
     def _debugRenderAssetNames(self):
         self._cacheAssets()
@@ -70,25 +58,52 @@
         if self._cache is not None:
             return
 
-        self._cache = self.findAssets() or {}
-
-    def findAssets(self):
+        source = self._page.source
         content_item = self._page.content_item
-        source = content_item.source
-        assets = source.getRelatedContent(content_item, REL_ASSETS)
+
+        assets = source.getRelatedContents(content_item, REL_ASSETS)
         if assets is None:
-            return {}
+            self._cache = {}
+            return
+
+        self._cache = {}
 
         app = source.app
+        root_dir = app.root_dir
+        asset_url_format = app.config.get('site/asset_url_format')
+
+        page_uri = self._page.getUri()
+        pretty_urls = app.config.get('site/pretty_urls')
+        if not pretty_urls:
+            page_uri, _ = os.path.splitext(page_uri)
+
+        uri_build_tokens = {
+            '%path%': None,
+            '%filename%': None,
+            '%page_uri%': page_uri
+        }
+
+        for a in assets:
+            name = a.metadata['name']
+            if name in self._cache:
+                raise UnsupportedAssetsError(
+                    "An asset with name '%s' already exists for item '%s'. "
+                    "Do you have multiple assets with colliding names?" %
+                    (name, content_item.spec))
+
+            # TODO: this assumes a file-system source!
+            uri_build_tokens['%path%'] = (
+                os.path.relpath(a.spec, root_dir).replace('\\', '/'))
+            uri_build_tokens['%filename%'] = a.metadata['filename'],
+            uri = multi_replace(asset_url_format, uri_build_tokens)
+
+            self._cache[name] = _AssetInfo(a, uri)
+
         stack = app.env.render_ctx_stack
         cur_ctx = stack.current_ctx
         if cur_ctx is not None:
             cur_ctx.current_pass_info.used_assets = True
 
-        # base_url = build_base_url(app, self._uri, rel_assets_dir)
-
-        return assets
-
     def copyAssets(self, dest_dir):
         page_pathname, _ = os.path.splitext(self._page.path)
         in_assets_dir = page_pathname + ASSET_DIR_SUFFIX