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
registryargument is notNone, it must be an instance of thepyramid.registry.Registryclass representing the registry to configure. IfregistryisNone, the configurator will create apyramid.registry.Registryinstance 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
registryis assigned the above-mentioned class instance, all other constructor arguments are ignored, with the exception ofpackage.If the
packageargument 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 arendererargument, into absolute paths. IfNoneis passed (the default), the package is assumed to be the Python package in which the caller of theConfiguratorconstructor lives.If the
root_packageis passed, it will propagate through the configuration hierarchy as a way for included packages to locate resources relative to the package in which the mainConfiguratorwas created. IfNoneis passed (the default), theroot_packagewill be derived from thepackageargument. Thepackageattribute is always pointing at the package being included when usinginclude(), whereas theroot_packagedoes not change.If the
settingsargument is passed, it should be a Python dictionary representing the deployment settings for this application. These are later retrievable using thepyramid.registry.Registry.settingsattribute (akarequest.registry.settings).If the
root_factoryargument 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_policyis passed, it should be an instance of a security policy or a dotted Python name to the same.If
authentication_policyis passed, it should be an instance of an authentication policy or a dotted Python name to the same.If
authorization_policyis passed, it should be an instance of an authorization policy or a dotted Python name to the same.Note
A
ConfigurationErrorwill be raised when an authorization policy is supplied without also supplying an authentication policy (authorization requires authentication).If
renderersisNone(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_loggeris 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_negotiatoris passed, it should be a locale negotiator implementation or a dotted Python name to same. See custom_locale_negotiator.If
request_factoryis 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_factoryis 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_permissionis 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_permissionisNone, 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_factoryis passed, it should be an object which implements the session factory interface. If a nondefault value is passed, thesession_factorywill be used to create a session object whenrequest.sessionis 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.sessionwill throw an error) unlessset_session_factoryis called later during configuration.If
autocommitisTrue, every method called on the configurator will cause an immediate action, and no configuration conflict detection will be used. IfautocommitisFalse, 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 unlessautocommitisTrue. If a conflict is detected, aConfigurationConflictErrorwill be raised. Callingpyramid.config.Configurator.make_wsgi_app()always implies a final commit.If
default_view_mapperis 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_mapperis not passed, a superdefault view mapper will be used.If
exceptionresponse_viewis 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_viewisNone, 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_viewfunction is used as theexceptionresponse_view.If
route_prefixis passed, all routes added withpyramid.config.Configurator.add_route()will have the specified path prepended to their pattern.If
introspectionis 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_viewargument.Added in version 1.2: The
route_prefixargument.Added in version 1.3: The
introspectionargument.Added in version 1.6: The
root_packageargument. Theresponse_factoryargument.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 multipleacceptheaders provided all at once.The base
CorniceServiceonly allows a singleacceptheader 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.Servicein 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
pyramidinpatch_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
Servicekeyword 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.