backend.utils
Attr utility functions for Spock
#
encrypt_valuedef encrypt_value(value: Any, key: Union[str, ByteString, bytes], salt: str)
Encrypts a given value with a key and salt
Arguments:
value
- current value to encryptkey
- A URL-safe base64-encoded 32-byte key.salt
- salt to add to value
Returns:
encrypted value
#
decrypt_valuedef decrypt_value(value: Any, key: Union[str, ByteString, bytes], salt: str)
Decrypts a given value from a key and salt
Arguments:
value
- current value to decryptkey
- A URL-safe base64-encoded 32-byte key.salt
- salt to add to value
Returns:
decrypted value
#
_str_2_callabledef _str_2_callable(val: str, **kwargs)
Tries to convert a string representation of a module and callable to the reference to the callable
If the module of callable cannot be found on the PYTHONPATH then an exception is raised
Arguments:
val
- string rep of a module and callable**kwargs
- in case additional keyword args are passed
Returns:
reference to a callable
Raises:
_SpockValueError
#
_callable_2_strdef _callable_2_str(val: Callable, **kwargs)
Converts a callable to a str based on the module and name
Arguments:
val
- callable object**kwargs
- in case additional keyword args are passed
Returns:
string of module.name
#
_recurse_callablesdef _recurse_callables(val: _T, fnc: Callable, check_type: Type = str)
Recurses through objects casting any callables to the correct format
Handles both strings to callables and callables to strings depending on the function passed in
Arguments:
val
- current objectfnc
- callablecheck_type
- type to check against to verify conversion
Returns:
object with mapped callables/strings
#
_get_name_py_versiondef _get_name_py_version(typed: _T)
Gets the name of the type depending on the python version
Arguments:
typed
- the type of the parameter
Returns:
name of the type
#
get_attr_fieldsdef get_attr_fields(input_classes: List)
Gets the attribute fields from all classes
Arguments:
input_classes
- current list of input classes
Returns:
dictionary of all attrs attribute fields
#
get_type_fieldsdef get_type_fields(input_classes: List)
Creates a dictionary of names and types
Arguments:
input_classes
- list of input classes
Returns:
type_fields
- dictionary of names and types
#
flatten_type_dictdef flatten_type_dict(type_dict: Dict)
Flattens a nested dictionary
Arguments:
type_dict
- dictionary of types that are generic
Returns:
flat_dict
- flatten dictionary to a single level
#
_get_iterdef _get_iter(value: Union[List, Dict])
Returns the iterator for the type
Arguments:
value
- instance of type List or Dict
Returns:
iterator
#
convert_to_tuplesdef convert_to_tuples(input_dict: Dict, base_type_dict: Dict, flat_type_dict: Dict, class_names: List)
Convert lists to tuples
Payloads from markup come in as Lists and not Tuples. This function turns lists in to tuples for the payloads so the attr values are set correctly. Will call itself recursively if it is iterable. Handles list of classes specifically
Arguments:
input_dict
- input dictionarybase_type_dict
- dictionary of nested typesflat_type_dict
- dictionary of names with generic typesclass_names
- List of base spock class names
Returns:
updated_dict
- a dictionary with lists converted to tuples
#
deep_updatedef deep_update(source: Dict, updates: Dict)
Deeply updates a dictionary
Iterates through a dictionary recursively to update individual values within a possibly nested dictionary of dictionaries
Arguments:
source
- source dictionaryupdates
- updates to the dictionary
Returns:
source
- updated version of the source dictionary
#
_recursive_list_to_tupledef _recursive_list_to_tuple(key: str, value: Any, typed: _T, class_names: List)
Recursively turn lists into tuples
Recursively looks through a pair of value and type and sets any of the possibly nested type of value to tuple if tuple is the specified type
Arguments:
key
- name of parametervalue
- value to check and set type if necessarytyped
- type of the generic alias to check againstclass_names
- list of all spock class names
Returns:
value
- updated value with correct type casts