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 resolvevalue_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) -> boolCheck if the value passed into the resolver matches the compiled regex op
Arguments:
full_regex_op- the full compiled regexvalue- 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) -> AnyAttempts to cast the resolved variable into the given type
Arguments:
maybe_env- possible resolved variablevalue_type- type to cast intoref_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) -> AnyAttempts to cast the resolved variable into the given type
Public version
Arguments:
maybe_env- possible resolved variablevalue_type- type to cast intoref_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 matchclip_regex_op- compiled regex for the front half of the matchfull_regex_op- compiled full regexvalue- current string value to resolveallow_default- if allowed to contain default value syntaxallow_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 matchclip_regex_op- compiled regex for the front half of the matchfull_regex_op- compiled full regexvalue- current string value to resolveallow_default- if allowed to contain default value syntaxallow_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 annotationsCLIP_VAR_PATTERN- regex for the front halfCLIP_REGEX_OP- compiled regex for front halfEND_VAR_PATTERN- regex for back halfEND_REGEX_OP- compiled regex for back halfFULL_VAR_PATTERN- full regex patternFULL_REGEX_OP- compiled regex for full regex
__init__#
def __init__()Init for EnvResolver
detect#
def detect(value: Any, value_type: _T) -> boolDetects regex matches
Arguments:
value- current valuevalue_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 annotationsCLIP_ENV_PATTERN- regex for the front halfCLIP_REGEX_OP- compiled regex for front halfEND_ENV_PATTERN- regex for back halfEND_REGEX_OP- compiled regex for back halfFULL_ENV_PATTERN- full regex patternFULL_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 resolverenv_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 annotationsCLIP_CRYPTO_PATTERN- regex for the front halfCLIP_REGEX_OP- compiled regex for front halfEND_ENV_PATTERN- regex for back halfEND_REGEX_OP- compiled regex for back halfFULL_ENV_PATTERN- full regex patternFULL_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 usekey- cryptographic key to use