deckhand.common.utils module¶
-
deckhand.common.utils.
deepfilter
(dct, **filters)[source]¶ Match
dct
against all the filters infilters
.Check whether
dct
matches all the fitlers infilters
. The filters can reference nested attributes, attributes that are contained within other dictionaries withindct
.Useful for querying whether
metadata.name
ormetadata.layeringDefinition.layerOrder
match specific values.Parameters: - dct (dict) – The dictionary to check against all the
filters
. - filters (dict) – Dictionary of key-value pairs used for filtering out unwanted results.
Returns: True if the dictionary satisfies all the filters, else False.
- dct (dict) – The dictionary to check against all the
-
deckhand.common.utils.
jsonpath_parse
(data, jsonpath, match_all=False)[source]¶ Parse value in the data for the given
jsonpath
.Retrieve the nested entry corresponding to
data[jsonpath]
. For example, ajsonpath
of “.foo.bar.baz” means that the data section should conform to:--- foo: bar: baz: <data_to_be_extracted_here>
Parameters: - data – The data section of a document.
- jsonpath – A multi-part key that references a nested path in
data
. - match_all – Whether to return all matches or just the first one.
Returns: Entry that corresponds to
data[jsonpath]
if present, else None.Example:
src_name = sub['src']['name'] src_path = sub['src']['path'] src_doc = db_api.document_get(schema=src_schema, name=src_name) src_secret = utils.jsonpath_parse(src_doc['data'], src_path) # Do something with the extracted secret from the source document.
-
deckhand.common.utils.
jsonpath_replace
(data, value, jsonpath, pattern=None, recurse=None, src_pattern=None, src_match_group=0)[source]¶ Update value in
data
at the path specified byjsonpath
.If the nested path corresponding to
jsonpath
isn’t found indata
, the path is created as an empty{}
for each sub-path along thejsonpath
.Example:
doc = { 'data': { 'some_url': http://admin:INSERT_PASSWORD_HERE@svc-name:8080/v1 } } secret = 'super-duper-secret' path = '$.some_url' pattern = 'INSERT_[A-Z]+_HERE' replaced_data = utils.jsonpath_replace( doc['data'], secret, path, pattern) # The returned URL will look like: # http://admin:super-duper-secret@svc-name:8080/v1 doc['data'].update(replaced_data)
Parameters: - data – The
data
section of a document. - value – The new value for
data[jsonpath]
. - jsonpath – A multi-part key that references a nested path in
data
. Must begin with “.” or “$” (without quotes). - pattern – A regular expression pattern.
- recurse – Dictionary containing a single key called “depth” which
specifies the recursion depth. If provided, indicates that recursive
pattern substitution should be performed, beginning at
jsonpath
. Best practice is to limit the scope of the recursion as much as possible: e.g. avoid passing in “$” as thejsonpath
, but rather a JSON path that lives closer to the nested strings in question. Optimize performance by choosing an idealdepth
value; -1 will cause recursion depth to be infinite. - src_pattern – An optional regular expression pattern to apply to the
source
value
. The pattern is applied using re.search(), and may include parenthesized subgroups. Only the matched portion ofvalue
is considered when substituting into the destination document. - src_match_group – The numbered subgroup of the
src_pattern
match to use as the substitution source, where 0 (the default) represents the entire match, 1 is the first parenthesized subgroup, etc.
Returns: Updated value at
data[jsonpath]
.Raises: MissingDocumentPattern if
pattern
is not None anddata[jsonpath]
doesn’t exist.Raises: ValueError – If
jsonpath
doesn’t begin with “.”- data – The
-
deckhand.common.utils.
multisort
(data, sort_by=None, order_by=None)[source]¶ Sort a dictionary by multiple keys.
The order of the keys is important. The first key takes precedence over the second key, and so forth.
Parameters: - data – Dictionary to be sorted.
- sort_by (list or string) – list or string of keys to sort
data
by.
Returns: Sorted dictionary by each key.
-
deckhand.common.utils.
redact_document
(document)[source]¶ Redact
data
andsubstitutions
sections fordocument
.Parameters: document (dict) – Document whose data to redact. Returns: Document with redacted data. Return type: dict
-
deckhand.common.utils.
redact_documents
(documents)[source]¶ Redact sensitive data for each document in
documents
.Sensitive data includes
data
,substitutions[n].src.path
, andsubstitutions[n].dest.path
fields.Parameters: documents (list[dict]) – List of documents whose data to redact. Returns: Documents with redacted sensitive data. Return type: list[dict]