comparison piecrust/data/assetor.py @ 915:9d804fa186a0

data: Make the `Assetor` more into a `dict` than a `list`.
author Ludovic Chabant <ludovic@chabant.com>
date Wed, 27 Sep 2017 19:05:34 -0700
parents dcdec4b951a1
children a0a62d0da723
comparison
equal deleted inserted replaced
911:f2b75e4be981 915:9d804fa186a0
1 import os 1 import os
2 import os.path 2 import os.path
3 import logging 3 import logging
4 import collections.abc
5 from piecrust.sources.base import REL_ASSETS 4 from piecrust.sources.base import REL_ASSETS
6 from piecrust.uriutil import multi_replace 5 from piecrust.uriutil import multi_replace
7 6
8 7
9 logger = logging.getLogger(__name__) 8 logger = logging.getLogger(__name__)
17 def __init__(self, content_item, uri): 16 def __init__(self, content_item, uri):
18 self.content_item = content_item 17 self.content_item = content_item
19 self.uri = uri 18 self.uri = uri
20 19
21 20
22 class Assetor(collections.abc.Sequence): 21 class Assetor:
23 debug_render_doc = """Helps render URLs to files in the current page's 22 debug_render_doc = """Helps render URLs to files in the current page's
24 asset folder.""" 23 asset folder."""
25 debug_render = [] 24 debug_render = []
26 debug_render_dynamic = ['_debugRenderAssetNames'] 25 debug_render_dynamic = ['_debugRenderAssetNames']
27 26
35 self._cacheAssets() 34 self._cacheAssets()
36 return self._cache_map[name].uri 35 return self._cache_map[name].uri
37 except KeyError: 36 except KeyError:
38 raise AttributeError() 37 raise AttributeError()
39 38
40 def __getitem__(self, i): 39 def __getitem__(self, name):
41 self._cacheAssets() 40 self._cacheAssets()
42 return self._cache_list[i] 41 return self._cache_map[name].uri
42
43 def __contains__(self, name):
44 self._cacheAssets()
45 return name in self._cache_map
46
47 def __iter__(self):
48 self._cacheAssets()
49 return iter(self._cache_list)
43 50
44 def __len__(self): 51 def __len__(self):
45 self._cacheAssets() 52 self._cacheAssets()
46 return len(self._cache_list) 53 return len(self._cache_map)
47 54
48 def _getAssetNames(self): 55 def _getAssetNames(self):
49 self._cacheAssets() 56 self._cacheAssets()
50 return self._cache_map.keys() 57 return self._cache_map.keys()
51 58