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

Features: single lookup and debugtoolbar panel #40

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ unreleased
the ``preprocessor`` will be wrapped in a function that invokes the
preprocessor with pyramid's config settings as the second argument.

- `add_mako_renderer` now accepts a list of `extensions` instead of a single
`extension`. This allows for multiple extensions to share a single lookup.
Under the previous system, `.mako` and `.mak` would be registered to different
render factories and template lookups; if a `.mako` referenced a `.mak` file,
then it would be compiled into the `.mako` template lookup and recompiled
again for the `.mak` template lookup if accessed directly.

1.0.2 (2014-04-22)
==================

Expand Down
37 changes: 24 additions & 13 deletions pyramid_mako/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ def __call__(self, value, system):

class MakoRendererFactory(object):
lookup = None
registered_extensions = None # debugging only
renderer_factory = staticmethod(MakoLookupTemplateRenderer) # testing

def __call__(self, info):
Expand Down Expand Up @@ -238,8 +239,8 @@ def preprocessor_injector(template):
preprocessor=preprocessor,
)

def add_mako_renderer(config, extension, settings_prefix='mako.'):
""" Register a Mako renderer for a template extension.
def add_mako_renderer(config, extensions, settings_prefix='mako.'):
""" Register a Mako renderer for a template extension(s).

This function is available on the Pyramid configurator after
including the package:
Expand All @@ -250,19 +251,29 @@ def add_mako_renderer(config, extension, settings_prefix='mako.'):

The renderer will load its configuration from a prefix in the Pyramid
settings dictionary. The default prefix is 'mako.'.

Multiple extensions can be registered to a single factory and template
lookup by passing in a list or tuple for the extensions arg.

.. code-block:: python

config.add_mako_renderer(('.mako', '.mak'), settings_prefix='mako.')
"""
extensions = extensions if isinstance(extensions, (list, tuple)) else (extensions, )
renderer_factory = MakoRendererFactory()
config.add_renderer(extension, renderer_factory)
renderer_factory.registered_extensions = extensions
for ext in extensions:
config.add_renderer(ext, renderer_factory)

def register():
registry = config.registry
opts = parse_options_from_settings(
registry.settings, settings_prefix, config.maybe_dotted)
lookup = PkgResourceTemplateLookup(**opts)
if renderer_factory.lookup is None:
opts = parse_options_from_settings(
config.registry.settings, settings_prefix, config.maybe_dotted)
lookup = PkgResourceTemplateLookup(**opts)
renderer_factory.lookup = lookup

renderer_factory.lookup = lookup

config.action(('mako-renderer', extension), register)
for ext in extensions:
config.action(('mako-renderer', ext), register)

def includeme(config):
""" Set up standard configurator registrations. Use via:
Expand All @@ -278,6 +289,6 @@ def includeme(config):
:func:`~pyramid_mako.add_mako_renderer` documentation for more information.
"""
config.add_directive('add_mako_renderer', add_mako_renderer)

config.add_mako_renderer('.mako')
config.add_mako_renderer('.mak')
config.add_mako_renderer(('.mako',
'.mak',
))
1 change: 1 addition & 0 deletions pyramid_mako/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,7 @@ def test_repr_and_str(self):
self.assertEqual(str(exc), 'text')
self.assertEqual(repr(exc), 'text')


class DummyLookup(object):
directories = True

Expand Down