weaver.utils
¶
Module Contents¶
-
class
weaver.utils.
_NullType
[source]¶ Bases:
six.with_metaclass()
Represents a
null
value to differentiate fromNone
.
-
weaver.utils.
get_weaver_url
(container) → AnyStr[source]¶ Retrieves the home URL of the weaver application.
-
weaver.utils.
get_any_id
(info) → Union[AnyStr, 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 orNone
if not found.
-
weaver.utils.
get_any_value
(info) → 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 orNone
if not found.
-
weaver.utils.
get_any_message
(info) → AnyStr[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) → Registry[source]¶ Retrieves the application
registry
from various containers referencing to it.
-
weaver.utils.
get_settings
(container) → SettingsType[source]¶ Retrieves the application
settings
from various containers referencing to it.
-
weaver.utils.
get_header
(header_name, header_container) → Union[AnyStr, None][source]¶ Searches for the specified header by case/dash/underscore-insensitive
header_name
insideheader_container
.
Looks for
cookie_header_name
header withinheader_container
. :returns: new header container in the form{'Cookie': <found_cookie>}
if it was matched, or empty otherwise.
-
weaver.utils.
parse_extra_options
(option_str)[source]¶ Parses the extra options parameter.
The option_str is a string with coma separated
opt=value
pairs. 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) → str[source]¶ Obtains the
'<module>.<name>'
full path definition of the object to allow finding and importing it.
-
weaver.utils.
localize_datetime
(dt, tz_name='UTC') → datetime[source]¶ Provide a timezone-aware object for a given datetime and timezone name
-
weaver.utils.
ows_context_href
(href, partial=False) → JSON[source]¶ Returns the complete or partial dictionary defining an
OWSContext
from a reference.
-
weaver.utils.
pass_http_error
(exception, expected_http_error) → 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)[source]¶ Raises an exception with the description if the XML response document defines an ExceptionReport. :param xml_node: instance of
etree.Element
:raise Exception: on found ExceptionReport document.
-
weaver.utils.
parse_request_query
(request) → Dict[AnyStr, Dict[AnyKey, AnyStr]][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.
make_dirs
(path, mode=493, exist_ok=False)[source]¶ Alternative to
os.makedirs
withexists_ok
parameter only available forpython>3.5
. Also using a reduced set of permissions755
instead of original default777
.Note
The method employed in this function is safer then
if os.pat.exists
orif os.pat.isdir
pre-check to callingos.makedirs
as this can result in race condition (between evaluation and actual creation).
-
weaver.utils.
request_retry
(method, url, retries=0, backoff=0, intervals=None, allowed_codes=None, **request_kwargs) → AnyResponseType[source]¶ Implements basic request retry operation if the previous request failed, up to the specified number of retries.
Using :paramref:`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, :paramref:`backoff` is ignored and :paramref:`retries` is overridden by the list size.Because different request implementations use different parameter naming conventions, all following keywords are looked for:
- Both variants of
backoff
andbackoff_factor
are accepted. - All variants of
retires
,retry
andmax_retries
are accepted.
Parameters: - method – HTTP method to set request.
- url – URL of the request to execute.
- retries – number of retries to attempt.
- backoff – factor by which to multiply delays between retries.
- intervals – explicit intervals in seconds between retries.
- allowed_codes – HTTP status codes that are considered valid to stop retrying (default: any non-4xx/5xx code).
- request_kwargs – All other keyword arguments are passed down to the request call.
- Both variants of
-
weaver.utils.
fetch_file
(file_reference, file_outdir, **request_kwargs) → AnyStr[source]¶ Fetches a file from a local path 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.
Parameters: - file_reference – Local filesystem path or remote URL file reference.
- file_outdir – Output directory path of the fetched file.
- request_kwargs – additional keywords to forward to request call (if needed).
Returns: Path of the local copy of the fetched file.
-
weaver.utils.
get_sane_name
(name, min_len=3, max_len=None, assert_invalid=True, replace_character='_') → Union[AnyStr, None][source]¶ Returns a cleaned-up version of the input name, replacing invalid characters matched with
REGEX_SEARCH_INVALID_CHARACTERS
byreplace_character
.Parameters: - name – value to clean
- min_len – Minimal length of
name
to be respected, raises or returnsNone
on fail according toassert_invalid
. - max_len – Maximum length of
name
to be respected, raises or returns trimmedname
on fail according toassert_invalid
. IfNone
, condition is ignored for assertion or fullname
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=False
.
-
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()
- argument details in