magpie.adapter¶
Submodules¶
Attributes¶
Classes¶
Registry for OWS services. |
|
This class abstracts handling of a project's versions. |
|
A metaclass that creates a Singleton base class when called. |
|
Context handlers associated to the request/response for the Service Hook to be evaluated. |
Functions¶
|
Evaluates the specified |
|
Raises error HTTP with standardized information formatted with content type. |
|
Returns successful HTTP with standardized information formatted with content type. (see |
|
Evaluate various parameter combinations given the requested verification flags. Given a failing verification, |
|
Obtains additional content details about the |
|
Resolve known configuration file paths from settings or environment variables and process them for the application. |
|
Search in order for matched value of |
|
Generates Magpie application configuration with all utilities required for security and access control. |
|
Retrieves a dictionary of cookie names and values from distinct implementations and container types. |
|
Retrieves the 'JSON' body of a response using the property/callable according to the response's implementation. |
|
Immediately sets the logger level to avoid duplicate log outputs from the root logger and this logger when |
|
Obtains the configured Magpie URL entrypoint based on the various combinations of supported configuration settings. |
|
Retrieve application settings from a supported container. |
|
Imports a target resource from a Python script as module. |
|
|
|
Escapes necessary regex pattern characters and applies start/end-of-line control characters. |
|
Setup caching settings if not defined in configuration and enforce values if requested. |
|
Setup |
|
Setup database |
|
Verifies that a valid user authentication on the pointed |
Package Contents¶
- magpie.adapter.magpie_version = '4.1.0'¶
- class magpie.adapter.MagpieOWSSecurity(container: magpie.typedefs.AnySettingsContainer)[source]¶
Bases:
twitcher.interface.OWSSecurityInterface- _cached_request: Dict[uuid.UUID, pyramid.request.Request]¶
- _get_service_cached(service_name: magpie.typedefs.Str, request_uuid: uuid.UUID) Tuple[magpie.services.ServiceInterface, Dict[str, magpie.typedefs.AnyValue]][source]¶
Cache this method with
beakerbased on the provided caching key parameters.If the cache is not hit (expired timeout or new key entry), calls
service_factory()to retrieve the actualServiceInterfaceimplementation. Otherwise, returns the cached service to avoid SQL queries.Note
Function arguments are required to generate caching keys by which cached elements will be retrieved. Those arguments must be serializable to generate the cache key (i.e.: cannot pass a
Requestobject that contains session and other unserializable/circular references).
- get_service(request: pyramid.request.Request) magpie.services.ServiceInterface[source]¶
Obtains the service referenced by the request.
Caching is automatically handled according to configured application settings and whether the specific service name being requested was already processed recently and not expired.
- verify_request(request: pyramid.request.Request) bool[source]¶
Verify that the service request is allowed.
Added in version 3.18: Available only in
Twitcher >= 0.6.x.
- check_request(request: pyramid.request.Request) None[source]¶
Verifies if the request user has access to the targeted resource according to parent service and permissions.
If the request path corresponds to configured Twitcher proxy, evaluate the ACL. Otherwise, ignore request access validation.
In the case Twitcher proxy path is matched, the Logged User MUST be allowed access following Effective Permissions resolution via ACL. Otherwise, :exception:`OWSAccessForbidden` is raised.
Failing to parse the request or any underlying component that raises an exception will be left up to the parent caller to handle the exception. In most typical use case, this means Twitcher will raise a generic :exception:`OWSException` with
NoApplicableCode, unless the exception was more specifically handled.- Raises:
OWSAccessForbidden – If the user does not have access to the targeted resource under the service.
HTTPBadRequest – If a request parsing error was detected when trying to resolve the permission based on the service/resource.
Exception – Any derived exception that was not explicitly handled is re-raised directly after logging the event.
- Returns:
Nothing if user has access.
- update_request_cookies(request: pyramid.request.Request) None[source]¶
Ensure login of the user and update the request cookies if Twitcher is in a special configuration.
Only update if
MAGPIE_COOKIE_NAMEis missing and is retrievable fromaccess_tokenfield within theAuthorizationheader. Counter-validate the login procedure by calling Magpie’s/sessionwhich should indicate if there is a logged user.
- class magpie.adapter.MagpieServiceStore(request: pyramid.request.Request)[source]¶
Bases:
twitcher.store.ServiceStoreInterfaceRegistry for OWS services.
Uses magpie to fetch service url and attributes.
- save_service(name: magpie.typedefs.Str, url: magpie.typedefs.Str, *args: Any, **kwargs: Any) NoReturn[source]¶
Store is read-only, use Magpie API to add services.
Note
Multiple redundant parameters are applied to support different Twitcher versions.
Twitcher <=0.5.xuses(service, *args, **kwargs)Twitcher >=0.6.xuses(name, url, *args, **kwargs)Some alternate interfaces also provided extra parameters at some point.
- delete_service(name: magpie.typedefs.Str, *args: Any, **kwargs: Any) NoReturn[source]¶
Store is read-only, use
MagpieAPI to delete services.
- _fetch_by_name_cached(service_name: magpie.typedefs.Str) twitcher.models.Service[source]¶
Cache this method with
beakerbased on the provided caching key parameters.If the cache is not hit (expired timeout or new key entry), calls
fetch_by_name()to retrieve the actualtwitcher.datatype.Servicedefinition. Otherwise, returns the cached item to avoid SQL queries.Note
Function arguments are required to generate caching keys by which cached elements will be retrieved.
Note
Method
fetch_by_name()gets triggered bytwitcher.owsproxy.owsproxy_view()after successful validation of granted access for Logged User to the service / resources following call tomagpie.adapter.magpieowssecurity.MagpieOWSSecurity.check_request()in order to send and retrieve the actual response of that proxied service and forward it back to the requesting user. Caching helps greatly reduce recurrent SQL queries to convert Twitcher to Magpie service.
- fetch_by_name(name: magpie.typedefs.Str) twitcher.models.Service[source]¶
Gets
twitcher.datatype.Servicecorresponding tomagpie.models.Servicebyname.
- magpie.adapter.evaluate_call(call: Callable[[], Any], fallback: Callable[[], None] | None = None, http_error: Type[pyramid.httpexceptions.HTTPError] = HTTPInternalServerError, http_kwargs: magpie.typedefs.ParamsType | None = None, msg_on_fail: magpie.typedefs.Str = '', content: magpie.typedefs.JSON | None = None, content_type: magpie.typedefs.Str = CONTENT_TYPE_JSON, metadata: magpie.typedefs.JSON | None = None) Any[source]¶
Evaluates the specified
callwith a wrapped HTTP exception handling. On failure, tries to call.fallbackif specified, and finally raises the specifiedhttp_error.Any potential error generated by
fallbackorhttp_errorthemselves are treated asHTTPInternalServerError.Exceptions are generated using the standard output method formatted based on specified
content_type.- Example:
normal call:
try: res = func(args) except Exception as exc: fb_func() raise HTTPExcept(exc.message)
wrapped call:
res = evaluate_call(lambda: func(args), fallback=lambda: fb_func(), http_error=HTTPExcept, **kwargs)
- Parameters:
call – function to call, MUST be specified as lambda: <function_call>
fallback – function to call (if any) when call failed, MUST be lambda: <function_call>
http_error – alternative exception to raise on call failure
http_kwargs – additional keyword arguments to pass to http_error if called in case of HTTP exception
msg_on_fail – message details to return in HTTP exception if call failed
content – json formatted additional content to provide in case of exception
content_type – format in which to return the exception (one of magpie.common.SUPPORTED_ACCEPT_TYPES)
metadata – request metadata to add to the response body. (see:
magpie.api.requests.get_request_info())
- Raises:
http_error – on call failure
HTTPInternalServerError – on fallback failure
- Returns:
whichever return value call might have if no exception occurred
- magpie.adapter.raise_http(http_error: Type[pyramid.httpexceptions.HTTPError] = HTTPInternalServerError, http_kwargs: magpie.typedefs.ParamsType | None = None, detail: magpie.typedefs.Str = '', content: magpie.typedefs.JSON | None = None, content_type: magpie.typedefs.Str = CONTENT_TYPE_JSON, metadata: magpie.typedefs.JSON | None = None, nothrow: bool = False) NoReturn[source]¶
Raises error HTTP with standardized information formatted with content type.
The content contains the corresponding http error code, the provided message as detail and optional specified additional json content (kwarg dict).
See also
valid_http()for HTTP successful calls- Parameters:
http_error – any derived class from base HTTPError (default: HTTPInternalServerError)
http_kwargs – additional keyword arguments to pass to http_error if called in case of HTTP exception
detail – additional message information (default: empty)
content – JSON formatted content to include
content_type – format in which to return the exception (one of magpie.common.SUPPORTED_ACCEPT_TYPES)
metadata – request metadata to add to the response body. (see:
magpie.api.requests.get_request_info())nothrow – returns the error response instead of raising it automatically, but still handles execution errors
- Raises:
HTTPError – formatted raised exception with additional details and HTTP code
- Returns:
HTTPError formatted exception with additional details and HTTP code only if nothrow is True
- magpie.adapter.valid_http(http_success: Type[pyramid.httpexceptions.HTTPSuccessful] | Type[pyramid.httpexceptions.HTTPRedirection] = HTTPOk, http_kwargs: magpie.typedefs.ParamsType | None = None, detail: magpie.typedefs.Str | None = '', content: magpie.typedefs.JSON | None = None, content_type: magpie.typedefs.Str | None = CONTENT_TYPE_JSON, metadata: magpie.typedefs.JSON | None = None) pyramid.httpexceptions.HTTPSuccessful | pyramid.httpexceptions.HTTPRedirection[source]¶
Returns successful HTTP with standardized information formatted with content type. (see
raise_http()for HTTP error calls)- Parameters:
http_success – any derived class from valid HTTP codes (<400) (default: HTTPOk)
http_kwargs – additional keyword arguments to pass to http_success when called
detail – additional message information (default: empty)
content – json formatted content to include
content_type – format in which to return the exception (one of magpie.common.SUPPORTED_ACCEPT_TYPES)
metadata – request metadata to add to the response body. (see:
magpie.api.requests.get_request_info())
- Returns:
formatted successful response with additional details and HTTP code
- magpie.adapter.verify_param(param: Any, param_compare: Any | List[Any] | None = None, param_name: magpie.typedefs.Str | None = None, param_content: magpie.typedefs.JSON | None = None, with_param: bool = True, http_error: Type[pyramid.httpexceptions.HTTPError] = HTTPBadRequest, http_kwargs: magpie.typedefs.ParamsType | None = None, msg_on_fail: magpie.typedefs.Str = '', content: magpie.typedefs.JSON | None = None, content_type: magpie.typedefs.Str = CONTENT_TYPE_JSON, metadata: magpie.typedefs.JSON | None = None, not_none: bool = False, not_empty: bool = False, not_in: bool = False, not_equal: bool = False, is_true: bool = False, is_false: bool = False, is_none: bool = False, is_empty: bool = False, is_in: bool = False, is_equal: bool = False, is_type: bool = False, matches: bool = False) None[source]¶
Evaluate various parameter combinations given the requested verification flags. Given a failing verification, directly raises the specified
http_error. Invalid usage exceptions generated by this verification process are treated asHTTPInternalServerError. Exceptions are generated using the standard output method.- Parameters:
param – parameter value to evaluate
param_compare – Other value(s) to test
paramagainst. Can be an iterable (single value resolved as iterable unlessNone). To test forNonetype, useis_none/not_noneflags instead.param_name – name of the tested parameter returned in response if specified for debugging purposes
param_content – Additional JSON content to apply to generated error content on raise when
with_paramisTrue. Must be JSON serializable. Provided content can override generated error parameter if matching fields.with_param – On raise, adds values of
param,param_nameandparam_compare, as well as additional failing conditions metadata to the JSON response body for each of the corresponding value.http_error – derived exception to raise on test failure (default:
HTTPBadRequest)http_kwargs – additional keyword arguments to pass to
http_errorcalled in case of HTTP exceptionmsg_on_fail – message details to return in HTTP exception if flag condition failed
content – json formatted additional content to provide in case of exception
content_type – format in which to return the exception (one of
magpie.common.SUPPORTED_ACCEPT_TYPES)metadata – request metadata to add to the response body. (see:
magpie.api.requests.get_request_info())not_none – test that
paramis notNonetypenot_empty – test that
paramis not an empty iterable (string, list, set, etc.)not_in – test that
paramdoes not exist inparam_comparevaluesnot_equal – test that
paramis not equal toparam_comparevalueis_true – test that
paramisTrueis_false – test that
paramisFalseis_none – test that
paramisNonetypeis_empty – test param for an empty iterable (string, list, set, etc.)
is_in – test that
paramexists inparam_comparevaluesis_equal – test that
paramequalsparam_comparevalueis_type – test that
paramis of same type as specified byparam_comparetypematches – test that
parammatches the regex specified byparam_comparevalue
- Raises:
HTTPError – if tests fail, specified exception is raised (default:
HTTPBadRequest)HTTPInternalServerError – for evaluation error
- Returns:
nothing if all tests passed
- magpie.adapter.get_request_info(request: pyramid.request.Request | pyramid.httpexceptions.HTTPException, default_message: magpie.typedefs.Str | None = None, exception_details: bool = False) magpie.typedefs.JSON[source]¶
Obtains additional content details about the
requestaccording to available information.
- magpie.adapter.setup_magpie_configs(settings: magpie.typedefs.SettingsType, db_session: sqlalchemy.orm.session.Session | None = None, setup_providers: bool = True, setup_permissions: bool = True, setup_webhooks: bool = True, skip_registration: bool = False) None[source]¶
Resolve known configuration file paths from settings or environment variables and process them for the application.
See also
https://pavics-magpie.readthedocs.io/en/latest/configuration.html#file-providers-cfg
https://pavics-magpie.readthedocs.io/en/latest/configuration.html#file-permissions-cfg
https://pavics-magpie.readthedocs.io/en/latest/configuration.html#configuration-file-formats
https://pavics-magpie.readthedocs.io/en/latest/configuration.html#combined-configuration-file
- class magpie.adapter.LooseVersion(version: str)[source]¶
Bases:
packaging.version.Version,VersionInterfaceThis class abstracts handling of a project’s versions.
A
Versioninstance is comparison aware and can be compared and sorted using the standard Python interfaces.>>> v1 = Version("1.0a5") >>> v2 = Version("1.0") >>> v1 <Version('1.0a5')> >>> v2 <Version('1.0')> >>> v1 < v2 True >>> v1 == v2 False >>> v1 > v2 False >>> v1 >= v2 False >>> v1 <= v2 True
Initialize a Version object.
- Parameters:
version – The string representation of a version which will be parsed and normalized before use.
- Raises:
InvalidVersion – If the
versiondoes not conform to PEP 440 in any way then this exception will be raised.
- property version: Tuple[int | str, Ellipsis]¶
- property patch¶
- _cmp(other: LooseVersion | str) int[source]¶
- magpie.adapter.get_constant(constant_name: magpie.typedefs.Str, settings_container: magpie.typedefs.AnySettingsContainer | None = None, settings_name: magpie.typedefs.Str | None = None, default_value: magpie.typedefs.SettingValue | None = None, raise_not_set: bool = True, raise_missing: bool = True, print_missing: bool = False, empty_missing: bool = False) magpie.typedefs.SettingValue[source]¶
- Search in order for matched value of
constant_name: search in
MAGPIE_CONSTANTSsearch in settings if specified
search alternative setting names (see below)
search in
magpie.constantsdefinitionssearch in environment variables
Parameter
constant_nameis expected to have the formatMAGPIE_[VARIABLE_NAME]although any value can be passed to retrieve generic settings from all above-mentioned search locations.If
settings_nameis provided as alternative name, it is used as is to search for results ifconstant_namewas not found. Otherwise,magpie.[variable_name]is used for additional search when the formatMAGPIE_[VARIABLE_NAME]was used forconstant_name(i.e.:MAGPIE_ADMIN_USERwill also search formagpie.admin_userand so on for corresponding constants).- Parameters:
constant_name – key to search for a value
settings_container – WSGI application settings container (if not provided, uses found one in current thread)
settings_name – alternative name for settings if specified
default_value – default value to be returned if not found anywhere, and exception raises are disabled.
raise_not_set – raise an exception if the found key is
None, search until last case if others areNoneraise_missing – raise exception if key is not found anywhere
print_missing – print message if key is not found anywhere, return
Noneempty_missing – consider an empty value for an existing key as if it was missing (i.e.: as if not set).
- Returns:
found value or default_value
- Raises:
ValueError – if resulting value is invalid based on options (by default raise missing/empty/
Nonevalue)LookupError – if no appropriate value could be found from all search locations (according to options)
- Search in order for matched value of
- magpie.adapter.get_auth_config(container: magpie.typedefs.AnySettingsContainer) pyramid.config.Configurator[source]¶
Generates Magpie application configuration with all utilities required for security and access control.
- class magpie.adapter.SingletonMeta[source]¶
Bases:
typeA metaclass that creates a Singleton base class when called.
Create a class such that:
@six.add_metaclass(SingletonMeta) class A(object): pass @six.add_metaclass(SingletonMeta) class B(object): pass a1 = A() a2 = A() b1 = B() b2 = B() a1 is a2 # True b1 is b2 # True a1 is b1 # False
- _instances¶
- magpie.adapter.get_cookies(request_or_response: magpie.typedefs.AnyRequestType | magpie.typedefs.AnyResponseType) magpie.typedefs.CookiesType[source]¶
Retrieves a dictionary of cookie names and values from distinct implementations and container types.
- Parameters:
request_or_response – Object that potentially contains cookies, by literal property or corresponding headers.
- Returns:
Found cookies.
- magpie.adapter.get_json(request_or_response: magpie.typedefs.AnyRequestType | magpie.typedefs.AnyResponseType) magpie.typedefs.JSON[source]¶
Retrieves the ‘JSON’ body of a response using the property/callable according to the response’s implementation.
- magpie.adapter.get_logger(name: magpie.typedefs.Str, level: int | None = None, force_stdout: bool = None, message_format: magpie.typedefs.Str | None = None, datetime_format: magpie.typedefs.Str | None = None) logging.Logger[source]¶
Immediately sets the logger level to avoid duplicate log outputs from the root logger and this logger when level is
logging.NOTSET.
- magpie.adapter.get_magpie_url(container: magpie.typedefs.AnySettingsContainer | None = None) magpie.typedefs.Str[source]¶
Obtains the configured Magpie URL entrypoint based on the various combinations of supported configuration settings.
See also
Documentation section Application Settings for available setting combinations.
- Parameters:
container – container that provides access to application settings.
- Returns:
resolved Magpie URL
- magpie.adapter.get_settings(container: magpie.typedefs.AnySettingsContainer | None, app: bool = False) magpie.typedefs.SettingsType[source]¶
Retrieve application settings from a supported container.
- Parameters:
container – supported container with a handle to application settings.
app – allow retrieving from current thread registry if no container was defined.
- Returns:
found application settings dictionary.
- Raises:
TypeError – when no application settings could be found or unsupported container.
- magpie.adapter.import_target(target: magpie.typedefs.Str, default_root: magpie.typedefs.Str | None = None) Any | None[source]¶
Imports a target resource from a Python script as module.
The Python script does not need to be defined within a module directory (i.e.: with
__init__.py). Files can be imported from virtually anywhere. To avoid name conflicts in generated module references, each imported target employs its full escaped file path as module name.Format expected as follows:
"path/to/script.py:function"- Parameters:
target – Resource to be imported.
default_root – Root directory to employ if target is relative (default
magpie.constants.MAGPIE_ROOT).
- Returns:
Found and imported resource or None.
- magpie.adapter.normalize_field_pattern(pattern: magpie.typedefs.Str, escape: bool = True) magpie.typedefs.Str[source]¶
Escapes necessary regex pattern characters and applies start/end-of-line control characters.
- magpie.adapter.setup_cache_settings(settings: magpie.typedefs.SettingsType, force: bool = False, enabled: bool = False, expire: int = 0) None[source]¶
Setup caching settings if not defined in configuration and enforce values if requested.
Required caching regions that were missing from configuration are initiated with disabled caching by default. This ensures that any decorators or region references will not cause unknown key or region errors.
- Parameters:
settings – reference to application settings that will be updated in place.
force – enforce following parameters, otherwise only ensure that caching regions exist.
enabled – enable caching when enforced settings is requested.
expire – cache expiration delay if setting values are enforced and enabled.
- magpie.adapter.setup_pyramid_config(config: pyramid.config.Configurator) None[source]¶
Setup
Pyramidutilities in the application configuration to define expected object references.
- magpie.adapter.setup_session_config(config: pyramid.config.Configurator) None[source]¶
Setup database
Sessiontransaction handlers andRequestproperties for active User.
- magpie.adapter.verify_user(request: pyramid.request.Request) pyramid.httpexceptions.HTTPException[source]¶
Verifies that a valid user authentication on the pointed
Magpieinstance (via configuration) also results into a valid user authentication with the currentTwitcherinstance to ensure settings match between them.- Parameters:
request – an HTTP request with valid authentication token/cookie credentials.
- Returns:
appropriate HTTP success or error response with details about the result.
- class magpie.adapter.MagpieAdapter(container: magpie.typedefs.AnySettingsContainer)[source]¶
Bases:
twitcher.adapter.base.AdapterInterface- servicestore_factory(request: pyramid.request.Request) magpieservice.MagpieServiceStore[source]¶
- abstract tokenstore_factory(request: pyramid.request.Request) twitcher.store.AccessTokenStoreInterface[source]¶
Unused token store implementation.
Changed in version 3.18: Available only in
Twitcher <= 0.5.x.
- owsregistry_factory(request: pyramid.request.Request) twitcher.owsregistry.OWSRegistry[source]¶
Creates the
OWSRegistryimplementation derived fromMagpieServiceStore.Added in version 3.18: Available only in
Twitcher >= 0.6.x.
- owssecurity_factory(request: magpie.typedefs.AnySettingsContainer | None = None) magpieowssecurity.MagpieOWSSecurity[source]¶
Creates the
OWSSecurityimplementation derived fromMagpieOWSSecurity.Changed in version 3.18: Method
requestdoes not exist starting inTwitcher >= 0.6.x.
- configurator_factory(container: magpie.typedefs.AnySettingsContainer) pyramid.config.Configurator[source]¶
- _apply_hooks(instance: pyramid.request.Request | pyramid.response.Response, service_name: magpie.typedefs.Str, hook_type: magpie.typedefs.ServiceHookType, method: magpie.typedefs.Str, path: magpie.typedefs.Str, query: magpie.typedefs.Str) pyramid.request.Request | pyramid.response.Response[source]¶
Executes the hooks processing chain.
- static _proxied_service_path(request: pyramid.request.Request) magpie.typedefs.Str[source]¶
Extract the request extra path of the proxied service without Twitcher proxy prefix.
- request_hook(request: pyramid.request.Request, service: twitcher.models.service.ServiceConfig) pyramid.request.Request[source]¶
Apply modifications onto the request before sending it.
Added in version 3.25: Requires
Twitcher >= 0.7.x.Request members employed after this hook is called include: -
Request.headers()-Request.method()-Request.body()This method can modified those members to adapt the request for specific service logic.
- response_hook(response: pyramid.response.Response, service: twitcher.models.service.ServiceConfig) pyramid.response.Response[source]¶
Apply modifications onto the response from sent request.
Added in version 3.25: Requires
Twitcher >= 0.7.x.The received response from the proxied service is normally returned directly. This method can modify the response to adapt it for specific service logic.
- send_request(request: pyramid.request.Request, service: twitcher.models.service.ServiceConfig) pyramid.response.Response[source]¶
Send the request to the proxied service and handle its response.
Added in version 3.31: Requires
Twitcher >= 0.8.x.Because requests are sometimes handled using
requestsdepending on the service type, therequestreference in the producedresponseis not always set. Ensure it is applied to allowresponse_hook()retrieving it when attempting to resolve the proxied service path.
- class magpie.adapter.HookContext(instance: pyramid.request.Request | pyramid.response.Response, adapter: MagpieAdapter, config: magpie.typedefs.ServiceConfigItem, hook: magpie.typedefs.ServiceHookConfigItem)[source]¶
Bases:
objectContext handlers associated to the request/response for the Service Hook to be evaluated.
Dispatch calls to database or other derived operations on demand as much as possible to minimize the impact of retrieving some parameters by each request that requires this hook context.
- adapter: MagpieAdapter = None[source]¶
- _service: magpie.services.ServiceInterface | None = None[source]¶
- property service: magpie.services.ServiceInterface[source]¶
Service implementation that defines the Resource structure and Permission resolution method.
- property resource: magpie.models.Resource[source]¶
Database Resource that represents the Service reference implementation.