deckhand.common.utils module¶
-
deckhand.common.utils.deepfilter(dct, **filters)[source]¶ Match
dctagainst all the filters infilters.Check whether
dctmatches all the fitlers infilters. The filters can reference nested attributes, attributes that are contained within other dictionaries withindct.Useful for querying whether
metadata.nameormetadata.layeringDefinition.layerOrdermatch 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, ajsonpathof “.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
dataat the path specified byjsonpath.If the nested path corresponding to
jsonpathisn’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
datasection 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 idealdepthvalue; -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 ofvalueis considered when substituting into the destination document. - src_match_group – The numbered subgroup of the
src_patternmatch 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
patternis not None anddata[jsonpath]doesn’t exist.Raises: ValueError – If
jsonpathdoesn’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
databy.
Returns: Sorted dictionary by each key.
-
deckhand.common.utils.redact_document(document)[source]¶ Redact
dataandsubstitutionssections 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.pathfields.Parameters: documents (list[dict]) – List of documents whose data to redact. Returns: Documents with redacted sensitive data. Return type: list[dict]