weaver.utils¶
Module Contents¶
-
weaver.utils.get_weaver_url(container: weaver.typedefs.AnySettingsContainer) → str[source]¶ Retrieves the home URL of the weaver application.
-
weaver.utils.get_any_id(info: weaver.typedefs.JSON) → Union[str, None][source]¶ Retrieves a dictionary id-like key using multiple common variations
[id, identifier, _id]. :param info: dictionary that potentially contains an id-like key. :returns: value of the matched id-like key orNoneif not found.
-
weaver.utils.get_any_value(info: weaver.typedefs.JSON) → weaver.typedefs.AnyValue[source]¶ Retrieves a dictionary value-like key using multiple common variations
[href, value, reference]. :param info: dictionary that potentially contains a value-like key. :returns: value of the matched value-like key orNoneif not found.
-
weaver.utils.get_any_message(info: weaver.typedefs.JSON) → str[source]¶ Retrieves a dictionary ‘value’-like key using multiple common variations [message]. :param info: dictionary that potentially contains a ‘message’-like key. :returns: value of the matched ‘message’-like key or an empty string if not found.
-
weaver.utils.get_registry(container: weaver.typedefs.AnyRegistryContainer, nothrow: bool = False) → Optional[Registry][source]¶ Retrieves the application
registryfrom various containers referencing to it.
-
weaver.utils.get_settings(container: Optional[AnySettingsContainer] = None) → weaver.typedefs.SettingsType[source]¶ Retrieves the application
settingsfrom various containers referencing to it.
-
weaver.utils.get_header(header_name: str, header_container: weaver.typedefs.AnyHeadersContainer) → Union[str, None][source]¶ Searches for the specified header by case/dash/underscore-insensitive
header_nameinsideheader_container.
Looks for
cookie_header_nameheader withinheader_container. :returns: new header container in the form{'Cookie': <found_cookie>}if it was matched, or empty otherwise.
-
weaver.utils.get_url_without_query(url: Union[str, ParseResult]) → str[source]¶ Removes the query string part of an URL.
-
weaver.utils.is_uuid(maybe_uuid: Any) → bool[source]¶ Evaluates if the provided input is a UUID-like string.
-
weaver.utils.parse_extra_options(option_str: str) → Dict[str, str][source]¶ Parses the extra options parameter.
The option_str is a string with coma separated
opt=valuepairs. Example:tempdir=/path/to/tempdir,archive_root=/path/to/archive
- Parameters
option_str – A string parameter with the extra options.
- Returns
A dict with the parsed extra options.
-
weaver.utils.fully_qualified_name(obj: Union[Any, Type[Any]]) → str[source]¶ Obtains the
'<module>.<name>'full path definition of the object to allow finding and importing it.
-
weaver.utils.now() → datetime.datetime[source]¶
-
weaver.utils.localize_datetime(dt: datetime.datetime, tz_name: Optional[str] = 'UTC') → datetime.datetime[source]¶ Provide a timezone-aware object for a given datetime and timezone name.
-
weaver.utils.xml_strip_ns(tree: weaver.typedefs.XML) → None[source]¶
-
weaver.utils.ows_context_href(href: str, partial: Optional[bool] = False) → weaver.typedefs.JSON[source]¶ Returns the complete or partial dictionary defining an
OWSContextfrom a reference.
-
weaver.utils.pass_http_error(exception: Exception, expected_http_error: Union[Type[PyramidHTTPError], Iterable[Type[PyramidHTTPError]]]) → None[source]¶ Given an HTTPError of any type (pyramid, requests), ignores (pass) the exception if the actual error matches the status code. Other exceptions are re-raised.
- Parameters
exception – any Exception instance (“object” from a try..except exception as “object” block).
expected_http_error – single or list of specific pyramid HTTPError to handle and ignore.
- Raises
exception – if it doesn’t match the status code or is not an HTTPError of any module.
-
weaver.utils.raise_on_xml_exception(xml_node: weaver.typedefs.XML) → Optional[NoReturn][source]¶ Raises an exception with the description if the XML response document defines an ExceptionReport. :param xml_node: instance of
XML:raise Exception: on found ExceptionReport document.
-
weaver.utils.str2bytes(string: Union[str, bytes]) → bytes[source]¶ Obtains the bytes representation of the string.
-
weaver.utils.bytes2str(string: Union[str, bytes]) → str[source]¶ Obtains the unicode representation of the string.
-
weaver.utils.parse_request_query(request: pyramid.request.Request) → Dict[str, Dict[AnyKey, str]][source]¶ - Parameters
request –
- Returns
dict of dict where k=v are accessible by d[k][0] == v and q=k=v are accessible by d[q][k] == v, lowercase
-
weaver.utils.get_path_kvp(path: str, sep: str = ',', **params: weaver.typedefs.KVP_Item) → str[source]¶ Generates the WPS URL with Key-Value-Pairs (KVP) query parameters.
- Parameters
path – WPS URL or Path
sep – separator to employ when multiple values are provided.
params – keyword parameters and their corresponding single or multi values to generate KVP.
- Returns
combined path and query parameters as KVP.
-
weaver.utils.get_log_date_fmt() → str[source]¶ Logging date format employed for job output reporting.
-
weaver.utils.get_log_monitor_msg(job_id: str, status: str, percent: weaver.typedefs.Number, message: str, location: str) → str[source]¶
-
weaver.utils.get_job_log_msg(status: str, message: str, progress: Optional[Number] = 0, duration: Optional[str] = None) → str[source]¶
-
weaver.utils.setup_loggers(settings: weaver.typedefs.AnySettingsContainer, level: Optional[Union[int, str]] = None) → None[source]¶ Update logging configuration known loggers based on application settings.
When
weaver.log_levelexists in settings, it overrides any other INI configuration logging levels. Otherwise, undefined logger levels will be set according to whichever is found first betweenweaver.log_level, thelevelparameter or defaultlogging.INFO.
-
weaver.utils.make_dirs(path, mode=493, exist_ok=False)[source]¶ Alternative to
os.makedirswithexists_okparameter only available forpython>3.5. Also using a reduced set of permissions755instead of original default777.Note
The method employed in this function is safer then
if os.pat.existsorif os.pat.isdirpre-check to callingos.makedirsas this can result in race condition (between evaluation and actual creation).
-
weaver.utils.get_caller_name(skip=2, base_class=False)[source]¶ Returns the name of a caller in the format
module.class.method.- Parameters
skip – specifies how many levels of stack to skip while getting the caller.
base_class – Specified if the base class should be returned or the top-most class in case of inheritance If the caller is not a class, this doesn’t do anything.
- Returns
An empty string if skipped levels exceed stack height; otherwise, the requested caller name.
-
weaver.utils.setup_cache(settings: weaver.typedefs.SettingsType) → None[source]¶ Prepares the settings with default caching options.
-
weaver.utils.invalidate_region(caching_args: Tuple[Callable, str, Tuple[Any]]) → None[source]¶ Caching region invalidation with handling to ignore errors generated by of unknown regions.
- Parameters
caching_args – tuple of (function, region, *function-args)
-
weaver.utils.get_ssl_verify_option(method: str, url: str, settings: weaver.typedefs.AnySettingsContainer, request_options: Optional[SettingsType] = None) → bool[source]¶ Obtains the SSL verification option from combined settings from
weaver.ssl_verifyand parsedweaver.request_optionsfile for the corresponding request.- Parameters
method – request method (GET, POST, etc.).
url – request URL.
settings – application setting container with pre-loaded request options specifications.
request_options – pre-processed request options for method/URL to avoid re-parsing the settings.
- Returns
SSL
verifyoption to be passed down to somerequestfunction.
-
weaver.utils.get_no_cache_option(request_headers: weaver.typedefs.HeadersType, request_options: weaver.typedefs.SettingsType) → bool[source]¶ Obtains the No-Cache result from request headers and configured request options.
See also
Request.headers()
- Parameters
request_headers – specific request headers that could indicate
Cache-Control: no-cacherequest_options – specific request options that could define
cache: True|False
- Returns
whether to disable cache or not
-
weaver.utils.get_request_options(method: str, url: str, settings: weaver.typedefs.AnySettingsContainer) → weaver.typedefs.SettingsType[source]¶ Obtains the request options corresponding to the request according to configuration file specified by pre-loaded setting
weaver.request_options.If no file was pre-loaded or no match is found for the request, an empty options dictionary is returned.
- Parameters
method – request method (GET, POST, etc.).
url – request URL.
settings – application setting container with pre-loaded request options specifications.
- Returns
dictionary with keyword options to be applied to the corresponding request if matched.
-
weaver.utils.retry_on_cache_error(func: Callable[[…], Any]) → Callable[source]¶ Decorator to handle invalid cache setup.
Any function wrapped with this decorator will retry execution once if missing cache setup was the cause of error.
-
weaver.utils._request_call(method: str, url: str, kwargs: Dict[str, AnyValue]) → requests.Response[source]¶ Request operation employed by
request_extra()without caching.
-
weaver.utils._request_cached(method: str, url: str, kwargs: Dict[str, AnyValue]) → requests.Response[source]¶ Cached-enabled request operation employed by
request_extra().
-
weaver.utils.request_extra(method: str, url: str, retries: Optional[int] = None, backoff: Optional[Number] = None, intervals: Optional[List[Number]] = None, retry_after: bool = True, allowed_codes: Optional[List[int]] = None, only_server_errors: bool = True, ssl_verify: Optional[bool] = None, settings: Optional[AnySettingsContainer] = None, **request_kwargs: str) → weaver.typedefs.AnyResponseType[source]¶ Standard library
requestswith additional functional utilities.Retry operation¶
Implements request retry if the previous request failed, up to the specified number of retries. Using
backofffactor, you can control the interval between request attempts such as:delay = backoff * (2 ^ retry)
Alternatively, you can explicitly define
intervals=[...]with the list values being the number of seconds to wait between each request attempt. In this case,backoffis ignored andretriesis overridden accordingly with the number of items specified in the list.Furthermore,
retry_after(default:True) indicates if HTTP status code429 (Too Many Requests)should be automatically handled during retries. If enabled and provided in the previously failed request response through theRetry-Afterheader, the next request attempt will be executed only after the server-specified delay instead of following the calculated delay fromretriesandbackoff, or from corresponding index ofinterval, accordingly to specified parameters. This will avoid uselessly calling the server and automatically receive a denied response. You can disable this feature by passingFalse, which will result into requests being retried blindly without consideration of the called server instruction.Because different request implementations use different parameter naming conventions, all following keywords are looked for:
Both variants of
backoffandbackoff_factorare accepted.All variants of
retires,retryandmax_retriesare accepted.
Note
Total amount of executed request attempts will be +1 the number of
retriesorintervalsitems as first request is done immediately, and following attempts are done with the appropriate delay.File Transport Scheme¶
Any request with
file://scheme or empty scheme (no scheme specified) will be automatically handled as potential local file path. The path should be absolute to ensure it to be correctly resolved.All access errors due to file permissions return 403 status code, and missing file returns 404. Any other
IOErrortypes are converted to a 400 responses.See also
FileAdapter
SSL Verification¶
Allows SSL verify option to be enabled or disabled according to configuration settings or explicit parameters. Any variation of
verifyorssl_verifykeyword arguments are considered. If they all resolve toTrue, then application settings are retrieved fromweaver.inito parse additional SSL options that could disable it.- Following
weaversettings are considered : weaver.ssl_verify = True|False
weaver.request_options = request_options.yml
Note
Argument
settingsmust also be provided through any supported container byget_settings()to retrieve and apply anyweaver-specific configurations.- Parameters
method – HTTP method to set request.
url – URL of the request to execute.
retries – Number of request retries to attempt if first attempt failed (according to allowed codes or error).
backoff – Factor by which to multiply delays between retries.
intervals – Explicit intervals in seconds between retries.
retry_after – If enabled, honor
Retry-Afterresponse header of provided by a failing request attempt.allowed_codes – HTTP status codes that are considered valid to stop retrying (default: any non-4xx/5xx code).
ssl_verify – Explicit parameter to disable SSL verification (overrides any settings, default: True).
settings – Additional settings from which to retrieve configuration details for requests.
only_server_errors – Only HTTP status codes in the 5xx values will be considered for retrying the request (default: True). This catches sporadic server timeout, connection error, etc., but 4xx errors are still considered valid results. This parameter is ignored if allowed codes are explicitly specified.
request_kwargs – All other keyword arguments are passed down to the request call.
-
weaver.utils.fetch_file(file_reference: str, file_outdir: str, settings: Optional[AnySettingsContainer] = None, **request_kwargs: Any) → str[source]¶ Fetches a file from a local path, an AWS-S3 bucket or remote URL, and dumps it’s content to the specified output directory.
The output directory is expected to exist prior to this function call. The file reference scheme (protocol) determines from where to fetch the content. Output file name and extension will be the same as the original. Requests will consider
weaver.request_optionswhen usinghttp(s)://scheme.- Parameters
file_reference – Local filesystem path (optionally prefixed with
file://),s3://bucket location orhttp(s)://remote URL file reference. Referencehttps://s3.[...]are also considered ass3://.file_outdir – Output local directory path under which to place the fetched file.
settings – Additional request-related settings from the application configuration (notably request-options).
request_kwargs – Additional keywords to forward to request call (if needed).
- Returns
Path of the local copy of the fetched file.
- Raises
HTTPException – applicable HTTP-based exception if any occurred during the operation.
ValueError – when the reference scheme cannot be identified.
-
weaver.utils.get_sane_name(name: str, min_len: Optional[int] = 3, max_len: Optional[Union[int, None]] = None, assert_invalid: Optional[bool] = True, replace_character: str = '_') → Union[str, None][source]¶ Returns a cleaned-up version of the
name, replacing invalid characters not matched withREGEX_SEARCH_INVALID_CHARACTERSbyreplace_character.- Parameters
name – Value to clean.
min_len – Minimal length of
name`to be respected, raises or returnsNoneon fail according toassert_invalid.max_len – Maximum length of
nameto be respected, raises or returns trimmednameon fail according toassert_invalid. IfNone, condition is ignored for assertion or fullnameis returned respectively.assert_invalid – If
True, fail conditions or invalid characters will raise an error instead of replacing.replace_character – Single character to use for replacement of invalid ones if
assert_invalidisFalse.
-
weaver.utils.assert_sane_name(name, min_len=3, max_len=None)[source]¶ Asserts that the sane name respects conditions.
See also
argument details in
get_sane_name()
-
weaver.utils.clean_json_text_body(body: str, remove_newlines: bool = True, remove_indents: bool = True) → str[source]¶ Cleans a textual body field of superfluous characters to provide a better human-readable text in a JSON response.
-
weaver.utils.transform_json(json_data: weaver.typedefs.JSON, rename: Optional[Dict[AnyKey, Any]] = None, remove: Optional[List[AnyKey]] = None, add: Optional[Dict[AnyKey, Any]] = None, replace_values: Optional[Dict[AnyKey, Any]] = None, replace_func: Optional[Dict[AnyKey, Callable[[Any], Any]]] = None) → weaver.typedefs.JSON[source]¶ Transforms the input
json_datawith different methods. The transformations are applied in the same order as the arguments.