diff piecrust/data/assetor.py @ 867:757fba54bfd3

refactor: Improve pagination and iterators to work with other sources. This makes the assets work as a pagination source again.
author Ludovic Chabant <ludovic@chabant.com>
date Mon, 12 Jun 2017 22:20:58 -0700
parents fddaf43424e2
children 48d25fd68b8d
line wrap: on
line diff
--- a/piecrust/data/assetor.py	Mon Jun 12 22:10:50 2017 -0700
+++ b/piecrust/data/assetor.py	Mon Jun 12 22:20:58 2017 -0700
@@ -21,7 +21,7 @@
         self.uri = uri
 
 
-class Assetor(collections.abc.Mapping):
+class Assetor(collections.abc.Sequence):
     debug_render_doc = """Helps render URLs to files in the current page's
                           asset folder."""
     debug_render = []
@@ -29,7 +29,8 @@
 
     def __init__(self, page):
         self._page = page
-        self._cache = None
+        self._cache_map = None
+        self._cache_list = None
 
     def __getattr__(self, name):
         try:
@@ -38,36 +39,32 @@
         except KeyError:
             raise AttributeError()
 
-    def __getitem__(self, key):
+    def __getitem__(self, i):
         self._cacheAssets()
-        return self._cache[key].uri
-
-    def __iter__(self):
-        self._cacheAssets()
-        return self._cache.keys()
+        return self._cache_list[i]
 
     def __len__(self):
         self._cacheAssets()
-        return len(self._cache)
+        return len(self._cache_list)
 
     def _debugRenderAssetNames(self):
         self._cacheAssets()
         return list(self._cache.keys())
 
     def _cacheAssets(self):
-        if self._cache is not None:
+        if self._cache_map is not None:
             return
 
         source = self._page.source
         content_item = self._page.content_item
+        assets = source.getRelatedContents(content_item, REL_ASSETS)
 
-        assets = source.getRelatedContents(content_item, REL_ASSETS)
+        self._cache_map = {}
+        self._cache_list = []
+
         if assets is None:
-            self._cache = {}
             return
 
-        self._cache = {}
-
         app = source.app
         root_dir = app.root_dir
         asset_url_format = app.config.get('site/asset_url_format')
@@ -85,7 +82,7 @@
 
         for a in assets:
             name = a.metadata['name']
-            if name in self._cache:
+            if name in self._cache_map:
                 raise UnsupportedAssetsError(
                     "An asset with name '%s' already exists for item '%s'. "
                     "Do you have multiple assets with colliding names?" %
@@ -97,7 +94,8 @@
             uri_build_tokens['%filename%'] = a.metadata['filename'],
             uri = multi_replace(asset_url_format, uri_build_tokens)
 
-            self._cache[name] = _AssetInfo(a, uri)
+            self._cache_map[name] = _AssetInfo(a, uri)
+            self._cache_list.append(uri)
 
         stack = app.env.render_ctx_stack
         cur_ctx = stack.current_ctx