Skip to content
Browse files

initial unlacing chameleon

  • Loading branch information...
1 parent a8b2df6 commit 7e75f116d268a7e03fe0e7fa8838aa16032a676f @reedobrien reedobrien committed Mar 12, 2012
View
144 pyramid/chameleon_text.py
@@ -1,144 +0,0 @@
-import sys
-
-from zope.deprecation import deprecated
-from zope.interface import implementer
-
-from pyramid.compat import reraise
-
-try:
- from chameleon.zpt.template import PageTextTemplateFile
- # prevent pyflakes complaining about a redefinition below
- PageTextTemplateFile
-except ImportError: # pragma: no cover
- exc_class, exc, tb = sys.exc_info()
- # Chameleon doesn't work on non-CPython platforms
- class PageTextTemplateFile(object):
- def __init__(self, *arg, **kw):
- reraise(ImportError, exc, tb)
-
-from pyramid.interfaces import ITemplateRenderer
-
-from pyramid.decorator import reify
-from pyramid import renderers
-from pyramid.path import caller_package
-
-def renderer_factory(info):
- return renderers.template_renderer_factory(info, TextTemplateRenderer)
-
-@implementer(ITemplateRenderer)
-class TextTemplateRenderer(object):
- def __init__(self, path, lookup):
- self.path = path
- self.lookup = lookup
-
- @reify # avoid looking up reload_templates before manager pushed
- def template(self):
- if sys.platform.startswith('java'): # pragma: no cover
- raise RuntimeError(
- 'Chameleon templates are not compatible with Jython')
- return PageTextTemplateFile(self.path,
- auto_reload=self.lookup.auto_reload,
- debug=self.lookup.debug,
- translate=self.lookup.translate)
-
- def implementation(self):
- return self.template
-
- def __call__(self, value, system):
- try:
- system.update(value)
- except (TypeError, ValueError):
- raise ValueError('renderer was passed non-dictionary as value')
- result = self.template(**system)
- return result
-
-def get_renderer(path):
- """ Return a callable object which can be used to render a
- :term:`Chameleon` text 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`.
-
- .. warning::
-
- This API is deprecated in :app:`Pyramid` 1.0. Use
- :func:`pyramid.renderers.get_renderer` instead.
- """
- package = caller_package()
- factory = renderers.RendererHelper(path, package=package)
- return factory.get_renderer()
-
-deprecated(
- 'get_renderer',
- '(pyramid.chameleon_text.get_renderer is deprecated '
- 'as of Pyramid 1.0; instead use pyramid.renderers.get_renderer)')
-
-def get_template(path):
- """ Return the underyling object representing a :term:`Chameleon`
- text 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`.
-
- .. warning::
-
- This API is deprecated in :app:`Pyramid` 1.0. Use
- the ``implementation()`` method of a template renderer retrieved via
- :func:`pyramid.renderers.get_renderer` instead.
- """
- package = caller_package()
- factory = renderers.RendererHelper(path, package=package)
- return factory.get_renderer().implementation()
-
-deprecated(
- 'get_template',
- '(pyramid.chameleon_text.get_template is deprecated '
- 'as of Pyramid 1.0; instead use '
- 'pyramid.renderers.get_renderer().implementation())')
-
-def render_template(path, **kw):
- """ Render a :term:`Chameleon` text 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`. The arguments in ``*kw`` are passed as top-level
- names to the template, and so may be used within the template
- itself. Returns a string.
-
- .. warning::
-
- This API is deprecated in :app:`Pyramid` 1.0. Use
- :func:`pyramid.renderers.render` instead.
- """
- package = caller_package()
- request = kw.pop('request', None)
- renderer = renderers.RendererHelper(path, package=package)
- return renderer.render(kw, None, request=request)
-
-deprecated(
- 'render_template',
- '(pyramid.chameleon_text.render_template is deprecated '
- 'as of Pyramid 1.0; instead use pyramid.renderers.render)')
-
-def render_template_to_response(path, **kw):
- """ Render a :term:`Chameleon` text 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`. The arguments in ``*kw`` are passed as top-level
- names to the template, and so may be used within the template
- itself. Returns a :term:`Response` object with the body as the
- template result.
-
- .. warning::
-
- This API is deprecated in :app:`Pyramid` 1.0. Use
- :func:`pyramid.renderers.render_to_response` instead.
- """
- package = caller_package()
- request = kw.pop('request', None)
- renderer = renderers.RendererHelper(path, package=package)
- return renderer.render_to_response(kw, None, request=request)
-
-deprecated(
- 'render_template_to_response',
- '(pyramid.chameleon_text.render_template_to_response is deprecated '
- 'as of Pyramid 1.0; instead use pyramid.renderers.render_to_response)')
-
View
142 pyramid/chameleon_zpt.py
@@ -1,142 +0,0 @@
-import sys
-
-from zope.deprecation import deprecated
-from zope.interface import implementer
-
-from pyramid.compat import reraise
-
-try:
- from chameleon.zpt.template import PageTemplateFile
- PageTemplateFile # prevent pyflakes complaining about a redefinition below
-except ImportError: # pragma: no cover
- exc_class, exc, tb = sys.exc_info()
- # Chameleon doesn't work on non-CPython platforms
- class PageTemplateFile(object):
- def __init__(self, *arg, **kw):
- reraise(ImportError, exc, tb)
-
-from pyramid.interfaces import ITemplateRenderer
-
-from pyramid.decorator import reify
-from pyramid.path import caller_package
-from pyramid import renderers
-
-def renderer_factory(info):
- return renderers.template_renderer_factory(info, ZPTTemplateRenderer)
-
-@implementer(ITemplateRenderer)
-class ZPTTemplateRenderer(object):
- def __init__(self, path, lookup):
- self.path = path
- self.lookup = lookup
-
- @reify # avoid looking up reload_templates before manager pushed
- def template(self):
- if sys.platform.startswith('java'): # pragma: no cover
- raise RuntimeError(
- 'Chameleon templates are not compatible with Jython')
- return PageTemplateFile(self.path,
- auto_reload=self.lookup.auto_reload,
- debug=self.lookup.debug,
- translate=self.lookup.translate)
-
- def implementation(self):
- return self.template
-
- def __call__(self, value, system):
- try:
- system.update(value)
- except (TypeError, ValueError):
- raise ValueError('renderer was passed non-dictionary as value')
- result = self.template(**system)
- return result
-
-def get_renderer(path):
- """ Return a callable object which can be used to render a
- :term:`Chameleon` ZPT 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`.
-
- .. warning::
-
- This API is deprecated in :app:`Pyramid` 1.0. Use
- :func:`pyramid.renderers.get_renderer` instead.
- """
- package = caller_package()
- factory = renderers.RendererHelper(name=path, package=package)
- return factory.get_renderer()
-
-deprecated(
- 'get_renderer',
- '(pyramid.chameleon_zpt.get_renderer is deprecated '
- 'as of Pyramid 1.0; instead use pyramid.renderers.get_renderer)')
-
-def get_template(path):
- """ Return the underyling object representing a :term:`Chameleon`
- ZPT 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`.
-
- .. warning::
-
- This API is deprecated in :app:`Pyramid` 1.0. Use
- the ``implementation()`` method of a template renderer retrieved via
- :func:`pyramid.renderers.get_renderer` instead.
- """
- package = caller_package()
- factory = renderers.RendererHelper(name=path, package=package)
- return factory.get_renderer().implementation()
-
-deprecated(
- 'get_template',
- '(pyramid.chameleon_zpt.get_template is deprecated '
- 'as of Pyramid 1.0; instead use '
- 'pyramid.renderers.get_renderer().implementation())')
-
-def render_template(path, **kw):
- """ Render a :term:`Chameleon` ZPT 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`. The arguments in ``*kw`` are passed as top-level
- names to the template, and so may be used within the template
- itself. Returns a string.
-
- .. warning::
-
- This API is deprecated in :app:`Pyramid` 1.0. Use
- :func:`pyramid.renderers.render` instead.
- """
- package = caller_package()
- request = kw.pop('request', None)
- renderer = renderers.RendererHelper(name=path, package=package)
- return renderer.render(kw, None, request=request)
-
-deprecated(
- 'render_template',
- '(pyramid.chameleon_zpt.render_template is deprecated as of Pyramid 1.0; '
- 'instead use pyramid.renderers.render)')
-
-def render_template_to_response(path, **kw):
- """ Render a :term:`Chameleon` ZPT 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`. The arguments in ``*kw`` are passed as top-level
- names to the template, and so may be used within the template
- itself. Returns a :term:`Response` object with the body as the
- template result.
-
- .. warning::
-
- This API is deprecated in :app:`Pyramid` 1.0. Use
- :func:`pyramid.renderers.render_to_response` instead.
- """
- package = caller_package()
- request = kw.pop('request', None)
- renderer = renderers.RendererHelper(name=path, package=package)
- return renderer.render_to_response(kw, None, request=request)
-
-deprecated(
- 'render_template_to_response',
- '(pyramid.chameleon_zpt.render_template_to_response is deprecated; as of '
- 'Pyramid 1.0, instead use pyramid.renderers.render_to_response)')
View
7 pyramid/config/i18n.py
@@ -1,10 +1,7 @@
import os
import sys
-from translationstring import ChameleonTranslate
-
from pyramid.interfaces import (
- IChameleonTranslate,
ILocaleNegotiator,
ITranslationDirectories,
)
@@ -114,8 +111,8 @@ def register():
# utility to be registered zero or one times. We register the
# same function once for each added translation directory,
# which does too much work, but has the same effect.
- ctranslate = ChameleonTranslate(translator)
- self.registry.registerUtility(ctranslate, IChameleonTranslate)
+ pass
+ ### ???: What is this for now without Chameleon?
self.action(None, register, introspectables=introspectables)
View
10 pyramid/config/rendering.py
@@ -8,17 +8,11 @@
from pyramid.config.util import action_method
-from pyramid import (
- renderers,
- chameleon_text,
- chameleon_zpt,
- )
+from pyramid import renderers
DEFAULT_RENDERERS = (
- ('.txt', chameleon_text.renderer_factory),
- ('.pt', chameleon_zpt.renderer_factory),
('json', renderers.json_renderer_factory),
- ('string', renderers.string_renderer_factory),
+ ('string', renderers.string_renderer_factory)
)
class RenderingConfiguratorMixin(object):
View
2 pyramid/fixers/fix_bfg_imports.py
@@ -15,7 +15,7 @@
'configuration',
'authentication',
'authorization',
- 'chameleon_text',
+ 'chameleon_text', ## ???: leave in for BFG to pyramid < 1.4 ?
'chameleon_zpt',
'decorator',
'encode',
View
12 pyramid/interfaces.py
@@ -798,18 +798,6 @@ class IPackageOverrides(Interface):
# traversalwrapper)
VH_ROOT_KEY = 'HTTP_X_VHM_ROOT'
-class IChameleonLookup(Interface):
- translate = Attribute('IChameleonTranslate object')
- debug = Attribute('The ``debug_templates`` setting for this application')
- auto_reload = Attribute('The ``reload_templates`` setting for this app')
- def __call__(self, info):
- """ Return an ITemplateRenderer based on IRendererInfo ``info`` """
-
-class IChameleonTranslate(Interface):
- """ Internal interface representing a chameleon translate function """
- def __call__(msgid, domain=None, mapping=None, context=None,
- target_language=None, default=None):
- """ Translate a mess of arguments to a Unicode object """
class ILocalizer(Interface):
""" Localizer for a specific language """
View
126 pyramid/renderers.py
@@ -1,22 +1,15 @@
import json
import os
-import pkg_resources
-import threading
from zope.interface import implementer
from pyramid.interfaces import (
- IChameleonLookup,
- IChameleonTranslate,
IRendererGlobalsFactory,
IRendererFactory,
IResponseFactory,
- ITemplateRenderer,
IRendererInfo,
)
-from pyramid.asset import asset_spec_from_abspath
-
from pyramid.compat import (
string_types,
text_type,
@@ -26,14 +19,13 @@
from pyramid.events import BeforeRender
-from pyramid.path import (
- caller_package,
- package_path,
- )
+from pyramid.path import caller_package
+
from pyramid.response import Response
from pyramid.threadlocal import get_current_registry
+
# API
def render(renderer_name, value, request=None, package=None):
@@ -237,118 +229,6 @@ def _render(value, system):
# utility functions, not API
-@implementer(IChameleonLookup)
-class ChameleonRendererLookup(object):
- def __init__(self, impl, registry):
- self.impl = impl
- self.registry = registry
- self.lock = threading.Lock()
-
- def get_spec(self, name, package):
- if not package:
- # if there's no package, we can't do any conversion
- return name
-
- spec = name
- isabspath = os.path.isabs(name)
- colon_in_name = ':' in name
- isabsspec = colon_in_name and (not isabspath)
- isrelspec = (not isabsspec) and (not isabspath)
-
- # if it's already an absolute spec, we don't need to do anything,
- # but if it's a relative spec or an absolute path, we need to try
- # to convert it to an absolute spec
-
- if isrelspec:
- # convert relative asset spec to absolute asset spec
- pp = package_path(package)
- spec = os.path.join(pp, spec)
- spec = asset_spec_from_abspath(spec, package)
-
- elif isabspath:
- # convert absolute path to absolute asset spec
- spec = asset_spec_from_abspath(spec, package)
-
- return spec
-
- @property # wait until completely necessary to look up translator
- def translate(self):
- return self.registry.queryUtility(IChameleonTranslate)
-
- @property # wait until completely necessary to look up debug_templates
- def debug(self):
- settings = self.registry.settings
- if settings is None:
- return False
- return settings.get('debug_templates', False)
-
- @property # wait until completely necessary to look up reload_templates
- def auto_reload(self):
- settings = self.registry.settings
- if settings is None:
- return False
- return settings.get('reload_templates', False)
-
- def __call__(self, info):
- spec = self.get_spec(info.name, info.package)
- registry = info.registry
-
- if os.path.isabs(spec):
- # 'spec' is an absolute filename
- if not os.path.exists(spec):
- raise ValueError('Missing template file: %s' % spec)
- renderer = registry.queryUtility(ITemplateRenderer, name=spec)
- if renderer is None:
- renderer = self.impl(spec, self)
- # cache the template
- try:
- self.lock.acquire()
- registry.registerUtility(renderer,
- ITemplateRenderer, name=spec)
- finally:
- self.lock.release()
- else:
- # spec is a package:relpath asset spec
- renderer = registry.queryUtility(ITemplateRenderer, name=spec)
- if renderer is None:
- try:
- package_name, filename = spec.split(':', 1)
- except ValueError: # pragma: no cover
- # somehow we were passed a relative pathname; this
- # should die
- package_name = caller_package(4).__name__
- filename = spec
- abspath = pkg_resources.resource_filename(package_name,
- filename)
- if not pkg_resources.resource_exists(package_name, filename):
- raise ValueError(
- 'Missing template asset: %s (%s)' % (spec, abspath))
- renderer = self.impl(abspath, self)
- settings = info.settings
- if not settings.get('reload_assets'):
- # cache the template
- self.lock.acquire()
- try:
- registry.registerUtility(renderer, ITemplateRenderer,
- name=spec)
- finally:
- self.lock.release()
-
- return renderer
-
-registry_lock = threading.Lock()
-
-def template_renderer_factory(info, impl, lock=registry_lock):
- registry = info.registry
- lookup = registry.queryUtility(IChameleonLookup, name=info.type)
- if lookup is None:
- lookup = ChameleonRendererLookup(impl, registry)
- lock.acquire()
- try:
- registry.registerUtility(lookup, IChameleonLookup, name=info.type)
- finally:
- lock.release()
- return lookup(info)
@implementer(IRendererInfo)
class RendererHelper(object):
View
209 pyramid/tests/test_chameleon_text.py
@@ -1,209 +0,0 @@
-import unittest
-
-from pyramid.compat import binary_type
-from pyramid.testing import skip_on
-from pyramid import testing
-
-class Base:
- def setUp(self):
- self.config = testing.setUp()
- from zope.deprecation import __show__
- __show__.off()
-
- def tearDown(self):
- testing.tearDown()
- from zope.deprecation import __show__
- __show__.on()
-
- def _getTemplatePath(self, name):
- import os
- here = os.path.abspath(os.path.dirname(__file__))
- return os.path.join(here, 'fixtures', name)
-
- def _registerUtility(self, utility, iface, name=''):
- reg = self.config.registry
- reg.registerUtility(utility, iface, name=name)
-
-class TextTemplateRendererTests(Base, unittest.TestCase):
- def _getTargetClass(self):
- from pyramid.chameleon_text import TextTemplateRenderer
- return TextTemplateRenderer
-
- def _makeOne(self, *arg, **kw):
- klass = self._getTargetClass()
- return klass(*arg, **kw)
-
- def test_instance_implements_ITemplate(self):
- from zope.interface.verify import verifyObject
- from pyramid.interfaces import ITemplateRenderer
- path = self._getTemplatePath('minimal.txt')
- lookup = DummyLookup()
- verifyObject(ITemplateRenderer, self._makeOne(path, lookup))
-
- def test_class_implements_ITemplate(self):
- from zope.interface.verify import verifyClass
- from pyramid.interfaces import ITemplateRenderer
- verifyClass(ITemplateRenderer, self._getTargetClass())
-
- @skip_on('java')
- def test_template_reified(self):
- minimal = self._getTemplatePath('minimal.txt')
- lookup = DummyLookup()
- instance = self._makeOne(minimal, lookup)
- self.assertFalse('template' in instance.__dict__)
- template = instance.template
- self.assertEqual(template, instance.__dict__['template'])
-
- @skip_on('java')
- def test_template_with_ichameleon_translate(self):
- minimal = self._getTemplatePath('minimal.txt')
- lookup = DummyLookup()
- instance = self._makeOne(minimal, lookup)
- self.assertFalse('template' in instance.__dict__)
- template = instance.template
- self.assertEqual(template.translate, lookup.translate)
-
- @skip_on('java')
- def test_template_with_debug_templates(self):
- minimal = self._getTemplatePath('minimal.txt')
- lookup = DummyLookup()
- lookup.debug = True
- instance = self._makeOne(minimal, lookup)
- self.assertFalse('template' in instance.__dict__)
- template = instance.template
- self.assertEqual(template.debug, True)
-
- @skip_on('java')
- def test_template_with_reload_templates(self):
- minimal = self._getTemplatePath('minimal.txt')
- lookup = DummyLookup()
- lookup.auto_reload = True
- instance = self._makeOne(minimal, lookup)
- self.assertFalse('template' in instance.__dict__)
- template = instance.template
- self.assertEqual(template.auto_reload, True)
-
- @skip_on('java')
- def test_template_without_reload_templates(self):
- minimal = self._getTemplatePath('minimal.txt')
- lookup = DummyLookup()
- lookup.auto_reload = False
- instance = self._makeOne(minimal, lookup)
- self.assertFalse('template' in instance.__dict__)
- template = instance.template
- self.assertEqual(template.auto_reload, False)
-
- @skip_on('java')
- def test_call(self):
- minimal = self._getTemplatePath('minimal.txt')
- lookup = DummyLookup()
- instance = self._makeOne(minimal, lookup)
- result = instance({}, {})
- self.assertTrue(isinstance(result, binary_type))
- self.assertEqual(result, b'Hello.\n')
-
- @skip_on('java')
- def test_call_with_nondict_value(self):
- minimal = self._getTemplatePath('minimal.txt')
- lookup = DummyLookup()
- instance = self._makeOne(minimal, lookup)
- self.assertRaises(ValueError, instance, None, {})
-
- @skip_on('java')
- def test_call_nonminimal(self):
- nonminimal = self._getTemplatePath('nonminimal.txt')
- lookup = DummyLookup()
- instance = self._makeOne(nonminimal, lookup)
- result = instance({'name':'Chris'}, {})
- self.assertTrue(isinstance(result, binary_type))
- self.assertEqual(result, b'Hello, Chris!\n')
-
- @skip_on('java')
- def test_implementation(self):
- minimal = self._getTemplatePath('minimal.txt')
- lookup = DummyLookup()
- instance = self._makeOne(minimal, lookup)
- result = instance.implementation()()
- self.assertTrue(isinstance(result, binary_type))
- self.assertEqual(result, b'Hello.\n')
-
-class RenderTemplateTests(Base, unittest.TestCase):
- def _callFUT(self, name, **kw):
- from pyramid.chameleon_text import render_template
- return render_template(name, **kw)
-
- @skip_on('java')
- def test_it(self):
- minimal = self._getTemplatePath('minimal.txt')
- result = self._callFUT(minimal)
- self.assertTrue(isinstance(result, binary_type))
- self.assertEqual(result, b'Hello.\n')
-
-class RenderTemplateToResponseTests(Base, unittest.TestCase):
- def _callFUT(self, name, **kw):
- from pyramid.chameleon_text import render_template_to_response
- return render_template_to_response(name, **kw)
-
- @skip_on('java')
- def test_minimal(self):
- minimal = self._getTemplatePath('minimal.txt')
- result = self._callFUT(minimal)
- from webob import Response
- self.assertTrue(isinstance(result, Response))
- self.assertEqual(result.app_iter, [b'Hello.\n'])
- self.assertEqual(result.status, '200 OK')
- self.assertEqual(len(result.headerlist), 2)
-
- @skip_on('java')
- def test_iresponsefactory_override(self):
- from webob import Response
- class Response2(Response):
- pass
- from pyramid.interfaces import IResponseFactory
- self._registerUtility(Response2, IResponseFactory)
- minimal = self._getTemplatePath('minimal.txt')
- result = self._callFUT(minimal)
- self.assertTrue(isinstance(result, Response2))
-
-class GetRendererTests(Base, unittest.TestCase):
- def _callFUT(self, name):
- from pyramid.chameleon_text import get_renderer
- return get_renderer(name)
-
- @skip_on('java')
- def test_it(self):
- from pyramid.interfaces import IRendererFactory
- class Dummy:
- template = object()
- def implementation(self): pass
- renderer = Dummy()
- def rf(spec):
- return renderer
- self._registerUtility(rf, IRendererFactory, name='foo')
- result = self._callFUT('foo')
- self.assertTrue(result is renderer)
-
-class GetTemplateTests(Base, unittest.TestCase):
- def _callFUT(self, name):
- from pyramid.chameleon_text import get_template
- return get_template(name)
-
- @skip_on('java')
- def test_it(self):
- from pyramid.interfaces import IRendererFactory
- class Dummy:
- template = object()
- def implementation(self):
- return self.template
- renderer = Dummy()
- def rf(spec):
- return renderer
- self._registerUtility(rf, IRendererFactory, name='foo')
- result = self._callFUT('foo')
- self.assertTrue(result is renderer.template)
-
-class DummyLookup(object):
- auto_reload=True
- debug = True
- def translate(self, msg): pass
-
View
215 pyramid/tests/test_chameleon_zpt.py
@@ -1,215 +0,0 @@
-import unittest
-
-from pyramid.testing import skip_on
-from pyramid import testing
-from pyramid.compat import text_type
-
-class Base(object):
- def setUp(self):
- self.config = testing.setUp()
- from zope.deprecation import __show__
- __show__.off()
-
- def tearDown(self):
- testing.tearDown()
- from zope.deprecation import __show__
- __show__.on()
-
- def _getTemplatePath(self, name):
- import os
- here = os.path.abspath(os.path.dirname(__file__))
- return os.path.join(here, 'fixtures', name)
-
- def _registerUtility(self, utility, iface, name=''):
- reg = self.config.registry
- reg.registerUtility(utility, iface, name=name)
- return reg
-
-class ZPTTemplateRendererTests(Base, unittest.TestCase):
- def _getTargetClass(self):
- from pyramid.chameleon_zpt import ZPTTemplateRenderer
- return ZPTTemplateRenderer
-
- def _makeOne(self, *arg, **kw):
- klass = self._getTargetClass()
- return klass(*arg, **kw)
-
- def test_instance_implements_ITemplate(self):
- from zope.interface.verify import verifyObject
- from pyramid.interfaces import ITemplateRenderer
- path = self._getTemplatePath('minimal.pt')
- lookup = DummyLookup()
- verifyObject(ITemplateRenderer, self._makeOne(path, lookup))
-
- def test_class_implements_ITemplate(self):
- from zope.interface.verify import verifyClass
- from pyramid.interfaces import ITemplateRenderer
- verifyClass(ITemplateRenderer, self._getTargetClass())
-
- @skip_on('java')
- def test_call(self):
- minimal = self._getTemplatePath('minimal.pt')
- lookup = DummyLookup()
- instance = self._makeOne(minimal, lookup)
- result = instance({}, {})
- self.assertTrue(isinstance(result, text_type))
- self.assertEqual(result.rstrip('\n'),
- '<div xmlns="http://www.w3.org/1999/xhtml">\n</div>')
-
- @skip_on('java')
- def test_template_reified(self):
- minimal = self._getTemplatePath('minimal.pt')
- lookup = DummyLookup()
- instance = self._makeOne(minimal, lookup)
- self.assertFalse('template' in instance.__dict__)
- template = instance.template
- self.assertEqual(template, instance.__dict__['template'])
-
- @skip_on('java')
- def test_template_with_ichameleon_translate(self):
- minimal = self._getTemplatePath('minimal.pt')
- lookup = DummyLookup()
- instance = self._makeOne(minimal, lookup)
- self.assertFalse('template' in instance.__dict__)
- template = instance.template
- self.assertEqual(template.translate, lookup.translate)
-
- @skip_on('java')
- def test_template_with_debug_templates(self):
- minimal = self._getTemplatePath('minimal.pt')
- lookup = DummyLookup()
- lookup.debug = True
- instance = self._makeOne(minimal, lookup)
- self.assertFalse('template' in instance.__dict__)
- template = instance.template
- self.assertEqual(template.debug, True)
-
- @skip_on('java')
- def test_template_without_debug_templates(self):
- minimal = self._getTemplatePath('minimal.pt')
- lookup = DummyLookup()
- lookup.debug = False
- instance = self._makeOne(minimal, lookup)
- self.assertFalse('template' in instance.__dict__)
- template = instance.template
- self.assertEqual(template.debug, False)
-
- @skip_on('java')
- def test_template_with_reload_templates(self):
- minimal = self._getTemplatePath('minimal.pt')
- lookup = DummyLookup()
- lookup.auto_reload = True
- instance = self._makeOne(minimal, lookup)
- self.assertFalse('template' in instance.__dict__)
- template = instance.template
- self.assertEqual(template.auto_reload, True)
-
- @skip_on('java')
- def test_template_without_reload_templates(self):
- minimal = self._getTemplatePath('minimal.pt')
- lookup = DummyLookup()
- lookup.auto_reload = False
- instance = self._makeOne(minimal, lookup)
- self.assertFalse('template' in instance.__dict__)
- template = instance.template
- self.assertEqual(template.auto_reload, False)
-
- @skip_on('java')
- def test_call_with_nondict_value(self):
- minimal = self._getTemplatePath('minimal.pt')
- lookup = DummyLookup()
- instance = self._makeOne(minimal, lookup)
- self.assertRaises(ValueError, instance, None, {})
-
- @skip_on('java')
- def test_implementation(self):
- minimal = self._getTemplatePath('minimal.pt')
- lookup = DummyLookup()
- instance = self._makeOne(minimal, lookup)
- result = instance.implementation()()
- self.assertTrue(isinstance(result, text_type))
- self.assertEqual(result.rstrip('\n'),
- '<div xmlns="http://www.w3.org/1999/xhtml">\n</div>')
-
-
-class RenderTemplateTests(Base, unittest.TestCase):
- def _callFUT(self, name, **kw):
- from pyramid.chameleon_zpt import render_template
- return render_template(name, **kw)
-
- @skip_on('java')
- def test_it(self):
- minimal = self._getTemplatePath('minimal.pt')
- result = self._callFUT(minimal)
- self.assertTrue(isinstance(result, text_type))
- self.assertEqual(result.rstrip('\n'),
- '<div xmlns="http://www.w3.org/1999/xhtml">\n</div>')
-
-class RenderTemplateToResponseTests(Base, unittest.TestCase):
- def _callFUT(self, name, **kw):
- from pyramid.chameleon_zpt import render_template_to_response
- return render_template_to_response(name, **kw)
-
- @skip_on('java')
- def test_it(self):
- minimal = self._getTemplatePath('minimal.pt')
- result = self._callFUT(minimal)
- from webob import Response
- self.assertTrue(isinstance(result, Response))
- self.assertEqual(result.app_iter[0].rstrip(b'\n'),
- b'<div xmlns="http://www.w3.org/1999/xhtml">\n</div>')
- self.assertEqual(result.status, '200 OK')
- self.assertEqual(len(result.headerlist), 2)
-
- @skip_on('java')
- def test_iresponsefactory_override(self):
- from webob import Response
- class Response2(Response):
- pass
- from pyramid.interfaces import IResponseFactory
- self._registerUtility(Response2, IResponseFactory)
- minimal = self._getTemplatePath('minimal.pt')
- result = self._callFUT(minimal)
- self.assertTrue(isinstance(result, Response2))
-
-class GetRendererTests(Base, unittest.TestCase):
- def _callFUT(self, name):
- from pyramid.chameleon_zpt import get_renderer
- return get_renderer(name)
-
- @skip_on('java')
- def test_it(self):
- from pyramid.interfaces import IRendererFactory
- class Dummy:
- template = object()
- def implementation(self): pass
- renderer = Dummy()
- def rf(spec):
- return renderer
- self._registerUtility(rf, IRendererFactory, name='foo')
- result = self._callFUT('foo')
- self.assertTrue(result is renderer)
-
-class GetTemplateTests(Base, unittest.TestCase):
- def _callFUT(self, name):
- from pyramid.chameleon_zpt import get_template
- return get_template(name)
-
- @skip_on('java')
- def test_it(self):
- from pyramid.interfaces import IRendererFactory
- class Dummy:
- template = object()
- def implementation(self):
- return self.template
- renderer = Dummy()
- def rf(spec):
- return renderer
- self._registerUtility(rf, IRendererFactory, name='foo')
- result = self._callFUT('foo')
- self.assertTrue(result is renderer.template)
-
-class DummyLookup(object):
- auto_reload=True
- debug = True
- def translate(self, msg): pass
View
335 pyramid/tests/test_renderers.py
@@ -4,341 +4,6 @@
from pyramid import testing
from pyramid.compat import text_
-class TestTemplateRendererFactory(unittest.TestCase):
- def setUp(self):
- self.config = cleanUp()
-
- def tearDown(self):
- cleanUp()
-
- def _callFUT(self, info, impl):
- from pyramid.renderers import template_renderer_factory
- return template_renderer_factory(info, impl)
-
- def test_lookup_found(self):
- from pyramid.interfaces import IChameleonLookup
- L = []
- def dummy(info):
- L.append(info)
- return True
- self.config.registry.registerUtility(dummy, IChameleonLookup,
- name='abc')
- class DummyInfo(object):
- pass
- info = DummyInfo()
- info.registry = self.config.registry
- info.type = 'abc'
- result = self._callFUT(info, None)
- self.assertEqual(result, True)
- self.assertEqual(L, [info])
-
- def test_lookup_miss(self):
- from pyramid.interfaces import ITemplateRenderer
- import os
- abspath = os.path.abspath(__file__)
- renderer = {}
- self.config.registry.registerUtility(
- renderer, ITemplateRenderer, name=abspath)
- info = DummyRendererInfo({
- 'name':abspath,
- 'package':None,
- 'registry':self.config.registry,
- 'settings':{},
- 'type':'type',
- })
- result = self._callFUT(info, None)
- self.assertTrue(result is renderer)
-
-class TestChameleonRendererLookup(unittest.TestCase):
- def setUp(self):
- self.config = testing.setUp()
-
- def tearDown(self):
- testing.tearDown()
-
- def _makeOne(self, impl):
- from pyramid.renderers import ChameleonRendererLookup
- return ChameleonRendererLookup(impl, self.config.registry)
-
- def _registerTemplateRenderer(self, renderer, name):
- from pyramid.interfaces import ITemplateRenderer
- self.config.registry.registerUtility(
- renderer, ITemplateRenderer, name=name)
-
- def test_get_spec_not_abspath_no_colon_no_package(self):
- lookup = self._makeOne(None)
- result = lookup.get_spec('foo', None)
- self.assertEqual(result, 'foo')
-
- def test_get_spec_not_abspath_no_colon_with_package(self):
- from pyramid import tests
- lookup = self._makeOne(None)
- result = lookup.get_spec('foo', tests)
- self.assertEqual(result, 'pyramid.tests:foo')
-
- def test_get_spec_not_abspath_with_colon_no_package(self):
- lookup = self._makeOne(None)
- result = lookup.get_spec('fudge:foo', None)
- self.assertEqual(result, 'fudge:foo')
-
- def test_get_spec_not_abspath_with_colon_with_package(self):
- from pyramid import tests
- lookup = self._makeOne(None)
- result = lookup.get_spec('fudge:foo', tests)
- self.assertEqual(result, 'fudge:foo')
-
- def test_get_spec_is_abspath_no_colon_no_package(self):
- import os
- lookup = self._makeOne(None)
- spec = os.path.abspath(__file__)
- result = lookup.get_spec(spec, None)
- self.assertEqual(result, spec)
-
- def test_get_spec_is_abspath_no_colon_with_path_in_package(self):
- from pyramid import tests
- import os
- lookup = self._makeOne(None)
- f = __file__
- spec = os.path.abspath(f)
- result = lookup.get_spec(spec, tests)
- self.assertEqual(result, 'pyramid.tests:%s' % os.path.split(f)[-1])
-
- def test_get_spec_is_abspath_no_colon_with_path_outside_package(self):
- import venusian # used only because it's outside of pyramid.tests
- import os
- lookup = self._makeOne(None)
- f = __file__
- spec = os.path.abspath(f)
- result = lookup.get_spec(spec, venusian)
- self.assertEqual(result, spec)
-
- def test_get_spec_is_abspath_with_colon_no_package(self):
- import os
- lookup = self._makeOne(None)
- spec = os.path.join(os.path.abspath(__file__), ':foo')
- result = lookup.get_spec(spec, None)
- self.assertEqual(result, spec)
-
- def test_get_spec_is_abspath_with_colon_with_path_in_package(self):
- from pyramid import tests
- import os
- lookup = self._makeOne(None)
- f = os.path.abspath(__file__)
- spec = os.path.join(f, ':foo')
- result = lookup.get_spec(spec, tests)
- tail = spec.split(os.sep)[-2:]
- self.assertEqual(result, 'pyramid.tests:%s/%s' % tuple(tail))
-
- def test_get_spec_is_abspath_with_colon_with_path_outside_package(self):
- import venusian # used only because it's outside of pyramid.tests
- import os
- lookup = self._makeOne(None)
- spec = os.path.join(os.path.abspath(__file__), ':foo')
- result = lookup.get_spec(spec, venusian)
- self.assertEqual(result, spec)
-
- def test_translate(self):
- from pyramid.interfaces import IChameleonTranslate
- def t(): pass
- self.config.registry.registerUtility(t, IChameleonTranslate)
- lookup = self._makeOne(None)
- self.assertEqual(lookup.translate, t)
-
- def test_debug_settings_None(self):
- self.config.registry.settings = None
- lookup = self._makeOne(None)
- self.assertEqual(lookup.debug, False)
-
- def test_debug_settings_not_None(self):
- self.config.registry.settings = {'debug_templates':True}
- lookup = self._makeOne(None)
- self.assertEqual(lookup.debug, True)
-
- def test_auto_reload_settings_None(self):
- self.config.registry.settings = None
- lookup = self._makeOne(None)
- self.assertEqual(lookup.auto_reload, False)
-
- def test_auto_reload_settings_not_None(self):
- self.config.registry.settings = {'reload_templates':True}
- lookup = self._makeOne(None)
- self.assertEqual(lookup.auto_reload, True)
-
- def test___call__abspath_path_notexists(self):
- abspath = '/wont/exist'
- self._registerTemplateRenderer({}, abspath)
- info = DummyRendererInfo({
- 'name':abspath,
- 'package':None,
- 'registry':self.config.registry,
- 'settings':{},
- 'type':'type',
- })
- lookup = self._makeOne(None)
- self.assertRaises(ValueError, lookup.__call__, info)
-
- def test___call__abspath_alreadyregistered(self):
- import os
- abspath = os.path.abspath(__file__)
- renderer = {}
- self._registerTemplateRenderer(renderer, abspath)
- info = DummyRendererInfo({
- 'name':abspath,
- 'package':None,
- 'registry':self.config.registry,
- 'settings':{},
- 'type':'type',
- })
- lookup = self._makeOne(None)
- result = lookup(info)
- self.assertTrue(result is renderer)
-
- def test___call__abspath_notyetregistered(self):
- import os
- abspath = os.path.abspath(__file__)
- renderer = {}
- factory = DummyFactory(renderer)
- info = DummyRendererInfo({
- 'name':abspath,
- 'package':None,
- 'registry':self.config.registry,
- 'settings':{},
- 'type':'type',
- })
- lookup = self._makeOne(factory)
- result = lookup(info)
- self.assertEqual(result, renderer)
-
- def test___call__relpath_path_registered(self):
- renderer = {}
- spec = 'foo/bar'
- self._registerTemplateRenderer(renderer, spec)
- info = DummyRendererInfo({
- 'name':spec,
- 'package':None,
- 'registry':self.config.registry,
- 'settings':{},
- 'type':'type',
- })
- lookup = self._makeOne(None)
- result = lookup(info)
- self.assertTrue(renderer is result)
-
- def test___call__relpath_has_package_registered(self):
- renderer = {}
- import pyramid.tests
- spec = 'bar/baz'
- self._registerTemplateRenderer(renderer, 'pyramid.tests:bar/baz')
- info = DummyRendererInfo({
- 'name':spec,
- 'package':pyramid.tests,
- 'registry':self.config.registry,
- 'settings':{},
- 'type':'type',
- })
- lookup = self._makeOne(None)
- result = lookup(info)
- self.assertTrue(renderer is result)
-
- def test___call__spec_notfound(self):
- spec = 'pyramid.tests:wont/exist'
- info = DummyRendererInfo({
- 'name':spec,
- 'package':None,
- 'registry':self.config.registry,
- 'settings':{},
- 'type':'type',
- })
- lookup = self._makeOne(None)
- self.assertRaises(ValueError, lookup.__call__, info)
-
- def test___call__spec_alreadyregistered(self):
- from pyramid import tests
- module_name = tests.__name__
- relpath = 'test_renderers.py'
- spec = '%s:%s' % (module_name, relpath)
- info = DummyRendererInfo({
- 'name':spec,
- 'package':None,
- 'registry':self.config.registry,
- 'settings':{},
- 'type':'type',
- })
- renderer = {}
- self._registerTemplateRenderer(renderer, spec)
- lookup = self._makeOne(None)
- result = lookup(info)
- self.assertTrue(result is renderer)
-
- def test___call__spec_notyetregistered(self):
- import os
- from pyramid import tests
- module_name = tests.__name__
- relpath = 'test_renderers.py'
- renderer = {}
- factory = DummyFactory(renderer)
- spec = '%s:%s' % (module_name, relpath)
- info = DummyRendererInfo({
- 'name':spec,
- 'package':None,
- 'registry':self.config.registry,
- 'settings':{},
- 'type':'type',
- })
- lookup = self._makeOne(factory)
- result = lookup(info)
- self.assertTrue(result is renderer)
- path = os.path.abspath(__file__).split('$')[0] # jython
- if path.endswith('.pyc'): # pragma: no cover
- path = path[:-1]
- self.assertTrue(factory.path.startswith(path))
- self.assertEqual(factory.kw, {})
-
- def test___call__reload_assets_true(self):
- import pyramid.tests
- from pyramid.interfaces import ISettings
- from pyramid.interfaces import ITemplateRenderer
- settings = {'reload_assets':True}
- self.config.registry.registerUtility(settings, ISettings)
- renderer = {}
- factory = DummyFactory(renderer)
- spec = 'test_renderers.py'
- reg = self.config.registry
- info = DummyRendererInfo({
- 'name':spec,
- 'package':pyramid.tests,
- 'registry':reg,
- 'settings':settings,
- 'type':'type',
- })
- lookup = self._makeOne(factory)
- result = lookup(info)
- self.assertTrue(result is renderer)
- spec = '%s:%s' % ('pyramid.tests', 'test_renderers.py')
- self.assertEqual(reg.queryUtility(ITemplateRenderer, name=spec),
- None)
-
- def test___call__reload_assets_false(self):
- import pyramid.tests
- from pyramid.interfaces import ITemplateRenderer
- settings = {'reload_assets':False}
- renderer = {}
- factory = DummyFactory(renderer)
- spec = 'test_renderers.py'
- reg = self.config.registry
- info = DummyRendererInfo({
- 'name':spec,
- 'package':pyramid.tests,
- 'registry':reg,
- 'settings':settings,
- 'type':'type',
- })
- lookup = self._makeOne(factory)
- result = lookup(info)
- self.assertTrue(result is renderer)
- spec = '%s:%s' % ('pyramid.tests', 'test_renderers.py')
- self.assertNotEqual(reg.queryUtility(ITemplateRenderer, name=spec),
- None)
class Test_json_renderer_factory(unittest.TestCase):
def setUp(self):

0 comments on commit 7e75f11

Please sign in to comment.
Something went wrong with that request. Please try again.