comparison piecrust/data/assetor.py @ 6:f5ca5c5bed85

More Python 3 fixes, modularization, and new unit tests.
author Ludovic Chabant <ludovic@chabant.com>
date Sat, 16 Aug 2014 08:15:30 -0700
parents 474c9882decf
children a8f9c78a6608
comparison
equal deleted inserted replaced
5:474c9882decf 6:f5ca5c5bed85
1 import os
1 import os.path 2 import os.path
2 import logging 3 import logging
3 from piecrust.uriutil import multi_replace 4 from piecrust.uriutil import multi_replace
4 5
5 6
6 logger = logging.getLogger(__name__) 7 logger = logging.getLogger(__name__)
8
9
10 class UnsupportedAssetsError(Exception):
11 pass
7 12
8 13
9 def build_base_url(app, uri, assets_path): 14 def build_base_url(app, uri, assets_path):
10 base_url_format = app.env.base_asset_url_format 15 base_url_format = app.env.base_asset_url_format
11 site_root = app.config.get('site/root') 16 site_root = app.config.get('site/root')
12 rel_path = os.path.relpath(assets_path, app.root_dir) 17 rel_path = os.path.relpath(assets_path, app.root_dir)
13 pretty = app.config.get('site/pretty_urls') 18 pretty = app.config.get('site/pretty_urls')
14 if not pretty: 19 if not pretty:
15 uri, _ = os.path.splitext(uri) 20 uri, _ = os.path.splitext(uri)
21 uri = uri.lstrip('/')
16 base_url = multi_replace( 22 base_url = multi_replace(
17 base_url_format, 23 base_url_format,
18 { 24 {
19 '%site_root%': site_root, 25 '%site_root%': site_root,
20 '%path%': rel_path, 26 '%path%': rel_path,
69 75
70 base_url = build_base_url(self._page.app, self._uri, assets_dir) 76 base_url = build_base_url(self._page.app, self._uri, assets_dir)
71 for _, __, filenames in os.walk(assets_dir): 77 for _, __, filenames in os.walk(assets_dir):
72 for fn in filenames: 78 for fn in filenames:
73 name, ext = os.path.splitext(fn) 79 name, ext = os.path.splitext(fn)
80 if name in self._cache:
81 raise UnsupportedAssetsError(
82 "Multiple asset files are named '%s'." % name)
74 self._cache[name] = base_url + fn 83 self._cache[name] = base_url + fn
75 84