pyramid.path¶
- CALLER_PACKAGE¶
A constant used by the constructor of
pyramid.path.DottedNameResolverandpyramid.path.AssetResolver.
- class DottedNameResolver(package=pyramid.path.CALLER_PACKAGE)[ソース]¶
A class used to resolve a dotted Python name to a package or module object.
バージョン 1.3 で追加.
The constructor accepts a single argument named
packagewhich may be any of:A fully qualified (not relative) dotted name to a module or package
a Python module or package object
The value
NoneThe constant value
pyramid.path.CALLER_PACKAGE.
The default value is
pyramid.path.CALLER_PACKAGE.The
packageis used when a relative dotted name is supplied to theresolve()method. A dotted name which has a.(dot) or:(colon) as its first character is treated as relative.If
packageisNone, the resolver will only be able to resolve fully qualified (not relative) names. Any attempt to resolve a relative name will result in anValueErrorexception.If
packageispyramid.path.CALLER_PACKAGE, the resolver will treat relative dotted names as relative to the caller of theresolve()method.If
packageis a module or module name (as opposed to a package or package name), its containing package is computed and this package used to derive the package name (all names are resolved relative to packages, never to modules). For example, if thepackageargument to this type was passed the stringxml.dom.expatbuilder, and.mindomis supplied to theresolve()method, the resulting import would be forxml.minidom, becausexml.dom.expatbuilderis a module object, not a package object.If
packageis a package or package name (as opposed to a module or module name), this package will be used to relative compute dotted names. For example, if thepackageargument to this type was passed the stringxml.dom, and.minidomis supplied to theresolve()method, the resulting import would be forxml.minidom.- maybe_resolve(dotted)[ソース]¶
This method behaves just like
resolve(), except if thedottedvalue passed is not a string, it is simply returned. For example:import xml r = DottedNameResolver() v = r.maybe_resolve(xml) # v is the xml module; no exception raised
- resolve(dotted)[ソース]¶
This method resolves a dotted name reference to a global Python object (an object which can be imported) to the object itself.
Two dotted name styles are supported:
pkg_resources-style dotted names where non-module attributes of a package are separated from the rest of the path using a:e.g.package.module:attr.zope.dottedname-style dotted names where non-module attributes of a package are separated from the rest of the path using a.e.g.package.module.attr.
These styles can be used interchangeably. If the supplied name contains a
:(colon), thepkg_resourcesresolution mechanism will be chosen, otherwise thezope.dottednameresolution mechanism will be chosen.If the
dottedargument passed to this method is not a string, aValueErrorwill be raised.When a dotted name cannot be resolved, a
ValueErrorerror is raised.Example:
r = DottedNameResolver() v = r.resolve('xml') # v is the xml module
- class AssetResolver(package=pyramid.path.CALLER_PACKAGE)[ソース]¶
A class used to resolve an asset specification to an asset descriptor.
バージョン 1.3 で追加.
The constructor accepts a single argument named
packagewhich may be any of:A fully qualified (not relative) dotted name to a module or package
a Python module or package object
The value
NoneThe constant value
pyramid.path.CALLER_PACKAGE.
The default value is
pyramid.path.CALLER_PACKAGE.The
packageis used when a relative asset specification is supplied to theresolve()method. An asset specification without a colon in it is treated as relative.If
packageisNone, the resolver will only be able to resolve fully qualified (not relative) asset specifications. Any attempt to resolve a relative asset specification will result in anValueErrorexception.If
packageispyramid.path.CALLER_PACKAGE, the resolver will treat relative asset specifications as relative to the caller of theresolve()method.If
packageis a module or module name (as opposed to a package or package name), its containing package is computed and this package is used to derive the package name (all names are resolved relative to packages, never to modules). For example, if thepackageargument to this type was passed the stringxml.dom.expatbuilder, andtemplate.ptis supplied to theresolve()method, the resulting absolute asset spec would bexml.minidom:template.pt, becausexml.dom.expatbuilderis a module object, not a package object.If
packageis a package or package name (as opposed to a module or module name), this package will be used to compute relative asset specifications. For example, if thepackageargument to this type was passed the stringxml.dom, andtemplate.ptis supplied to theresolve()method, the resulting absolute asset spec would bexml.minidom:template.pt.- resolve(spec)[ソース]¶
Resolve the asset spec named as
specto an object that has the attributes and methods described inpyramid.interfaces.IAssetDescriptor.If
specis an absolute filename (e.g./path/to/myproject/templates/foo.pt) or an absolute asset spec (e.g.myproject:templates.foo.pt), an asset descriptor is returned without taking into account thepackagepassed to this class' constructor.If
specis a relative asset specification (an asset specification without a:in it, e.g.templates/foo.pt), thepackageargument of the constructor is used as the package portion of the asset spec. For example:a = AssetResolver('myproject') resolver = a.resolve('templates/foo.pt') print(resolver.abspath()) # -> /path/to/myproject/templates/foo.pt
If the AssetResolver is constructed without a
packageargument ofNone, and a relative asset specification is passed toresolve, anValueErrorexception is raised.