Permalink
Browse files

Make rendering_extension_lookup propagate up to templating layer

  • Loading branch information...
amol- committed Jun 15, 2012
1 parent 547357c commit 8d89dabd8a675c6d6e7d677588f436dab38048ee
Showing with 44 additions and 18 deletions.
  1. +15 −0 tests/test_template.py
  2. +2 −0 tw2/core/middleware.py
  3. +25 −15 tw2/core/templating.py
  4. +2 −3 tw2/core/testbase/base.py
@@ -164,3 +164,18 @@ def test_genshi_abs(self):
test_dir = os.path.sep.join(__file__.split(os.path.sep)[:-1])
fname = os.path.sep.join([test_dir, 'test.html'])
twc.Widget(template='genshi_abs:%s' % fname).display()
def test_rendering_extension_propagation(self):
mw = twc.make_middleware(None, preferred_rendering_engines=['genshi', 'jinja'],
rendering_extension_lookup={'genshi':['genshi', 'html'],
'jinja':['jinja']})
assert twc.templating.get_engine_name('tw2.core.test_templates.parent_genshi', mw) == 'genshi'
#flush caches to avoid wrong results due to cached results
twc.util.flush_memoization()
twc.templating.engine_name_cache = {}
mw = twc.make_middleware(None, preferred_rendering_engines=['genshi', 'jinja'],
rendering_extension_lookup={'genshi':['genshi'],
'jinja':['jinja', 'html']})
assert twc.templating.get_engine_name('tw2.core.test_templates.parent_genshi', mw) == 'jinja'
@@ -116,8 +116,10 @@ class Config(object):
rendering_extension_lookup = {
'mako': ['mak', 'mako'],
'genshi': ['genshi', 'html'],
'genshi_abs': ['genshi', 'html'], # just for backwards compatibility with tw2 2.0.0
'jinja':['jinja', 'html'],
'kajiki':['kajiki', 'html'],
'chameleon': ['pt']
}
script_name = ''
@@ -10,16 +10,24 @@
engine_name_cache = {}
rendering_extension_lookup = {
_default_rendering_extension_lookup = {
'mako': ['mak', 'mako'],
'genshi': ['html'],
'genshi_abs': ['html'], # just for backwards compatibility with tw2 2.0.0
'jinja': ['jinja', 'html'],
'kajiki': ['kajiki', 'html'],
'chameleon': ['pt'],
'genshi': ['genshi', 'html'],
'genshi_abs': ['genshi', 'html'], # just for backwards compatibility with tw2 2.0.0
'jinja':['jinja', 'html'],
'kajiki':['kajiki', 'html'],
'chameleon': ['pt']
}
def get_rendering_extensions_lookup(mw):
if mw is None:
rl = core.request_local()
mw = rl.get('middleware')
if mw is None:
return _default_rendering_extension_lookup
return mw.config.rendering_extension_lookup
@memoize
def get_engine_name(template_name, mw=None):
global engine_name_cache
@@ -43,7 +51,7 @@ def get_engine_name(template_name, mw=None):
# find the first file in the preffered engines available for templating
for engine_name in pref_rend_eng:
try:
get_source(engine_name, template_name)
get_source(engine_name, template_name, mw=mw)
engine_name_cache[template_name] = engine_name
return engine_name
except IOError:
@@ -53,7 +61,7 @@ def get_engine_name(template_name, mw=None):
pref_rend_eng = ['mako', 'genshi', 'jinja', 'kajiki', 'chameleon']
for engine_name in pref_rend_eng:
try:
get_source(engine_name, template_name)
get_source(engine_name, template_name, mw=mw)
engine_name_cache[template_name] = engine_name
return engine_name
except IOError:
@@ -63,8 +71,9 @@ def get_engine_name(template_name, mw=None):
@memoize
def _get_dotted_filename(engine_name, template):
template = _strip_engine_name(template)
def _get_dotted_filename(engine_name, template, mw=None):
rendering_extension_lookup = get_rendering_extensions_lookup(mw)
template = _strip_engine_name(template, mw)
location, filename = template.rsplit('.', 1)
module = __import__(location, globals(), locals(), ['*'])
parent_dir = SEP.join(module.__file__.split(SEP)[:-1])
@@ -76,23 +85,24 @@ def _get_dotted_filename(engine_name, template):
raise IOError("Couldn't find source for %r" % template)
def _strip_engine_name(template):
def _strip_engine_name(template, mw=None):
""" Strip off the leading engine name from the template if it exists. """
rendering_extension_lookup = get_rendering_extensions_lookup(mw)
if any(map(template.lstrip().startswith, rendering_extension_lookup)):
return template.split(':', 1)[1]
return template
@memoize
def get_source(engine_name, template, inline=False):
def get_source(engine_name, template, inline=False, mw=None):
if inline:
return template
if SEP in template:
filename = _strip_engine_name(template)
filename = _strip_engine_name(template, mw=mw)
else:
filename = _get_dotted_filename(engine_name, template)
filename = _get_dotted_filename(engine_name, template, mw=mw)
# TODO -- use a context manager here once we drop support for py2.5.
f = open(filename, 'r')
@@ -182,7 +192,7 @@ def render(template_name, displays_on, kwargs, inline=False, mw=None):
engine_name = inline
# Load the template source
source = get_source(engine_name, template_name, inline)
source = get_source(engine_name, template_name, inline, mw)
# Establish the render function
callback = get_render_callable(
@@ -175,8 +175,7 @@ class WidgetTest(object):
declarative = False
validate_params = None
wrap = False
engines = templating.rendering_extension_lookup.keys()
engines = templating._default_rendering_extension_lookup.keys()
def request(self, requestid, mw=None):
if mw is None:
@@ -204,7 +203,7 @@ def setup(self):
return self.request(1)
def _get_all_possible_engines(self):
for engine in templating.rendering_extension_lookup:
for engine in templating._default_rendering_extension_lookup:
yield engine
def _check_rendering_vs_expected(self, engine, attrs, params, expected):

0 comments on commit 8d89dab

Please sign in to comment.