ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 02.01.2026

Просмотров: 2693

Скачиваний: 0

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

CHAPTER

FORTYTHREE

PYRAMID.CHAMELEON_ZPT

get_template(path)

Return the underyling object representing a 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 asset specification.

latex-warning.png

This API is deprecated in Pyramid 1.0. Use the implementation() method of a template renderer retrieved via pyramid.renderers.get_renderer() instead.

render_template(path, **kw)

Render a 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 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.

latex-warning.png

This API is deprecated in Pyramid 1.0. Use pyramid.renderers.render() instead.

517

43. PYRAMID.CHAMELEON_ZPT

render_template_to_response(path, **kw)

Render a 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 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 Response object with the body as the template result.

latex-warning.png

This API is deprecated in Pyramid 1.0. Use pyramid.renderers.render_to_response() instead.

These APIs will work against files which supply template text which matches the ZPT specification.

The API of pyramid.chameleon_zpt is identical to that of pyramid.chameleon_text; only its import location is different. If you need to import an API functions from this module as well as the pyramid.chameleon_text module within the same view file, use the as feature of the Python import statement, e.g.:

1

2

from pyramid.chameleon_zpt import render_template as zpt_render from pyramid.chameleon_text import render_template as text_render

518



CHAPTER

FORTYFOUR

PYRAMID.CONFIG

class Configurator(registry=None, package=None, settings=None, root_factory=None, authentication_policy=None, authorization_policy=None, renderers=None, debug_logger=None, locale_negotiator=None, request_factory=None, renderer_globals_factory=None, default_permission=None, session_factory=None, default_view_mapper=None, autocommit=False, exceptionresponse_view=<function default_exceptionresponse_view at 0x2a097d0>,

route_prefix=None, introspection=True)

A Configurator is used to configure a Pyramid application registry.

The Configurator accepts a number of arguments: registry, package, settings, root_factory, authentication_policy, authorization_policy, renderers, debug_logger, locale_negotiator, request_factory, renderer_globals_factory, default_permission, session_factory, default_view_mapper, autocommit, exceptionresponse_view and route_prefix.

If the registry argument is passed as a non-None value, it must be an instance of the pyramid.registry.Registry class representing the registry to configure. If registry is None, the configurator will create a pyramid.registry.Registry instance itself; it will also perform some default configuration that would not otherwise be done. After its construction, the configurator may be used to add further configuration to the registry.

latex-warning.png

If a registry is passed to the Configurator constructor, all other constructor arguments except package are ignored.

519

44. PYRAMID.CONFIG

If the package argument is passed, it must be a reference to a Python package (e.g. sys.modules[’thepackage’]) or a dotted Python name to the same. This value is used as a basis to convert relative paths passed to various configuration methods, such as methods which accept a renderer argument, into absolute paths. If None is passed (the default), the package is assumed to be the Python package in which the caller of the Configurator constructor lives.

If the settings argument is passed,

it should be a

Python dictionary rep-

resenting the deployment settings for this

application.

These are later retriev-

able using the pyramid.registry.Registry.settings attribute (aka request.registry.settings).

If the root_factory argument is passed, it should be an object representing the default root factory for your application or a dotted Python name to the same. If it is None, a default root factory will be used.

If authentication_policy is passed, it should be an instance of an authentication policy or a dotted Python name to the same.

If authorization_policy is passed, it should be an instance of an authorization policy or a dotted Python name to the same.

latex-note.png

A ConfigurationError will be raised when an authorization policy is supplied without also supplying an authentication policy (authorization requires authentication).

If renderers is passed, it should be a list of tuples representing a set of renderer factories which should be configured into this application (each tuple representing a set of positional values that should be passed to pyramid.config.Configurator.add_renderer()). If it is not passed, a default set of renderer factories is used.

If debug_logger is not passed, a default debug logger that logs to a logger will be used (the logger name will be the package name of the caller of this configurator). If it is passed, it should be an instance of the logging.Logger (PEP 282) standard library class or a Python logger name. The debug logger is used by Pyramid itself to log warnings and authorization debugging information.

If locale_negotiator is passed, it should be a locale negotiator implementation or a dotted Python name to same. See Using a Custom Locale Negotiator.

520


If request_factory is passed, it should be a request factory implementation or a dotted Python name to the same. See Changing the Request Factory. By default it is None, which means use the default request factory.

If renderer_globals_factory is passed, it should be a renderer globals factory implementation or a dotted Python name to the same. See Adding Renderer Globals (Deprecated). By default, it is None, which means use no renderer globals factory.

latex-warning.png

as of Pyramid 1.1, renderer_globals_factory is deprecated. Instead, use a BeforeRender event subscriber as per Using The Before Render Event.

If default_permission is passed, it should be a permission string to be used as the default permission for all view configuration registrations performed against this Configurator. An example of a permission string:’view’. Adding a default permission makes it unnecessary to protect each view configuration with an explicit permission, unless your application policy requires some exception for a particular view. By default, default_permission is None, meaning that view configurations which do not explicitly declare a permission will always be executable by entirely anonymous users (any authorization policy in effect is ignored). See also Setting a Default Permission.

If session_factory is passed, it should be an object which implements the session factory interface. If a nondefault value is passed, the session_factory will be used to create a session object when request.session is accessed. Note that the same outcome can be achieved by calling pyramid.config.Configurator.set_session_factory(). By default, this argument is None, indicating that no session factory will be configured (and thus accessing request.session will throw an error) unless set_session_factory is called later during configuration.

If autocommit is True, every method called on the configurator will cause an immediate action, and no configuration conflict detection will be used. If autocommit is False, most methods of the configurator will defer their action until pyramid.config.Configurator.commit() is called. When pyramid.config.Configurator.commit() is called, the actions implied by the called methods will be checked for configuration conflicts unless autocommit is True. If a conflict is detected a ConfigurationConflictError will be raised. Calling pyramid.config.Configurator.make_wsgi_app() always implies a final commit.

If default_view_mapper is passed,

it

will be

used as

the default

view

mapper factory for view configurations

that

don’t

otherwise

specify one

(see

521


44. PYRAMID.CONFIG

pyramid.interfaces.IViewMapperFactory). If a default_view_mapper is not passed, a superdefault view mapper will be used.

If exceptionresponse_view is passed, it must be a view callable or None. If it is a view callable, it will be used as an exception view callable when an exception response is raised. If exceptionresponse_view is None, no exception response view will be registered, and all raised exception responses will be bubbled up to Pyramid’s caller. By default, the pyramid.httpexceptions.default_exceptionresponse_view function is used as the exceptionresponse_view. This argument is new in Pyramid 1.1.

If route_prefix is passed, all routes added with pyramid.config.Configurator.add_route() will have the specified path prepended to their pattern. This parameter is new in Pyramid 1.2.

If introspection is passed, it must be a boolean value. If it’s True, introspection values during actions will be kept for for use for tools like the debug toolbar. If it’s False, introspection values provided by registrations will be ignored. By default, it is True. This parameter is new as of Pyramid 1.3.

Controlling Configuration State

commit()

Commit any pending configuration actions. If a configuration conflict is detected in the pending configuration actions, this method will raise a ConfigurationConflictError; within the traceback of this error will be information about the source of the conflict, usually including file names and line numbers of the cause of the configuration conflicts.

begin(request=None)

Indicate that application or test configuration has begun. This pushes a dictionary containing the application registry implied by registry attribute of this configurator and the request implied by the request argument on to the thread local stack consulted by various pyramid.threadlocal API functions.

end()

Indicate that application or test configuration has ended. This pops the last value pushed on to the thread local stack (usually by the begin method) and returns that value.

include(callable, route_prefix=None)

Include a configuration callables, to support imperative application extensibility.

522

latex-warning.png

In versions of Pyramid prior to 1.2, this function accepted *callables, but this has been changed to support only a single callable.

A configuration callable should be a callable that accepts a single argument named config, which will be an instance of a Configurator (be warned that it will not be the same configurator instance on which you call this method, however). The code which runs as the result of calling the callable should invoke methods on the configurator passed to it which add configuration state. The return value of a callable will be ignored.

Values allowed to be presented via the callable argument to this method: any callable Python object or any dotted Python name which resolves to a callable Python object. It may also be a Python module, in which case, the module will be searched for a callable named includeme, which will be treated as the configuration callable.

For example, if the includeme function below lives in a module named myapp.myconfig:

1

2

3

4

5

6

7

8

# myapp.myconfig module

def my_view(request):

from pyramid.response import Response return Response(’OK’)

def includeme(config): config.add_view(my_view)

You might cause it be included within your Pyramid application like so:

1 from pyramid.config import Configurator

2

3 def main(global_config, **settings):

4 config = Configurator()

5config.include(’myapp.myconfig.includeme’)

523