Permalink
Browse files

fix caching of template registrations that reference macros

  • Loading branch information...
1 parent 24e14af commit fa587efc26c285cdc7e23990f947fdbd233ab53a @mcdonc mcdonc committed Oct 5, 2012
View
@@ -367,6 +367,12 @@ def _render(value, system):
@implementer(IChameleonLookup)
class ChameleonRendererLookup(object):
+ spec_re = re.compile(
+ r'(?P<asset>[\w_.:/-]+)'
+ r'(?:\#(?P<defname>[\w_]+))?'
+ r'(\.(?P<ext>.*))'
+ )
+
def __init__(self, impl, registry):
self.impl = impl
self.registry = registry
@@ -417,6 +423,12 @@ def auto_reload(self):
return False
return settings.get('reload_templates', False)
+ def _crack_spec(self, spec):
+ asset, macro, ext = self.spec_re.match(spec).group(
+ 'asset', 'defname', 'ext'
+ )
+ return asset, macro, ext
+
def __call__(self, info):
spec = self.get_spec(info.name, info.package)
registry = info.registry
@@ -436,27 +448,22 @@ def __call__(self, info):
# spec is a package:relpath asset spec
renderer = registry.queryUtility(ITemplateRenderer, name=spec)
if renderer is None:
- p = re.compile(
- r'(?P<asset>[\w_.:/-]+)'
- r'(?:\#(?P<defname>[\w_]+))?'
- r'(\.(?P<ext>.*))'
- )
- asset, macro, ext = p.match(spec).group(
- 'asset', 'defname', 'ext'
- )
- spec = '%s.%s' % (asset, ext)
+ asset, macro, ext = self._crack_spec(spec)
+ spec_without_macro = '%s.%s' % (asset, ext)
try:
- package_name, filename = spec.split(':', 1)
+ package_name, filename = spec_without_macro.split(':', 1)
except ValueError: # pragma: no cover
# somehow we were passed a relative pathname; this
# should die
package_name = caller_package(4).__name__
- filename = spec
+ filename = spec_without_macro
abspath = pkg_resources.resource_filename(package_name,
filename)
if not pkg_resources.resource_exists(package_name, filename):
raise ValueError(
- 'Missing template asset: %s (%s)' % (spec, abspath))
+ 'Missing template asset: %s (%s)' % (
+ spec_without_macro, abspath)
+ )
renderer = self.impl(abspath, self, macro=macro)
settings = info.settings
if not settings.get('reload_assets'):
@@ -1,4 +1,5 @@
<html>
+Outside macro
<metal:m define-macro="foo">
Hello!
</metal:m>
@@ -132,8 +132,13 @@ def test_macro_supplied(self):
result = instance.implementation()()
self.assertEqual(result, '\n Hello!\n')
-
-
+ def test_macro_notsupplied(self):
+ minimal = self._getTemplatePath('withmacro.pt')
+ lookup = DummyLookup()
+ instance = self._makeOne(minimal, lookup)
+ result = instance.implementation()()
+ self.assertEqual(result,
+ '<html>\nOutside macro\n\n Hello!\n\n</html>\n\n')
class DummyLookup(object):
auto_reload=True
@@ -295,17 +295,19 @@ def test___call__spec_notyetregistered(self):
self.assertEqual(factory.kw, {'macro':None})
def test___call__spec_withmacro(self):
+ from pyramid.interfaces import ITemplateRenderer
import os
from pyramid import tests
module_name = tests.__name__
relpath = 'fixtures/withmacro#foo.pt'
renderer = {}
factory = DummyFactory(renderer)
spec = '%s:%s' % (module_name, relpath)
+ reg = self.config.registry
info = DummyRendererInfo({
'name':spec,
'package':None,
- 'registry':self.config.registry,
+ 'registry':reg,
'settings':{},
'type':'type',
})
@@ -318,6 +320,9 @@ def test___call__spec_withmacro(self):
'withmacro.pt')
self.assertTrue(factory.path.startswith(path))
self.assertEqual(factory.kw, {'macro':'foo'})
+ self.assertTrue(
+ reg.getUtility(ITemplateRenderer, name=spec) is renderer
+ )
def test___call__reload_assets_true(self):
import pyramid.tests

0 comments on commit fa587ef

Please sign in to comment.