weaver.utils

Module Contents

weaver.utils.LOGGER[source]
weaver.utils.SUPPORTED_FILE_SCHEMES[source]
weaver.utils.FILE_NAME_QUOTE_PATTERN[source]
weaver.utils.FILE_NAME_LOOSE_PATTERN[source]
weaver.utils.UUID_PATTERN[source]
class weaver.utils._Singleton[source]
class weaver.utils.NullType[source]

Represents a null value to differentiate from None.

weaver.utils.null[source]
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, pop: bool = False, key: bool = False)Union[str, None][source]

Retrieves a dictionary id-like key using multiple common variations [id, identifier, _id].

Parameters
  • info – dictionary that potentially contains an id-like key.

  • pop – If enabled, remove the matched key from the input mapping.

  • key – If enabled, return the matched key instead of the value.

Returns

value of the matched id-like key or None if not found.

weaver.utils.get_any_value(info: weaver.typedefs.JSON, default: Any = None, file: bool = True, data: bool = True, pop: bool = False, key: bool = False)weaver.typedefs.AnyValueType[source]

Retrieves a dictionary value-like key using multiple common variations [href, value, reference, data].

Parameters
  • info – Dictionary that potentially contains a value-like key.

  • default – Default value to be returned if none of the known keys were matched.

  • file – If enabled, file-related key names will be considered.

  • data – If enabled, data-related key names will be considered.

  • pop – If enabled, remove the matched key from the input mapping.

  • key – If enabled, return the matched key instead of the value.

Returns

Value (or key if requested) of the matched value-like key or None if not found.

weaver.utils.get_any_message(info: weaver.typedefs.JSON)str[source]

Retrieves a dictionary ‘value’-like key using multiple common variations [message].

Parameters

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: Optional[weaver.typedefs.AnyRegistryContainer] = None, nothrow: bool = False)Optional[pyramid.registry.Registry][source]

Retrieves the application registry from various containers referencing to it.

weaver.utils.get_settings(container: Optional[weaver.typedefs.AnySettingsContainer] = None)weaver.typedefs.SettingsType[source]

Retrieves the application settings from various containers referencing to it.

weaver.utils.get_header(header_name: str, header_container: weaver.typedefs.AnyHeadersContainer, pop: bool = False)Union[str, None][source]

Searches for the specified header by case/dash/underscore-insensitive header_name inside header_container.

Looks for cookie_header_name header within header_container.

Returns

new header container in the form {'Cookie': <found_cookie>} if it was matched, or empty otherwise.

weaver.utils.parse_kvp(query: Ellipsis, key_value_sep: str = '=', pair_sep: str = ';', nested_pair_sep: Optional[str] = '', multi_value_sep: Optional[str] = ',', accumulate_keys: bool = True, unescape_quotes: bool = True, strip_spaces: bool = True, case_insensitive: bool = True)weaver.typedefs.KVP[source]

Parse key-value pairs using specified separators.

All values are normalized under a list, whether their have an unique or multi-value definition. When a key is by itself (without separator and value), the resulting value will be an empty list.

When accumulate_keys is enabled, entries such as {key}={val};{key}={val} will be joined together under the same list as if they were specified using directly {key}={val},{val} (default separators employed only for demonstration purpose). Both nomenclatures can also be employed simultaneously.

When nested_pair_sep is provided, definitions that contain nested key_value_sep character within an already established KVP will be parsed once again. This will parse {key}={subkey1}={val1},{subkey2}={val2} into a nested KVP dictionary as value under the top level KVP entry {key}. Separators are passed down for nested parsing, except pair_sep that is replaced by nested_pair_sep.

>> parse_kvp("format=json&inputs=key1=value1;key2=val2,val3", pair_sep="&", nested_pair_sep=";")
{
    'format': ['json'],
    'inputs': {
        'key1': ['value1'],
        'key2': ['val2', 'val3']
    }
}
Parameters
  • query – Definition to be parsed as KVP.

  • key_value_sep – Separator that delimitates the keys from their values.

  • pair_sep – Separator that distinguish between different (key, value) entries.

  • nested_pair_sep – Separator to parse values of pairs containing nested KVP definition.

  • multi_value_sep – Separator that delimitates multiple values associated to the same key. If empty or None, values will be left as a single entry in the list under the key.

  • accumulate_keys – Whether replicated keys should be considered equivalent to multi-value entries.

  • unescape_quotes – Whether to remove single and double quotes around values.

  • strip_spaces – Whether to remove spaces around values after splitting them.

  • case_insensitive – Whether to consider keys as case-insensitive. If True, resulting keys will be normalized to lowercase. Otherwise, original keys are employed.

Returns

Parsed KVP.

Raises

HTTPBadRequest – If parsing cannot be accomplished based on parsing conditions.

weaver.utils.parse_prefer_header_execute_mode(header_container: Ellipsis, supported_modes: Optional[List[weaver.execute.AnyExecuteControlOption]] = None, wait_max: int = 10)Tuple[weaver.execute.AnyExecuteMode, Optional[int], weaver.typedefs.HeadersType][source]

Obtain execution preference if provided in request headers.

See also

Parameters
  • header_container – Request headers to retrieve preference, if any available.

  • supported_modes – Execute modes that are permitted for the operation that received the Prefer header. Resolved mode will respect this constrain following specification requirements of OGC API - Processes.

  • wait_max – Maximum wait time enforced by the server. If requested wait time is greater, ‘wait’ preference will not be applied and will fallback to asynchronous response.

Returns

Tuple of resolved execution mode, wait time if specified, and header of applied preferences if possible. Maximum wait time indicates duration until synchronous response should fallback to asynchronous response.

Raises

HTTPBadRequest – If contents of Prefer are not valid.

weaver.utils.get_url_without_query(url: Union[str, urllib.parse.ParseResult])str[source]

Removes the query string part of an URL.

weaver.utils.is_valid_url(url: Optional[str])bool[source]
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, sep: str = ',')Dict[str, Optional[str]][source]

Parses the extra options parameter.

The option_str is a string with coma separated opt=value pairs.

tempdir=/path/to/tempdir,archive_root=/path/to/archive
Parameters
  • option_str – A string parameter with the extra options.

  • sep – separator to employ in order to split the multiple values within the option string.

Returns

A dict with the parsed extra options.

weaver.utils.fully_qualified_name(obj: Union[Any, Type[Any]])str[source]

Obtains the full path definition of the object to allow finding and importing it.

For classes, functions and exceptions, the following format is returned:

module.name

The module is omitted if it is a builtin object or type.

For methods, the class is also represented, resulting in the following format:

module.class.name
weaver.utils.now(tz_name: Optional[str] = None)datetime.datetime[source]

Obtain the current time with timezone-awareness.

Parameters

tz_name – If specified, returned current time will be localized to specified timezone.

weaver.utils.repr_json(data: Any, force_string: bool = True, **kwargs: Any)Union[weaver.typedefs.JSON, str, None][source]

Ensure that the input data can be serialized as JSON to return it formatted representation as such.

If formatting as JSON fails, returns the data as string representation or None accordingly.

weaver.utils.wait_secs(run_step: int = - 1)int[source]

Obtain a wait time in seconds within increasing delta intervals based on iteration index.

weaver.utils.localize_datetime(dt: datetime.datetime, tz_name: Optional[str] = None)datetime.datetime[source]

Provide a timezone-aware datetime for a given datetime and timezone name.

Warning

Any datetime provided as input that is not already timezone-aware will be assumed to be relative to the current locale timezone. This is the default returned by naive datetime.datetime instances.

If no timezone name is provided, the timezone-aware datatime will be localized with locale timezone offset. Otherwise, the desired localization will be applied with the specified timezone offset.

weaver.utils.get_file_header_datetime(dt: datetime.datetime)str[source]

Obtains the standard header datetime representation.

See also

Format of the date defined in RFC 5322#section-3.3.

weaver.utils.get_file_headers(path: str, download_headers: bool = False, content_headers: bool = False, content_type: Optional[str] = None)weaver.typedefs.HeadersType[source]

Obtain headers applicable for the provided file.

Parameters
  • path – File to describe.

  • download_headers – If enabled, add the attachment filename for downloading the file.

  • content_headers – If enabled, add Content- prefixed headers.

  • content_type – Explicit Content-Type to provide. Otherwise, use default guessed by file system.

Returns

Headers for the file.

weaver.utils.get_base_url(url: str)str[source]

Obtains the base URL from the given url.

weaver.utils.xml_path_elements(path: str)List[str][source]
weaver.utils.xml_strip_ns(tree: weaver.xml_util.XML)None[source]
weaver.utils.ows_context_href(href: str, partial: Optional[bool] = False)weaver.typedefs.JSON[source]

Retrieves the complete or partial dictionary defining an OWSContext from a reference.

weaver.utils.pass_http_error(exception: Exception, expected_http_error: Union[Type[pyramid.httpexceptions.HTTPError], Iterable[Type[pyramid.httpexceptions.HTTPError]]])None[source]

Silently ignore a raised HTTP error that matches the specified error code of the reference exception class.

Given an HTTPError of any type (pyramid, requests), ignores the exception if the actual error matches the status code. Other exceptions are re-raised. This is equivalent to capturing a specific Exception within an except block and calling pass to drop it.

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.xml_util.XML)Optional[NoReturn][source]

Raises an exception with the description if the XML response document defines an ExceptionReport.

Parameters

xml_node – instance of XML

Raises

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.islambda(func: Any)bool[source]
weaver.utils.first_cap_re[source]
weaver.utils.all_cap_re[source]
weaver.utils.get_path_kvp(path: str, sep: str = ',', **params: weaver.typedefs.KVP_Item)str[source]

Generates the 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_fmt()str[source]

Logging format employed for job output reporting.

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: Union[weaver.status.Status, str], message: str, progress: Optional[weaver.typedefs.Number] = 0, duration: Optional[str] = None)str[source]
weaver.utils.setup_loggers(settings: Ellipsis = None, level: Optional[Union[int, str]] = None, force_stdout: bool = False, message_format: Optional[str] = None, datetime_format: Optional[str] = None, log_file: Optional[str] = None)logging.Logger[source]

Update logging configuration known loggers based on application settings.

When weaver.log_level exists in settings, it overrides any other INI configuration logging levels. Otherwise, undefined logger levels will be set according to whichever is found first between weaver.log_level, the level parameter or default logging.INFO.

weaver.utils.make_dirs(path: str, mode: int = 493, exist_ok: bool = False)None[source]

Backward compatible make_dirs with reduced set of default mode flags.

Alternative to os.makedirs with exists_ok parameter only available for python>3.5. Also, using a reduced set of permissions 755 instead of original default 777.

Note

The method employed in this function is safer then if os.pat.exists or if os.pat.isdir pre-check to calling os.makedirs as this can result in race condition (between evaluation and actual creation).

weaver.utils.get_caller_name(skip=2, base_class=False)[source]

Find the name of a parent caller function or method.

The name is returned with respective formats module.class.method or module.function.

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) representing caching key to invalidate.

weaver.utils.get_ssl_verify_option(method: str, url: str, settings: weaver.typedefs.AnySettingsContainer, request_options: Optional[weaver.typedefs.SettingsType] = None)bool[source]

Obtains the SSL verification option considering multiple setting definitions and the provided request context.

Obtains the SSL verification option from combined settings from weaver.ssl_verify and parsed weaver.request_options file 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 verify option to be passed down to some request function.

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

Parameters
  • request_headers – specific request headers that could indicate Cache-Control: no-cache

  • request_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 from the configuration file.

The configuration file specified is expected to be pre-loaded within 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[[Ellipsis], 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, weaver.typedefs.AnyValueType])requests.Response[source]

Request operation employed by request_extra() without caching.

weaver.utils._request_cached(method: str, url: str, kwargs: Dict[str, weaver.typedefs.AnyValueType])requests.Response[source]

Cached-enabled request operation employed by request_extra().

weaver.utils.request_extra(method: Ellipsis, url: str, retries: Optional[int] = None, backoff: Optional[weaver.typedefs.Number] = None, intervals: Optional[List[weaver.typedefs.Number]] = None, retry_after: bool = True, allowed_codes: Optional[List[int]] = None, only_server_errors: bool = True, ssl_verify: Optional[bool] = None, settings: Optional[weaver.typedefs.AnySettingsContainer] = None, **request_kwargs)weaver.typedefs.AnyResponseType[source]

Standard library requests with additional functional utilities.

Retry operation

Implements request retry if the previous request failed, up to the specified number of retries. Using backoff factor, 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, backoff is ignored and retries is overridden accordingly with the number of items specified in the list.

Furthermore, retry_after (default: True) indicates if HTTP status code 429 (Too Many Requests) should be automatically handled during retries. If enabled and provided in the previously failed request response through the Retry-After header, the next request attempt will be executed only after the server-specified delay instead of following the calculated delay from retries and backoff, or from corresponding index of interval, accordingly to specified parameters. This will avoid uselessly calling the server and automatically receive a denied response. You can disable this feature by passing False, 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 backoff and backoff_factor are accepted.

  • All variants of retires, retry and max_retries are accepted.

Note

Total amount of executed request attempts will be +1 the number of retries or intervals items 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 IOError types 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 verify or ssl_verify keyword arguments are considered. If they all resolve to True, then application settings are retrieved from weaver.ini to parse additional SSL options that could disable it.

Following weaver settings are considered :
  • weaver.ssl_verify = True|False

  • weaver.request_options = request_options.yml

Note

Argument settings must also be provided through any supported container by get_settings() to retrieve and apply any weaver-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-After response 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.download_file_http(file_reference: str, file_outdir: str, settings: Optional[weaver.typedefs.AnySettingsContainer] = None, **request_kwargs: Any)str[source]

Downloads the file referenced by an HTTP URL location.

Respects RFC 2183, RFC 5987 and RFC 6266 regarding Content-Disposition header handling to resolve any preferred file name. This value is employed if it fulfill validation criteria. Otherwise, the name is extracted from the last part of the URL path.

Parameters
  • file_reference – HTTP URL where the file is hosted.

  • file_outdir – Output local directory path under which to place the downloaded 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 unrecoverable problem occurred during fetch request.

  • ValueError – when resulting file name value is considered invalid.

weaver.utils.fetch_file(file_reference: str, file_outdir: str, settings: Optional[weaver.typedefs.AnySettingsContainer] = None, link: Optional[bool] = None, move: bool = False, **request_kwargs: Any)str[source]

Fetches a file from local path, AWS-S3 bucket or remote URL, and dumps it’s content to the 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 (after link resolution if applicable). Requests will consider weaver.request_options when using http(s):// scheme.

Parameters
  • file_reference – Local filesystem path (optionally prefixed with file://), s3:// bucket location or http(s):// remote URL file reference. Reference https://s3.[...] are also considered as s3://.

  • 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).

  • link – If True, force generation of a symbolic link instead of hard copy, regardless if source is a file or link. If False, force hard copy of the file to destination, regardless if source is a file or link. If None (default), resolve automatically as follows. When the source is a symbolic link itself, the destination will also be a link. When the source is a direct file reference, the destination will be a hard copy of the file. Only applicable when the file reference is local.

  • move – Move local file to the output directory instead of copying or linking it. No effect if the output directory already contains the local file. No effect if download must occurs for remote file.

  • 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.load_file(file_path: str, text: bool = False)Union[weaver.typedefs.JSON, str][source]

Load JSON or YAML file contents from local path or remote URL.

If URL, get the content and validate it by loading, otherwise load file directly.

Parameters
  • file_path – Local path or URL endpoint where file to load is located.

  • text – load contents as plain text rather than parsing it from JSON/YAML.

Returns

loaded contents either parsed and converted to Python objects or as plain text.

Raises

ValueError – if YAML or JSON cannot be parsed or loaded from location.

weaver.utils.is_remote_file(file_location: str)bool[source]

Parses to file location to figure out if it is remotely available or a local path.

weaver.utils.REGEX_SEARCH_INVALID_CHARACTERS[source]
weaver.utils.REGEX_ASSERT_INVALID_CHARACTERS[source]
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]

Cleans up the name to allow only specified characters and conditions.

Returns a cleaned-up version of the name, replacing invalid characters not matched with REGEX_SEARCH_INVALID_CHARACTERS by replace_character. Also, ensure that the resulting name respects specified length conditions.

Parameters
  • name – Value to clean.

  • min_len – Minimal length of name` to be respected, raises or returns None on fail according to assert_invalid.

  • max_len – Maximum length of name to be respected, raises or returns trimmed name on fail according to assert_invalid. If None, condition is ignored for assertion or full name is 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_invalid is False.

weaver.utils.assert_sane_name(name: str, min_len: int = 3, max_len: Optional[int] = None)None[source]

Asserts that the sane name respects conditions.

See also

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: Ellipsis, rename: Optional[Dict[weaver.typedefs.AnyKey, Any]] = None, remove: Optional[List[weaver.typedefs.AnyKey]] = None, add: Optional[Dict[weaver.typedefs.AnyKey, Any]] = None, replace_values: Optional[Dict[weaver.typedefs.AnyKey, Any]] = None, replace_func: Optional[Dict[weaver.typedefs.AnyKey, Callable[[Any], Any]]] = None)Dict[str, weaver.typedefs.JSON][source]

Transforms the input JSON with different methods.

The transformations are applied in-place and in the same order as the arguments (rename, remove, add, etc.). All operations are applied onto the top-level fields of the mapping. No nested operations are applied, unless handled by replace functions.

Note

Because fields and values are iterated over the provided mappings, replacements of previous iterations could be re-replaced by following ones if the renamed item corresponds to a following item to match. For example, renaming field1 -> field2 and field2 -> field3` within the same operation type would result in successive replacements with ``field3 as result. The parameter order is important in this case as swapping the definitions would not find field2 on the first iteration (not in mapping yet), and then find field1, making the result to be field2.

Parameters
  • json_data – JSON mapping structure to transform.

  • rename – rename matched fields key name to the associated value name.

  • remove – remove matched fields by name.

  • add – add or override the fields names with associated values.

  • replace_values – replace matched values by the associated new values regardless of field names.

  • replace_func – Replace values under matched fields by name with the returned value from the associated function. Mapping functions will receive the original value as input. If the result is to be serialized to JSON, they should return a valid JSON-serializable value.

Returns

transformed JSON (same as modified in-place input JSON).