weaver.wps_restapi.patches
Helpers to work around some default view configurations that are not desired.
Module Contents
- class weaver.wps_restapi.patches.Configurator(registry=None, package=None, settings=None, root_factory=None, security_policy=None, authentication_policy=None, authorization_policy=None, renderers=None, debug_logger=None, locale_negotiator=None, request_factory=None, response_factory=None, default_permission=None, session_factory=None, default_view_mapper=None, autocommit=False, exceptionresponse_view=default_exceptionresponse_view, route_prefix=None, introspection=True, root_package=None)[source]
A Configurator is used to configure a :app:`Pyramid` application registry.
The Configurator lifecycle can be managed by using a context manager to automatically handle calling
pyramid.config.Configurator.begin()
andpyramid.config.Configurator.end()
as well aspyramid.config.Configurator.commit()
.with Configurator(settings=settings) as config: config.add_route('home', '/') app = config.make_wsgi_app()
If the
registry
argument is notNone
, it must be an instance of thepyramid.registry.Registry
class representing the registry to configure. Ifregistry
isNone
, the configurator will create apyramid.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.Warning
If
registry
is assigned the above-mentioned class instance, all other constructor arguments are ignored, with the exception ofpackage
.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 arenderer
argument, into absolute paths. IfNone
is passed (the default), the package is assumed to be the Python package in which the caller of theConfigurator
constructor lives.If the
root_package
is passed, it will propagate through the configuration hierarchy as a way for included packages to locate resources relative to the package in which the mainConfigurator
was created. IfNone
is passed (the default), theroot_package
will be derived from thepackage
argument. Thepackage
attribute is always pointing at the package being included when usinginclude()
, whereas theroot_package
does not change.If the
settings
argument is passed, it should be a Python dictionary representing the deployment settings for this application. These are later retrievable using thepyramid.registry.Registry.settings
attribute (akarequest.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 isNone
, a default root factory will be used.If
security_policy
is passed, it should be an instance of a security policy or a dotted Python name to the same.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.Note
A
ConfigurationError
will be raised when an authorization policy is supplied without also supplying an authentication policy (authorization requires authentication).If
renderers
isNone
(the default), a default set of renderer factories is used. Else, it should be a list of tuples representing a set of renderer factories which should be configured into this application, and each tuple representing a set of positional values that should be passed topyramid.config.Configurator.add_renderer()
.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 thelogging.Logger
(PEP 282) standard library class or a Python logger name. The debug logger is used by :app:`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 custom_locale_negotiator.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 isNone
, which means use the default request factory.If
response_factory
is passed, it should be a response factory implementation or a dotted Python name to the same. See changing_the_response_factory. By default it isNone
, which means use the default response factory.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
isNone
, 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
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, thesession_factory
will be used to create a session object whenrequest.session
is accessed. Note that the same outcome can be achieved by callingpyramid.config.Configurator.set_session_factory()
. By default, this argument isNone
, indicating that no session factory will be configured (and thus accessingrequest.session
will throw an error) unlessset_session_factory
is called later during configuration.If
autocommit
isTrue
, every method called on the configurator will cause an immediate action, and no configuration conflict detection will be used. Ifautocommit
isFalse
, most methods of the configurator will defer their action untilpyramid.config.Configurator.commit()
is called. Whenpyramid.config.Configurator.commit()
is called, the actions implied by the called methods will be checked for configuration conflicts unlessautocommit
isTrue
. If a conflict is detected, aConfigurationConflictError
will be raised. Callingpyramid.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 (seepyramid.interfaces.IViewMapperFactory
). Ifdefault_view_mapper
is not passed, a superdefault view mapper will be used.If
exceptionresponse_view
is passed, it must be a view callable orNone
. If it is a view callable, it will be used as an exception view callable when an exception response is raised. Ifexceptionresponse_view
isNone
, no exception response view will be registered, and all raised exception responses will be bubbled up to Pyramid’s caller. By default, thepyramid.httpexceptions.default_exceptionresponse_view
function is used as theexceptionresponse_view
.If
route_prefix
is passed, all routes added withpyramid.config.Configurator.add_route()
will have the specified path prepended to their pattern.If
introspection
is passed, it must be a boolean value. If it’sTrue
, introspection values during actions will be kept for use for tools like the debug toolbar. If it’sFalse
, introspection values provided by registrations will be ignored. By default, it isTrue
.Added in version 1.1: The
exceptionresponse_view
argument.Added in version 1.2: The
route_prefix
argument.Added in version 1.3: The
introspection
argument.Added in version 1.6: The
root_package
argument. Theresponse_factory
argument.Added in version 1.9: The ability to use the configurator as a context manager with the
with
-statement to make threadlocal configuration available for further configuration with an implicit commit.- route_prefix_context(route_prefix)[source]
Copy of the original configurator, with tweak for leaving the leading
/
of the suppliedroute_prefix
.Todo
Workaround for https://github.com/Pylons/pyramid/issues/3758
- class weaver.wps_restapi.patches.NoAutoHeadList[source]
List that does not allow addition of HTTP HEAD method object references unless allowed once.
Initialize self. See help(type(self)) for accurate signature.
- class weaver.wps_restapi.patches.ServiceAutoAcceptDecorator(name, path=None, description=None, cors_policy=None, depth=1, pyramid_route=None, **kw)[source]
Extends the view
decorator()
to allow multipleaccept
headers provided all at once.The base
CorniceService
only allows a singleaccept
header value, which forces repeating the entire parameters over multiple separate decorator calls.- decorator(method: weaver.typedefs.RequestMethod, accept: str | Sequence[str] | None = None, **kwargs: Any) Callable[[weaver.typedefs.AnyViewCallable], weaver.typedefs.AnyViewCallable] [source]
Add the ability to define methods using python’s decorators syntax.
For instance, it is possible to do this with this method:
service = Service("blah", "/blah") @service.decorator("get", accept="application/json") def my_view(request): pass
- class weaver.wps_restapi.patches.ServiceOnlyExplicitGetHead(*_: Any, **__: Any)[source]
Service that disallow the auto-insertion of HTTP HEAD method view when HTTP GET view is defined.
This service overrides the default
cornice.Service
in order to avoid auto-insertion of HTTP HEAD view. Similarly topyramid
, the view registration assume that HEAD are always wanted when adding GET definitions. Because HEAD view can be added explicitly, the class also detects these cases to let them pass as expected.Without this patch, all endpoint would otherwise report erroneous HEAD requests in the generated OpenAPI specification once HEAD is removed from
cornice_swagger.CorniceSwagger.ignore_methods
.See also
HEAD method removed from ignored methods in
weaver.wps_restapi.api.get_openapi_json()
.HEAD method auto-insertion disabled for
pyramid
inpatch_pyramid_view_no_auto_head_get_method()
.
- add_view(method: str | Tuple[str], view: Any, **kwargs: Any) None [source]
Add a view to a method and arguments.
All the
Service
keyword params except name and path can be overwritten here. Additionally,api()
has following keyword params:- Parameters:
method – The request method. Should be one of ‘GET’, ‘POST’, ‘PUT’, ‘DELETE’, ‘OPTIONS’, ‘TRACE’, or ‘CONNECT’.
view – the view to hook to
**kwargs –
additional configuration for this view, including permission.
- class weaver.wps_restapi.patches.WeaverService(name, path=None, description=None, cors_policy=None, depth=1, pyramid_route=None, **kw)[source]
Service that combines all respective capabilities required by
weaver
.
- class weaver.wps_restapi.patches.RequestMethodPredicateNoGetHead(val: str | Tuple[str], config: Configurator)[source]
- weaver.wps_restapi.patches.patch_pyramid_view_no_auto_head_get_method(config: Configurator) None [source]
Replace predicate handlers automatically adding HTTP HEAD route/view when HTTP GET are defined by ones that doesn’t.