comparison piecrust/sources/mixins.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 13e8b50a2113
comparison
equal deleted inserted replaced
861:d214918d4d2c 862:fddaf43424e2
8 8
9 assets_suffix = '-assets' 9 assets_suffix = '-assets'
10 10
11 11
12 class SimpleAssetsSubDirMixin: 12 class SimpleAssetsSubDirMixin:
13 def _getRelatedAssetsContents(self, item, relationship): 13 """ A content source mixin for sources that are file-system-based,
14 if not item.metadata.get('__has_assets', False): 14 and have item assets stored in a sub-folder that is named after
15 the item.
16
17 More specifically, assets are stored in a sub-folder named:
18 `<item_path>-assets`
19 """
20 def _getRelatedAssetsContents(self, item):
21 spec_no_ext, _ = os.path.splitext(item.spec)
22 assets_dir = spec_no_ext + assets_suffix
23 try:
24 asset_files = osutil.listdir(assets_dir)
25 except OSError:
15 return None 26 return None
16 27
17 assets = {} 28 assets = []
18 assets_dir = item.spec + assets_suffix 29 for f in asset_files:
19 for f in osutil.listdir(assets_dir):
20 fpath = os.path.join(assets_dir, f) 30 fpath = os.path.join(assets_dir, f)
21 name, _ = os.path.splitext(f) 31 name, _ = os.path.splitext(f)
22 if name in assets: 32 assets.append(ContentItem(
23 raise Exception("Multiple assets are named '%s'." % 33 fpath,
24 name) 34 {'name': name,
25 assets[name] = ContentItem(fpath, {'__is_asset': True}) 35 'filename': f,
36 '__is_asset': True}))
26 return assets 37 return assets
27 38
28 def _onFinalizeContent(self, parent_group, items, groups): 39 def _removeAssetGroups(self, groups):
29 assetsGroups = [] 40 asset_groups = [g for g in groups
30 for g in groups: 41 if g.spec.endswith(assets_suffix)]
31 if not g.spec.endswith(assets_suffix): 42 for g in asset_groups:
32 continue
33 match = g.spec[:-len(assets_suffix)]
34 item = next(filter(lambda i: i.spec == match), None)
35 if item:
36 item.metadata['__has_assets'] = True
37 assetsGroups.append(g)
38 for g in assetsGroups:
39 groups.remove(g) 43 groups.remove(g)
40