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() and pyramid.config.Configurator.end() as well as pyramid.config.Configurator.commit().

with Configurator(settings=settings) as config:
    config.add_route('home', '/')
    app = config.make_wsgi_app()

If the registry argument is not None, it must be an instance of the pyramid.registry.Registry class representing the registry to configure. If registry is None, the configurator will create a pyramid.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 of package.

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 a renderer argument, into absolute paths. If None is passed (the default), the package is assumed to be the Python package in which the caller of the Configurator 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 main Configurator was created. If None is passed (the default), the root_package will be derived from the package argument. The package attribute is always pointing at the package being included when using include(), whereas the root_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 the pyramid.registry.Registry.settings attribute (aka request.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 is None, 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 is None (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 to pyramid.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 the logging.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 is None, 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 is None, 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 is None, 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, the session_factory will be used to create a session object when request.session is accessed. Note that the same outcome can be achieved by calling pyramid.config.Configurator.set_session_factory(). By default, this argument is None, indicating that no session factory will be configured (and thus accessing request.session will throw an error) unless set_session_factory is called later during configuration.

If autocommit is True, every method called on the configurator will cause an immediate action, and no configuration conflict detection will be used. If autocommit is False, most methods of the configurator will defer their action until pyramid.config.Configurator.commit() is called. When pyramid.config.Configurator.commit() is called, the actions implied by the called methods will be checked for configuration conflicts unless autocommit is True. If a conflict is detected, a ConfigurationConflictError will be raised. Calling pyramid.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 (see pyramid.interfaces.IViewMapperFactory). If default_view_mapper is not passed, a superdefault view mapper will be used.

If exceptionresponse_view is passed, it must be a view callable or None. If it is a view callable, it will be used as an exception view callable when an exception response is raised. If exceptionresponse_view is None, no exception response view will be registered, and all raised exception responses will be bubbled up to Pyramid’s caller. By default, the pyramid.httpexceptions.default_exceptionresponse_view function is used as the exceptionresponse_view.

If route_prefix is passed, all routes added with pyramid.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’s True, introspection values during actions will be kept for use for tools like the debug toolbar. If it’s False, introspection values provided by registrations will be ignored. By default, it is True.

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. The response_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 supplied route_prefix.

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.

allow_once = False[source]
append(__object: str | Tuple[str, Any, Any]) None[source]

Append object to the end of the list.

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 multiple accept headers provided all at once.

The base CorniceService only allows a single accept 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 to pyramid, 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

defined_methods[source]
definitions[source]
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]
val[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.