From d4147eb8dc4962fa86863c77fc190717113994a7 Mon Sep 17 00:00:00 2001 From: Blaise Laflamme Date: Fri, 29 Jun 2012 16:46:03 -0400 Subject: fixed mako bug #606 for inheritance and includes --- pyramid/mako_templating.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/pyramid/mako_templating.py b/pyramid/mako_templating.py index bb4ccb2f0..9aeaa9153 100644 --- a/pyramid/mako_templating.py +++ b/pyramid/mako_templating.py @@ -42,12 +42,14 @@ class PkgResourceTemplateLookup(TemplateLookup): def get_template(self, uri): """Fetch a template from the cache, or check the filesystem for it - + In addition to the basic filesystem lookup, this subclass will use pkg_resource to load a file using the asset specification syntax. - + """ + if '$' in uri: + uri = uri.replace('$', ':') isabs = os.path.isabs(uri) if (not isabs) and (':' in uri): # Windows can't cope with colons in filenames, so we replace the @@ -70,7 +72,7 @@ class PkgResourceTemplateLookup(TemplateLookup): return TemplateLookup.get_template(self, uri) -registry_lock = threading.Lock() +registry_lock = threading.Lock() class MakoRendererFactoryHelper(object): def __init__(self, settings_prefix=None): @@ -143,7 +145,7 @@ class MakoRendererFactoryHelper(object): registry_lock.acquire() try: - registry.registerUtility(lookup, IMakoLookup, + registry.registerUtility(lookup, IMakoLookup, name=settings_prefix) finally: registry_lock.release() @@ -166,15 +168,15 @@ class MakoLookupTemplateRenderer(object): """ Render a :term:`Mako` template using the template implied by the ``path`` argument.The ``path`` argument may be a package-relative path, an absolute path, or a :term:`asset - specification`. If a defname is defined, in the form of - package:path/to/template#defname.mako, a function named ``defname`` + specification`. If a defname is defined, in the form of + package:path/to/template#defname.mako, a function named ``defname`` inside the template will then be rendered. """ def __init__(self, path, defname, lookup): self.path = path self.defname = defname self.lookup = lookup - + def implementation(self): return self.lookup.get_template(self.path) -- cgit v1.2.3 From 763646c2d0ed887223d71d03a52c62679bb456fc Mon Sep 17 00:00:00 2001 From: Blaise Laflamme Date: Fri, 3 Aug 2012 00:17:02 -0400 Subject: added test for adjusted uri in mako templates --- pyramid/mako_templating.py | 3 +++ pyramid/tests/test_mako_templating.py | 32 +++++++++++++++++++++----------- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/pyramid/mako_templating.py b/pyramid/mako_templating.py index 9aeaa9153..d1ee68878 100644 --- a/pyramid/mako_templating.py +++ b/pyramid/mako_templating.py @@ -49,6 +49,9 @@ class PkgResourceTemplateLookup(TemplateLookup): """ if '$' in uri: + # Checks if the uri is already adjusted and brings it back to + # an asset spec. Normally occurs with inherited templates or + # included components. uri = uri.replace('$', ':') isabs = os.path.isabs(uri) if (not isabs) and (':' in uri): diff --git a/pyramid/tests/test_mako_templating.py b/pyramid/tests/test_mako_templating.py index 41fa9bdc4..46826d9dd 100644 --- a/pyramid/tests/test_mako_templating.py +++ b/pyramid/tests/test_mako_templating.py @@ -135,7 +135,7 @@ class Test_renderer_factory(Base, unittest.TestCase): self._callFUT(info) lookup = self._getLookup() self.assertEqual(lookup.template_args['input_encoding'], 'utf-16') - + def test_with_error_handler(self): settings = {'mako.directories':self.templates_dir, 'mako.error_handler':'pyramid.tests'} @@ -383,7 +383,7 @@ class MakoLookupTemplateRendererTests(Base, unittest.TestCase): result = instance.implementation().render_unicode() self.assertTrue(isinstance(result, text_type)) self.assertEqual(result, text_('result')) - + class TestIntegration(unittest.TestCase): def setUp(self): import pyramid.mako_templating @@ -406,7 +406,7 @@ class TestIntegration(unittest.TestCase): self.config.add_settings({'reload_templates': True}) result = render('helloworld.mak', {'a':1}).replace('\r','') self.assertEqual(result, text_('\nHello föö\n', 'utf-8')) - + def test_render_inheritance(self): from pyramid.renderers import render result = render('helloinherit.mak', {}).replace('\r','') @@ -434,7 +434,7 @@ class TestIntegration(unittest.TestCase): {'a':1}) self.assertEqual(result.ubody.replace('\r', ''), text_('\nHello föö\n', 'utf-8')) - + def test_render_with_abs_path(self): from pyramid.renderers import render result = render('/helloworld.mak', {'a':1}).replace('\r','') @@ -446,7 +446,7 @@ class TestIntegration(unittest.TestCase): self.assertEqual( result.implementation().render_unicode().replace('\r',''), text_('\nHello föö\n', 'utf-8')) - + def test_template_not_found(self): from pyramid.renderers import render from mako.exceptions import TemplateLookupException @@ -484,7 +484,7 @@ class TestPkgResourceTemplateLookup(unittest.TestCase): inst = self._makeOne(directories=[fixturedir]) result = inst.get_template('helloworld.mak') self.assertFalse(result is None) - + def test_get_template_asset_spec_with_filesystem_checks(self): inst = self._makeOne(filesystem_checks=True) result = inst.get_template('pyramid.tests:fixtures/helloworld.mak') @@ -498,7 +498,17 @@ class TestPkgResourceTemplateLookup(unittest.TestCase): self.assertFalse(result is None) finally: shutil.rmtree(tmpdir, ignore_errors=True) - + + def test_get_template_asset_spec_with_uri_adjusted(self): + inst = self._makeOne(filesystem_checks=True) + result = inst.get_template('pyramid.tests$fixtures/helloworld.mak') + self.assertFalse(result is None) + + def test_get_template_asset_spec_with_uri_not_adjusted(self): + inst = self._makeOne(filesystem_checks=True) + result = inst.get_template('pyramid.tests:fixtures/helloworld.mak') + self.assertFalse(result is None) + def test_get_template_asset_spec_missing(self): from mako.exceptions import TopLevelLookupException fixturedir = self.get_fixturedir() @@ -510,7 +520,7 @@ class TestMakoRenderingException(unittest.TestCase): def _makeOne(self, text): from pyramid.mako_templating import MakoRenderingException return MakoRenderingException(text) - + def test_repr_and_str(self): exc = self._makeOne('text') self.assertEqual(str(exc), 'text') @@ -519,7 +529,7 @@ class TestMakoRenderingException(unittest.TestCase): class DummyLookup(object): def __init__(self, exc=None): self.exc = exc - + def get_template(self, path): self.path = path return self @@ -533,8 +543,8 @@ class DummyLookup(object): raise self.exc self.values = values return text_('result') - + class DummyRendererInfo(object): def __init__(self, kw): self.__dict__.update(kw) - + -- cgit v1.2.3 From 8b26752c4e54b1c9a6f8b14b14c0236be9c239b7 Mon Sep 17 00:00:00 2001 From: Blaise Laflamme Date: Fri, 3 Aug 2012 00:48:00 -0400 Subject: added entry to changes.txt for mako fix --- CHANGES.txt | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index ecb2bf659..94553955c 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -17,6 +17,12 @@ Bug Fixes during iteration`` exception. It no longer does. See https://github.com/Pylons/pyramid/issues/635 for more information. +- In Mako Templates lookup, check if the uri is already adjusted and bring + it back to an asset spec. Normally occurs with inherited templates or + included components. + https://github.com/Pylons/pyramid/issues/606 + https://github.com/Pylons/pyramid/issues/607 + Features -------- @@ -50,9 +56,9 @@ Features and ``HTTPMovedPermanently`` exceptions, so these can be caught by the NotFound view (and other exception views). -- The mako renderer now accepts a def name and returns the template def - result for the view being called. The uri format using an asset spec is - package:path/to/template#defname.mako. The old way of returning a tuple +- The mako renderer now accepts a def name and returns the template def + result for the view being called. The uri format using an asset spec is + package:path/to/template#defname.mako. The old way of returning a tuple from the view is supported for backward compatibility, ('defname', {}). - When there is a predicate mismatch exception (seen when no view matches for -- cgit v1.2.3 From c746ba4d0893ed7f4322492fdba548f3cd2a1ac5 Mon Sep 17 00:00:00 2001 From: Wayne Witzel III Date: Fri, 3 Aug 2012 00:44:41 -0700 Subject: Adding helpful link to installing chapter from first app --- docs/narr/firstapp.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/narr/firstapp.rst b/docs/narr/firstapp.rst index 1ca188d7e..a86826d86 100644 --- a/docs/narr/firstapp.rst +++ b/docs/narr/firstapp.rst @@ -8,7 +8,8 @@ Creating Your First :app:`Pyramid` Application In this chapter, we will walk through the creation of a tiny :app:`Pyramid` application. After we're finished creating the application, we'll explain in -more detail how it works. +more detail how it works. It assumes you already have :app:`Pyramid` installed. +If you do not, head over to the :ref:`installing_chapter` section. .. _helloworld_imperative: -- cgit v1.2.3 From 41b6ebf715b9c4935aa876c879e562e767d63fc0 Mon Sep 17 00:00:00 2001 From: Wayne Witzel III Date: Fri, 3 Aug 2012 07:52:54 -0700 Subject: Removing easy_install and demoing lines from index example. firstapp has installation chapter links and having peopel using pyramid outside a virtualenv is a bad idea --- docs/index.rst | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/docs/index.rst b/docs/index.rst index 31c2fde6b..699c89449 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -13,15 +13,9 @@ Here is one of the simplest :app:`Pyramid` applications you can make: .. literalinclude:: narr/helloworld.py -When saved to ``helloworld.py``, the above application can be run via: - -.. code-block:: text - - $ easy_install pyramid - $ python helloworld.py - -When you visit ``http://localhost:8080/hello/world`` in a browser, you will -see the text ``Hello, world!``. +After you install :app:`Pyramid` and run this application. When you visit +``http://localhost:8080/hello/world`` in a browser, you will see the +text ``Hello, world!`` See :ref:`firstapp_chapter` for a full explanation of how this application works. Read the :ref:`html_narrative_documentation` to understand how -- cgit v1.2.3 From db9468a22d8a9adbb29e70eb17c8ee9612797d61 Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Fri, 3 Aug 2012 12:21:02 -0400 Subject: no period required here --- docs/index.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/index.rst b/docs/index.rst index 699c89449..c84314274 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -13,9 +13,9 @@ Here is one of the simplest :app:`Pyramid` applications you can make: .. literalinclude:: narr/helloworld.py -After you install :app:`Pyramid` and run this application. When you visit -``http://localhost:8080/hello/world`` in a browser, you will see the -text ``Hello, world!`` +After you install :app:`Pyramid` and run this application, when you visit +``http://localhost:8080/hello/world`` in a browser, you will see the text +``Hello, world!`` See :ref:`firstapp_chapter` for a full explanation of how this application works. Read the :ref:`html_narrative_documentation` to understand how -- cgit v1.2.3