本文整理汇总了Python中pyramid.configuration.Configurator.add_renderer方法的典型用法代码示例。如果您正苦于以下问题:Python Configurator.add_renderer方法的具体用法?Python Configurator.add_renderer怎么用?Python Configurator.add_renderer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyramid.configuration.Configurator
的用法示例。
在下文中一共展示了Configurator.add_renderer方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_it_with_dotted_renderer
# 需要导入模块: from pyramid.configuration import Configurator [as 别名]
# 或者: from pyramid.configuration.Configurator import add_renderer [as 别名]
def test_it_with_dotted_renderer(self):
from zope.interface import implementedBy
from pyramid.threadlocal import get_current_registry
from pyramid.interfaces import IRequest
from pyramid.interfaces import IView
from pyramid.interfaces import IViewClassifier
from pyramid.exceptions import Forbidden
from pyramid.configuration import Configurator
context = DummyContext()
reg = get_current_registry()
config = Configurator(reg)
def dummy_renderer_factory(*arg, **kw):
return lambda *arg, **kw: 'OK'
config.add_renderer('.pt', dummy_renderer_factory)
def view(request):
return {}
self._callFUT(context, view, renderer='fake.pt')
actions = context.actions
regadapt = actions[0]
register = regadapt['callable']
register()
derived_view = reg.adapters.lookup(
(IViewClassifier, IRequest, implementedBy(Forbidden)),
IView, default=None)
self.assertNotEqual(derived_view, None)
self.assertEqual(derived_view(None, None).body, 'OK')
self.assertEqual(derived_view.__name__, 'bwcompat_view')
示例2: TestIntegration
# 需要导入模块: from pyramid.configuration import Configurator [as 别名]
# 或者: from pyramid.configuration.Configurator import add_renderer [as 别名]
class TestIntegration(unittest.TestCase):
def setUp(self):
import pyramid.mako_templating
from pyramid.configuration import Configurator
self.config = Configurator()
self.config.begin()
self.config.add_settings({'mako.directories':
'pyramid.tests:fixtures'})
self.config.add_renderer('.mak',
pyramid.mako_templating.renderer_factory)
def tearDown(self):
self.config.end()
def test_render(self):
from pyramid.renderers import render
result = render('helloworld.mak', {'a':1})
self.assertEqual(result, u'\nHello föö\n')
def test_render_to_response(self):
from pyramid.renderers import render_to_response
result = render_to_response('helloworld.mak', {'a':1})
self.assertEqual(result.ubody, u'\nHello föö\n')
def test_get_renderer(self):
from pyramid.renderers import get_renderer
result = get_renderer('helloworld.mak')
self.assertEqual(result.implementation().render_unicode(),
u'\nHello föö\n')
示例3: renderer
# 需要导入模块: from pyramid.configuration import Configurator [as 别名]
# 或者: from pyramid.configuration.Configurator import add_renderer [as 别名]
def renderer(_context, factory, name=''):
# renderer factories must be registered eagerly so they can be
# found by the view machinery
reg = get_current_registry()
config = Configurator(reg, package=_context.package)
config.add_renderer(name, factory, _info=_context.info)
_context.action(discriminator=(IRendererFactory, name))
示例4: renderer
# 需要导入模块: from pyramid.configuration import Configurator [as 别名]
# 或者: from pyramid.configuration.Configurator import add_renderer [as 别名]
def renderer(_context, factory, name=''):
# renderer factories must be registered eagerly so they can be
# found by the view machinery
try:
reg = _context.registry
except AttributeError: # pragma: no cover (b/c)
reg = get_current_registry()
config = Configurator(reg, package=_context.package)
config.add_renderer(name, factory, _info=_context.info)
_context.action(discriminator=(IRendererFactory, name))
示例5: make_pyramid_app
# 需要导入模块: from pyramid.configuration import Configurator [as 别名]
# 或者: from pyramid.configuration.Configurator import add_renderer [as 别名]
def make_pyramid_app(global_config, **kwargs):
""" This function returns a WSGI application.
"""
settings = dict(global_config)
settings.update(kwargs)
config = Configurator(root_factory=get_root,
settings=init_settings(global_config))
config.add_renderer('.html', renderer_factory)
config.add_static_view('static', 'academe:static')
config.scan('academe')
pyramid_app = config.make_wsgi_app()
return pyramid_app
示例6: main
# 需要导入模块: from pyramid.configuration import Configurator [as 别名]
# 或者: from pyramid.configuration.Configurator import add_renderer [as 别名]
def main(global_config, **settings):
""" This function returns a Pyramid WSGI application.
"""
config = Configurator()
config.begin()
db_uri = settings['db_uri']
server_url = settings['server_url']
conn = Connection(db_uri)
config.registry.settings['db_conn'] = conn
config.registry.settings['server_url'] = server_url
config.registry.settings['db_name'] = settings['db_name']
config.add_subscriber(add_mongo_db, NewRequest)
config.include('pyramid_zcml')
config.load_zcml('configure.zcml')
config.end()
#add renderer
#Fixme: try to insert this on zcml file
config.add_renderer('jsonp', JSONP(param_name='callback'))
return config.make_wsgi_app()
示例7: main
# 需要导入模块: from pyramid.configuration import Configurator [as 别名]
# 或者: from pyramid.configuration.Configurator import add_renderer [as 别名]
def main(global_config, **settings):
""" This function returns a Pyramid WSGI application.
"""
from pyramid.configuration import Configurator
config = Configurator(settings=settings)
config.add_renderer(name = 'nt', factory='.renderers.NTriplesGraphRenderer')
config.add_renderer(name = 'rdfxml', factory='.renderers.RDFXMLGraphRenderer')
config.add_renderer(name = 'n3', factory='.renderers.N3GraphRenderer')
session_factory = session_factory_from_settings(settings)
config.set_session_factory(session_factory)
config.registry['sparql.url'] = settings['sparql.url']
config.add_static_view('static', 'sparql_shim:static/')
config.add_handler('direct_graph', '/{name:.+}', '.handlers:GraphHandler',
action='graph')
config.add_handler('indirect_graph', '/', '.handlers:GraphHandler',
action='graph')
config.scan('sparql_shim.subscribers')
return config.make_wsgi_app()
示例8: setUp
# 需要导入模块: from pyramid.configuration import Configurator [as 别名]
# 或者: from pyramid.configuration.Configurator import add_renderer [as 别名]
def setUp(registry=None, request=None, hook_zca=True):
"""
Set :app:`Pyramid` registry and request thread locals for the
duration of a single unit test.
Use this function in the ``setUp`` method of a unittest test case
which directly or indirectly uses:
- any of the ``register*`` functions in :mod:`pyramid.testing`
(such as :func:`pyramid.testing.registerModels`)
- any method of the :class:`pyramid.configuration.Configurator`
object returned by this function.
- the :func:`pyramid.threadlocal.get_current_registry` or
:func:`pyramid.threadlocal.get_current_request` functions.
If you use the ``testing.register*`` APIs, or the
``get_current_*`` functions (or call :app:`Pyramid` code that
uses these functions) without calling ``setUp``,
:func:`pyramid.threadlocal.get_current_registry` will return a
*global* :term:`application registry`, which may cause unit tests
to not be isolated with respect to registrations they perform.
If the ``registry`` argument is ``None``, a new empty
:term:`application registry` will be created (an instance of the
:class:`pyramid.registry.Registry` class). If the ``registry``
argument is not ``None``, the value passed in should be an
instance of the :class:`pyramid.registry.Registry` class or a
suitable testing analogue.
After ``setUp`` is finished, the registry returned by the
:func:`pyramid.threadlocal.get_current_request` function will
be the passed (or constructed) registry until
:func:`pyramid.testing.tearDown` is called (or
:func:`pyramid.testing.setUp` is called again) .
If the ``hook_zca`` argument is ``True``, ``setUp`` will attempt
to perform the operation ``zope.component.getSiteManager.sethook(
pyramid.threadlocal.get_current_registry)``, which will cause
the :term:`Zope Component Architecture` global API
(e.g. :func:`zope.component.getSiteManager`,
:func:`zope.component.getAdapter`, and so on) to use the registry
constructed by ``setUp`` as the value it returns from
:func:`zope.component.getSiteManager`. If the
:mod:`zope.component` package cannot be imported, or if
``hook_zca`` is ``False``, the hook will not be set.
This function returns an instance of the
:class:`pyramid.configuration.Configurator` class, which can be
used for further configuration to set up an environment suitable
for a unit or integration test. The ``registry`` attribute
attached to the Configurator instance represents the 'current'
:term:`application registry`; the same registry will be returned
by :func:`pyramid.threadlocal.get_current_registry` during the
execution of the test.
.. warning:: Although this method of setting up a test registry
will never disappear, after :app:`Pyramid` 1.0,
using the ``begin`` and ``end`` methods of a
``Configurator`` are preferred to using
``pyramid.testing.setUp`` and
``pyramid.testing.tearDown``. See
:ref:`unittesting_chapter` for more information.
"""
manager.clear()
if registry is None:
registry = Registry("testing")
config = Configurator(registry=registry)
if hasattr(registry, "registerUtility"):
# Sometimes nose calls us with a non-registry object because
# it thinks this function is module test setup. Likewise,
# someone may be passing us an esoteric "dummy" registry, and
# the below won't succeed if it doesn't have a registerUtility
# method.
from pyramid.configuration import DEFAULT_RENDERERS
for name, renderer in DEFAULT_RENDERERS:
# Cause the default renderers to be registered because
# in-the-wild test code relies on being able to call
# e.g. ``pyramid.chameleon_zpt.render_template``
# without registering a .pt renderer, expecting the "real"
# template to be rendered. This is a holdover from when
# individual template system renderers weren't indirected
# by the ``pyramid.renderers`` machinery, and
# ``render_template`` and friends went behind the back of
# any existing renderer factory lookup system.
config.add_renderer(name, renderer)
hook_zca and config.hook_zca()
config.begin(request=request)
return config
示例9: TestIntegration
# 需要导入模块: from pyramid.configuration import Configurator [as 别名]
# 或者: from pyramid.configuration.Configurator import add_renderer [as 别名]
class TestIntegration(unittest.TestCase):
def setUp(self):
import pyramid.mako_templating
from pyramid.configuration import Configurator
self.config = Configurator()
self.config.begin()
self.config.add_settings({'mako.directories':
'pyramid.tests:fixtures'})
self.config.add_renderer('.mak',
pyramid.mako_templating.renderer_factory)
def tearDown(self):
self.config.end()
def test_render(self):
from pyramid.renderers import render
result = render('helloworld.mak', {'a':1})
self.assertEqual(result, u'\nHello föö\n')
def test_render_from_fs(self):
from pyramid.renderers import render
self.config.add_settings({'reload_templates': True})
result = render('helloworld.mak', {'a':1})
self.assertEqual(result, u'\nHello föö\n')
def test_render_inheritance(self):
from pyramid.renderers import render
result = render('helloinherit.mak', {})
self.assertEqual(result, u'Layout\nHello World!\n')
def test_render_inheritance_pkg_spec(self):
from pyramid.renderers import render
result = render('hello_inherit_pkg.mak', {})
self.assertEqual(result, u'Layout\nHello World!\n')
def test_render_to_response(self):
from pyramid.renderers import render_to_response
result = render_to_response('helloworld.mak', {'a':1})
self.assertEqual(result.ubody, u'\nHello föö\n')
def test_render_to_response_pkg_spec(self):
from pyramid.renderers import render_to_response
result = render_to_response('pyramid.tests:fixtures/helloworld.mak', {'a':1})
self.assertEqual(result.ubody, u'\nHello föö\n')
def test_render_with_abs_path(self):
from pyramid.renderers import render
result = render('/helloworld.mak', {'a':1})
self.assertEqual(result, u'\nHello föö\n')
def test_get_renderer(self):
from pyramid.renderers import get_renderer
result = get_renderer('helloworld.mak')
self.assertEqual(result.implementation().render_unicode(),
u'\nHello föö\n')
def test_template_not_found(self):
from pyramid.renderers import render
from mako.exceptions import TemplateLookupException
self.assertRaises(TemplateLookupException, render,
'helloworld_not_here.mak', {})