Skip to main content

backend.resolvers

Resolver functions for Spock

BaseResolver Objects#

class BaseResolver(ABC)

Base class for resolvers

Contains base methods for handling resolver syntax

Attributes:

  • _annotation_set - current set of supported resolver annotations

__init__#

def __init__()

Init for BaseResolver class

resolve#

@abstractmethoddef resolve(value: Any, value_type: _T, **kwargs) -> Tuple[Any, Optional[str]]

Resolves a variable from a given resolver syntax

Arguments:

  • value - current value to attempt to resolve
  • value_type - type of the value to cast into

Returns:

Tuple of correctly typed resolved variable and any annotations

_handle_default#

@staticmethoddef _handle_default(value: str) -> Tuple[str, Union[str, None]]

Handles setting defaults if allowed for a resolver

Arguments:

  • value - current string value

Returns:

tuple of given value and the default value

_check_full_match#

@staticmethoddef _check_full_match(full_regex_op: Pattern, value: Any) -> bool

Check if the value passed into the resolver matches the compiled regex op

Arguments:

  • full_regex_op - the full compiled regex
  • value - the value passed into the resolver

Returns:

boolean if there is a regex match

_attempt_cast#

@staticmethoddef _attempt_cast(maybe_env: Optional[str], value_type: _T, ref_value: str) -> Any

Attempts to cast the resolved variable into the given type

Arguments:

  • maybe_env - possible resolved variable
  • value_type - type to cast into
  • ref_value - the reference to the resolved variable

Returns:

value type cast into the correct type

Raises:

_SpockResolverError if it cannot be cast into the specified type

attempt_cast#

def attempt_cast(maybe_env: Optional[str], value_type: _T, ref_value: str) -> Any

Attempts to cast the resolved variable into the given type

Public version

Arguments:

  • maybe_env - possible resolved variable
  • value_type - type to cast into
  • ref_value - the reference to the resolved variable

Returns:

value type cast into the correct type

Raises:

_SpockResolverError if it cannot be cast into the specified type

_regex_clip#

def _regex_clip(end_regex_op: Pattern, clip_regex_op: Pattern, value: str, allow_default: bool, allow_annotation: bool) -> Tuple[str, str, Optional[str]]

Applies the front and back regexes to the string value, determines defaults and annotations

Arguments:

  • end_regex_op - compiled regex for the back half of the match
  • clip_regex_op - compiled regex for the front half of the match
  • full_regex_op - compiled full regex
  • value - current string value to resolve
  • allow_default - if allowed to contain default value syntax
  • allow_annotation - if allowed to contain annotation syntax

Returns:

tuple containing the resolved string reference, the default value, and the annotation string

Raises:

_SpockResolverError if annotation isn't within the supported set, annotation is not supported, multiple , values are used, or defaults are given yet not supported

_apply_regex#

def _apply_regex(end_regex_op: Pattern, clip_regex_op: Pattern, full_regex_op: Pattern, value: str, allow_default: bool, allow_annotation: bool, allow_multiple: bool = False) -> List[Tuple[str, str, Optional[str]]]

Applies the front and back regexes to the string value, determines defaults and annotations

Arguments:

  • end_regex_op - compiled regex for the back half of the match
  • clip_regex_op - compiled regex for the front half of the match
  • full_regex_op - compiled full regex
  • value - current string value to resolve
  • allow_default - if allowed to contain default value syntax
  • allow_annotation - if allowed to contain annotation syntax

Returns:

List of tuples containing the resolved string reference, the default value, and the annotation string

Raises:

_SpockResolverError if annotation isn't within the supported set, annotation is not supported, multiple , values are used, or defaults are given yet not supported

VarResolver Objects#

class VarResolver(BaseResolver)

Class for resolving references to other variable definitions

This needs to happen post instantiation of dependencies

Attributes:

  • _annotation_set - current set of supported resolver annotations
  • CLIP_VAR_PATTERN - regex for the front half
  • CLIP_REGEX_OP - compiled regex for front half
  • END_VAR_PATTERN - regex for back half
  • END_REGEX_OP - compiled regex for back half
  • FULL_VAR_PATTERN - full regex pattern
  • FULL_REGEX_OP - compiled regex for full regex

__init__#

def __init__()

Init for EnvResolver

detect#

def detect(value: Any, value_type: _T) -> bool

Detects regex matches

Arguments:

  • value - current value
  • value_type - current type

Returns:

bool if matched or not

get_regex_match_reference#

def get_regex_match_reference(value: Any) -> List[Tuple[str, str, Optional[str]]]

Applies the regex to a vlue and returns the matches

Arguments:

  • value - current value to match against

Returns:

List of tuples containing the resolved string reference, the default value, and the annotation string

EnvResolver Objects#

class EnvResolver(BaseResolver)

Class for resolving environmental variables

Attributes:

  • _annotation_set - current set of supported resolver annotations
  • CLIP_ENV_PATTERN - regex for the front half
  • CLIP_REGEX_OP - compiled regex for front half
  • END_ENV_PATTERN - regex for back half
  • END_REGEX_OP - compiled regex for back half
  • FULL_ENV_PATTERN - full regex pattern
  • FULL_REGEX_OP - compiled regex for full regex

__init__#

def __init__()

Init for EnvResolver

_get_from_env#

@staticmethoddef _get_from_env(default_value: Optional[str], env_value: str) -> Optional[str]

Gets a value from an environmental variable

Arguments:

  • default_value - default value to fall back on for the env resolver
  • env_value - current string of the env variable to get

Returns:

string or None for the resolved env variable

Raises:

_SpockResolverError if the env variable is not available or if no default is specified

CryptoResolver Objects#

class CryptoResolver(BaseResolver)

Class for resolving cryptographic variables

Attributes:

  • _annotation_set - current set of supported resolver annotations
  • CLIP_CRYPTO_PATTERN - regex for the front half
  • CLIP_REGEX_OP - compiled regex for front half
  • END_ENV_PATTERN - regex for back half
  • END_REGEX_OP - compiled regex for back half
  • FULL_ENV_PATTERN - full regex pattern
  • FULL_REGEX_OP - compiled regex for full regex
  • _salt - current cryptographic salt
  • _key - current cryptographic key

__init__#

def __init__(salt: str, key: ByteString)

Init for CryptoResolver

Arguments:

  • salt - cryptographic salt to use
  • key - cryptographic key to use