Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
pyramid.pdf
Скачиваний:
10
Добавлен:
24.03.2015
Размер:
3.82 Mб
Скачать

CHAPTER

FIFTY

PYRAMID.LOCATION

lineage(resource)

Return a generator representing the lineage of the resource object implied by the resource argument. The generator first returns resource unconditionally. Then, if resource supplies a __parent__ attribute, return the resource represented by resource.__parent__. If that resource has a __parent__ attribute, return that resource’s parent, and so on, until the resource being inspected either has no __parent__ attribute or which has a __parent__ attribute of None. For example, if the resource tree is:

thing1 = Thing() thing2 = Thing()

thing2.__parent__ = thing1

Calling lineage(thing2) will return a generator. When we turn it into a list, we will get:

list(lineage(thing2))

[ <Thing object at thing2>, <Thing object at thing1> ]

inside(resource1, resource2)

Is resource1 ‘inside’ resource2? Return True if so, else False.

resource1 is ‘inside’ resource2 if resource2 is a lineage ancestor of resource1. It is a lineage ancestor if its parent (or one of its parent’s parents, etc.) is an ancestor.

607

50. PYRAMID.LOCATION

608

CHAPTER

FIFTYONE

PYRAMID.PASTER

bootstrap(config_uri, request=None)

Load a WSGI application from the PasteDeploy config file specified by config_uri. The environment will be configured as if it is currently serving request, leaving a natural environment in place to write scripts that can generate URLs and utilize renderers.

This function returns a dictionary with app, root, closer, request, and registry keys. app is the WSGI app loaded (based on the config_uri), root is the traversal root resource of the Pyramid application, and closer is a parameterless callback that may be called when your script is complete (it pops a threadlocal stack).

latex-note.png

Most operations within Pyramid expect to be invoked within the context of a WSGI request, thus it’s important when loading your application to anchor it when executing scripts and other code that is not normally invoked during active WSGI requests.

latex-note.png

For a complex config file containing multiple Pyramid applications, this function will setup the environment under the context of the last-loaded Pyramid application. You may load a specific application yourself by using the lower-level functions pyramid.paster.get_app() and pyramid.scripting.prepare() in conjunction with pyramid.config.global_registries.

609

51. PYRAMID.PASTER

config_uri – specifies the PasteDeploy config file to use for the interactive shell. The format is inifile#name. If the name is left off, main will be assumed.

request – specified to anchor the script to a given set of WSGI parameters. For example, most people would want to specify the host, scheme and port such that their script will generate URLs in relation to those parameters. A request with default parameters is constructed for you if none is provided. You can mutate the request’s environ later to setup a specific host/port/scheme/etc.

See Writing a Script for more information about how to use this function.

get_app(config_uri, name=None)

Return the WSGI application named name in the PasteDeploy config file specified by config_uri.

If the name is None, this will attempt to parse the name from the config_uri string expecting the format inifile#name. If no name is found, the name will default to “main”.

get_appsettings(config_uri, name=None)

Return a dictionary representing the key/value pairs in an app section within the file represented by config_uri.

If the name is None, this will attempt to parse the name from the config_uri string expecting the format inifile#name. If no name is found, the name will default to “main”.

setup_logging(config_uri)

Set up logging via the logging module’s fileConfig function with the filename specified via config_uri (a string in the form filename#sectionname).

ConfigParser defaults are specified for the special __file__ and here variables, similar to PasteDeploy config loading.

610

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]