Skip to main content

backend.utils

Attr utility functions for Spock

encrypt_value#

def 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 encrypt
  • key - A URL-safe base64-encoded 32-byte key.
  • salt - salt to add to value

Returns:

encrypted value

decrypt_value#

def 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 decrypt
  • key - A URL-safe base64-encoded 32-byte key.
  • salt - salt to add to value

Returns:

decrypted value

_str_2_callable#

def _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_str#

def _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_callables#

def _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 object
  • fnc - callable
  • check_type - type to check against to verify conversion

Returns:

object with mapped callables/strings

_get_name_py_version#

def _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_fields#

def 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_fields#

def 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_dict#

def 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_iter#

def _get_iter(value: Union[List, Dict])

Returns the iterator for the type

Arguments:

  • value - instance of type List or Dict

Returns:

iterator

convert_to_tuples#

def 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 dictionary
  • base_type_dict - dictionary of nested types
  • flat_type_dict - dictionary of names with generic types
  • class_names - List of base spock class names

Returns:

  • updated_dict - a dictionary with lists converted to tuples

deep_update#

def 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 dictionary
  • updates - updates to the dictionary

Returns:

  • source - updated version of the source dictionary

_recursive_list_to_tuple#

def _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 parameter
  • value - value to check and set type if necessary
  • typed - type of the generic alias to check against
  • class_names - list of all spock class names

Returns:

  • value - updated value with correct type casts