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

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

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

Добавлен: 02.01.2026

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

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

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

49. PYRAMID.INTERFACES

action_info

An IActionInfo object representing the caller that invoked the creation of this introspectable (usually a sentinel until updated during self.register)

__hash__()

Introspectables must be hashable. The typical implementation of an introsepectable’s __hash__ is:

return hash((self.category_name,) + (self.discriminator,))

discriminator

introspectable discriminator (within category) (must be hashable)

order

integer order in which registered with introspector (managed by introspector, usually)

category_name introspection category name

interface IIntrospector

get(category_name, discriminator, default=None)

Get the IIntrospectable related to the category_name and the discriminator (or discriminator hash) discriminator. If it does not exist in the introspector, return the value of default

relate(*pairs)

Given any number of of (category_name, discriminator) pairs passed as positional arguments, relate the associated introspectables to each other. The introspectable related to each pair must have already been added via .add or

.add_intr; a KeyError will result if this is not true. An error will not be raised if any pair has already been associated with another.

This method is not typically called directly, instead it’s called indirectly by pyramid.interfaces.IIntrospector.register()

602

49.2. OTHER INTERFACES

get_category(category_name, default=None, sort_key=None)

Get a sequence of dictionaries in the form

[{’introspectable’:IIntrospectable, ’related’:[sequence of related IIntrospectables]},

...] where each introspectable is part of the category associated with category_name .

If the category named category_name does not exist in the introspector the value passed as default will be returned.

If sort_key is None, the sequence will be returned in the order the introspectables were added to the introspector. Otherwise, sort_key should be a function that accepts an IIntrospectable and returns a value from it (ala the key function of Python’s sorted callable).

unrelate(*pairs)

Given any number of of (category_name, discriminator) pairs passed as positional arguments, unrelate the associated introspectables from each other. The introspectable related to each pair must have already been added via .add or .add_intr; a KeyError will result if this is not true. An error will not be raised if any pair is not already related to another.

This method is not typically called directly, instead it’s called indirectly by pyramid.interfaces.IIntrospector.register()

remove(category_name, discriminator)

Remove the IIntrospectable related to category_name and discriminator from the introspector, and fix up any relations that the introspectable participates in. This method will not raise an error if an introspectable related to the category name and discriminator does not exist.

add(intr)

Add the IIntrospectable intr (use instead of pyramid.interfaces.IIntrospector.add() when you have a custom IIntrospectable). Replaces any existing introspectable registered using the same category/discriminator.

This method is not typically called directly, instead it’s called indirectly by pyramid.interfaces.IIntrospector.register()

related(intr)

Return a sequence of IIntrospectables related to the IIntrospectable intr. Return the empty sequence if no relations for exist.

603


49. PYRAMID.INTERFACES

categorized(sort_key=None)

Get a sequence of tuples in the form [(category_name, [{’introspectable’:IIntrospectable, ’related’:[sequence of related IIntrospectables]},

...])] representing all known introspectables. If sort_key is None, each introspectables sequence will be returned in the order the introspectables were added to the introspector. Otherwise, sort_key should be a function that accepts an IIntrospectable and returns a value from it (ala the key function of Python’s sorted callable).

categories()

Return a sorted sequence of category names known by this introspector

interface IActionInfo

Class which provides code introspection capability associated with an action. The ParserInfo class used by ZCML implements the same interface.

line

Starting line number in file (as an integer) of action-invoking code.This will be None if the value could not be determined.

file

Filename of action-invoking code as a string

__str__()

Return a representation of the action information (including source code from file, if possible)

interface IAssetDescriptor

Describes an asset.

isdir()

Returns True if the asset is a directory, otherwise returns False.

exists()

Returns True if asset exists, otherwise returns False.

stream()

Returns an input stream for reading asset contents. Raises an exception if the asset is a directory or does not exist.

abspath()

Returns an absolute path in the filesystem to the asset.

604

49.2. OTHER INTERFACES

listdir()

Returns iterable of filenames of directory contents. Raises an exception if asset is not a directory.

absspec()

Returns the absolute asset specification for this asset (e.g. mypackage:templates/foo.pt).

interface IResourceURL

virtual_path

The virtual url path of the resource.

physical_path

The physical url path of the resource.

605


49. PYRAMID.INTERFACES

606


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