Skip to content

Commit

Permalink
tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
jvanasco committed Nov 28, 2016
1 parent 6b60165 commit eb5d6c2
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 20 deletions.
11 changes: 11 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ unreleased
the ``preprocessor`` will be wrapped in a function that invokes the
preprocessor with pyramid's config settings as the second argument.

- Added a debugtoolbar panel for debugging use
debugtoolbar.panels =
pyramid_mako.debugtoolbar.panels.main.PyramidMakoMainDebugPanel

- `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
39 changes: 24 additions & 15 deletions pyramid_mako/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def __call__(self, value, system):

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

def __call__(self, info):
Expand Down Expand Up @@ -239,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 @@ -251,20 +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
renderer_factory.registered_ext = extension

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 @@ -280,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',
))
10 changes: 5 additions & 5 deletions pyramid_mako/debugtoolbar/panels/templates/main.dbtmako
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
% for renderer in mako_renderers:
<table class="table table-striped table-condensed">
<tr>
<th>renderer</th>
<td>${renderer}</td>
<th>renderer extensions</th>
<td><code>${renderer.registered_extensions}</code></td>
</tr>
<tr>
<th>renderer extension</th>
<td><code>${renderer.registered_ext}</code></td>
<th>renderer</th>
<td>${renderer}, ${id(renderer)}</td>
</tr>
<tr>
<th>lookup</th>
<td>${renderer.lookup}</td>
<td>${renderer.lookup}, ${id(renderer.lookup)}</td>
## <td>${renderer.lookup.__dict__}</td>
</tr>
<tr>
Expand Down
27 changes: 27 additions & 0 deletions pyramid_mako/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,33 @@ def test_repr_and_str(self):
self.assertEqual(str(exc), 'text')
self.assertEqual(repr(exc), 'text')

class TestDebugtoolbarPanel(Base, unittest.TestCase):

def test_panel_null(self):
# we shouldn't have any mako_renderers configured... but the panel should run
from pyramid_mako.debugtoolbar.panels.main import PyramidMakoMainDebugPanel
from pyramid.testing import DummyRequest
panel = PyramidMakoMainDebugPanel(DummyRequest())
self.assertTrue('mako_renderers' in panel.data)
self.assertTrue(len(panel.data['mako_renderers']) == 0)

def test_panel_full(self):
# we should have a single renders for .mako+.mak
from pyramid_mako.debugtoolbar.panels.main import PyramidMakoMainDebugPanel
from pyramid.testing import DummyRequest
self.config = testing.setUp()
self.settings = {'mako.directories': 'pyramid_mako.tests:fixtures',
'mako.preprocessor':
'pyramid_mako.tests.dummy_mako_preprocessor',
'mako.preprocessor_wants_settings': 'true',
'replace_Hello': 'Goodbye',
}
self.config.add_settings(self.settings)
self.config.include('pyramid_mako')
panel = PyramidMakoMainDebugPanel(DummyRequest())
self.assertTrue('mako_renderers' in panel.data)
self.assertTrue(len(panel.data['mako_renderers']) == 1)

class DummyLookup(object):
directories = True

Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
testing_extras = tests_require + [
'nose',
'coverage',
'pyramid_debugtoolbar',
]

requires = [
Expand Down

0 comments on commit eb5d6c2

Please sign in to comment.