Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Advertising jinja2 via entry-point to TurboGears and ToscaWidgets #109

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
61 changes: 61 additions & 0 deletions jinja2/ext/entrypoints.py
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,61 @@
# -*- coding: utf-8 -*-
"""
jinja2.ext.entrypoints
~~~~~~~~~~~~~~~~~~~~~~

These Jinja extensions advertise the Jinja2 engine by way of entry points.
It was intended for consumption by the TurboGears and ToscaWidgets
frameworks but can be reused by others.

This module was lifted from Mako ext/turbogears.py
Copyright (C) 2006-2012 the Mako authors and contributors.

:copyright: (c) 2012 by the Jinja Team.
:license: BSD.
"""

import re
import inspect

from jinja2 import Template
from jinja2 import Environment, PackageLoader


class EPPlugin(object):
""" Plugin for delivering Jinja2 as an entry point """

def __init__(self, extra_vars_func=None, options=None, extension='html'):
self.extra_vars_func = extra_vars_func
self.extension = extension
if not options:
options = {}

# Pull the options out and initialize the lookup
lookup_options = {}
for k, v in options.iteritems():
if k.startswith('jinja2.'):
lookup_options[k[7:]] = v
elif k in ['directories', 'filesystem_checks', 'module_directory']:
lookup_options[k] = v

self.lookup_options = lookup_options

def load_template(self, templatename, template_string=None):
"""Loads a template from a file or a string"""
if template_string is not None:
return Template(template_string)

toks = templatename.split('.')
package, folder, tname = '.'.join(toks[:-2]), toks[-2], toks[-1]
env = Environment(loader=PackageLoader(package, folder))
return env.get_template(tname + '.' + self.extension)

def render(self, info, format="html", fragment=False, template=None):
if isinstance(template, basestring):
template = self.load_template(template)

# Load extra vars func if provided
if self.extra_vars_func:
info.update(self.extra_vars_func())

return template.render(**info)
21 changes: 21 additions & 0 deletions jinja2/testsuite/ext.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from jinja2 import Environment, DictLoader, contextfunction, nodes from jinja2 import Environment, DictLoader, contextfunction, nodes
from jinja2.exceptions import TemplateAssertionError from jinja2.exceptions import TemplateAssertionError
from jinja2.ext import Extension from jinja2.ext import Extension
from jinja2.ext.entrypoints import EPPlugin
from jinja2.lexer import Token, count_newlines from jinja2.lexer import Token, count_newlines
from jinja2.utils import next from jinja2.utils import next


Expand Down Expand Up @@ -446,10 +447,30 @@ def test_volatile_scoping(self):
assert '<testing>\\n' in pysource assert '<testing>\\n' in pysource




class EPPluginTestCase(JinjaTestCase):

def setup(self):
self.tl = EPPlugin()

def test_basic(self):
t = self.tl.load_template('jinja2.testsuite.res.templates.test')
assert t.render() == "BAR"

def test_subdir(self):
try:
t = self.tl.load_template('jinja2.testsuite.res.templates.foo.test')
except ImportError as e:
assert str(e) == "No module named templates"

def test_string(self):
t = self.tl.load_template('foo', "hello world")
assert t.render() == "hello world"

def suite(): def suite():
suite = unittest.TestSuite() suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(ExtensionsTestCase)) suite.addTest(unittest.makeSuite(ExtensionsTestCase))
suite.addTest(unittest.makeSuite(InternationalizationTestCase)) suite.addTest(unittest.makeSuite(InternationalizationTestCase))
suite.addTest(unittest.makeSuite(NewstyleInternationalizationTestCase)) suite.addTest(unittest.makeSuite(NewstyleInternationalizationTestCase))
suite.addTest(unittest.makeSuite(AutoEscapeTestCase)) suite.addTest(unittest.makeSuite(AutoEscapeTestCase))
suite.addTest(unittest.makeSuite(EPPluginTestCase))
return suite return suite
3 changes: 3 additions & 0 deletions setup.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@
entry_points=""" entry_points="""
[babel.extractors] [babel.extractors]
jinja2 = jinja2.ext:babel_extract[i18n] jinja2 = jinja2.ext:babel_extract[i18n]

[python.templating.engines]
jinja2 = jinja2.ext.entrypoints:EPPlugin
""", """,
features={'debugsupport': debugsupport}, features={'debugsupport': debugsupport},
**extra **extra
Expand Down