backend.validators
Handles custom attr validators
_check_instance#
def _check_instance(value: Any, name: str, type: type) -> NoneMimics instance_of validator from attrs library
Arguments:
value- current valuename- attribute nametype- type to test against
Raises:
TypeError- if instance is not of the correct type
Returns:
None
_is_file#
def _is_file(type: _T, check_access: bool, attr: attr.Attribute, value: str) -> NoneChecks to verify that a file exists and if flagged that there are correct permissions on the file
Private version of the method
Arguments:
type- type to test againstcheck_access- checks if r/w on fileattr- current attribute being validatedvalue- current value trying to be set as the attribute
Raises:
ValueError- If the file path is not a valid filePermissionError- If the file does not have r/w permissions
Returns:
None
_IsFileValidator Objects#
@attr.attrs(repr=False, slots=True, hash=True)class _IsFileValidator()Attr style validator for checking if a path is a file
Attributes:
type- current type to check againstcheck_access- flag to check r/w permissions
__call__#
def __call__(inst: _C, attr: attr.Attribute, value: str) -> NoneOverloading call method
Arguments:
inst- current class object being builtattr- current attribute being validatedvalue- current value trying to be set as the attribute
Returns:
None
is_file#
def is_file(type: _T, check_access: bool = True) -> _IsFileValidatorA validator that raises exceptions if the file path isn't a valid file or if missing correct r/w privs
Arguments:
type- current type to check againstcheck_access- flag to check r/w permissions
Returns:
_IsFileValidator object
_is_directory#
def _is_directory(type: _T, create: bool, check_access: bool, attr: attr.Attribute, value: str) -> NoneArguments:
type- type to test against create:check_access- checks if r/w on directoryattr- current attribute being validatedvalue- current value trying to be set as the attribute
Raises:
ValueError- if the given path isn't a directoryPermissionError- if the given path cannot be created or if missing the correct r/w permissions
Returns:
None
_IsDirectoryValidator Objects#
@attr.attrs(repr=False, slots=True, hash=True)class _IsDirectoryValidator()Attr style validator for checking if a path is a directory
Attributes:
type- current type to check againstcreate- flag to attempt to create directory if it doesn't existcheck_access- flag to check r/w permissions
__call__#
def __call__(inst: _C, attr: attr.Attribute, value: str) -> NoneOverloading call method
Arguments:
inst- current class object being builtattr- current attribute being validatedvalue- current value trying to be set as the attribute
Returns:
None
is_directory#
def is_directory(type: _T, create: bool = True, check_access: bool = True) -> _IsDirectoryValidatorA validator that raises exceptions if the path isn't a valid directory, if missing correct r/w privs, or if the directory cannot be created
Arguments:
type- current type to check againstcreate- flag to attempt to create directory if it doesn't existcheck_access- flag to check r/w permissions
Returns:
_IsDirectoryValidator object
_InstanceOfValidator Objects#
@attr.attrs(repr=False, slots=True, hash=True)class _InstanceOfValidator()Attr style validator for handling instance checks
This handles the underlying new types (directory and path) that type check in a different manner than normal -- thus we essentially shim the underlying attr validator with our own to catch the extra cases we need to
Attributes:
type- current type to check against
__call__#
def __call__(inst: _C, attr: attr.Attribute, value: Any) -> NoneOverloading call method
Arguments:
inst- current class object being builtattr- current attribute being validatedvalue- current value trying to be set as the attribute
Returns:
None
instance_of#
def instance_of(type: _T) -> _InstanceOfValidatorA validator that verifies that the type is correct
Arguments:
type- current type to check against
Returns:
class of _InstanceOfValidator
_IsLenValidator Objects#
@attr.attrs(repr=False, slots=True, hash=True)class _IsLenValidator()Attr style validator for handling exact length checks
Attributes:
length- length value to check against
__call__#
def __call__(inst: _C, attr: attr.Attribute, value: Union[List, Tuple]) -> NoneOverloading call method
Arguments:
inst- current class object being builtattr- current attribute being validatedvalue- current value trying to be set as the attribute
Returns:
None
is_len#
def is_len(length: int)A validator that makes sure the input length matches what was specified
Arguments:
length- length value to check against
_OrderedIsInstanceDeepIterable Objects#
@attr.attrs(repr=False, slots=True, hash=True)class _OrderedIsInstanceDeepIterable()Attr style validator for handling instance checks in a deep iterable that is ordered
This handles creating instance validators for deep iterables that have an ordered nature -- mainly tuples. Since we need to march in the correct order of the given types we have to overload the IsInstance class with new one that handles recursing on its own
Attributes:
ordered_types- ordered iterator of the requested typesrecurse_callable- callable function that allows for recursing to create validators in the deep iterable objectiterable_validator- validator on the iterable
__call__#
def __call__(inst: _C, attr: attr.Attribute, value: Union[List[Type], Tuple[Type, ...]])Overloading call method
Arguments:
inst- current class object being builtattr- current attribute being validatedvalue- current value trying to be set as the attribute
Returns:
None
ordered_is_instance_deep_iterable#
def ordered_is_instance_deep_iterable(ordered_types: Tuple[Type, ...], recurse_callable, iterable_validator)A validator that makes sure the deep iterable matches the requested types in the given order
Arguments:
ordered_types- ordered iterator of the requested typesrecurse_callable- callable function that allows for recursing to create validators in the deep iterable objectiterable_validator- validator on the iterable
_in_type#
def _in_type(instance, attribute, value, options)attrs validator for class type enum
Checks if the type of the class (e.g. value) is in the specified set of types provided. Also checks if the value is specified via the Enum definition
Arguments:
instance- current object instanceattribute- current attribute instancevalue- current value trying to be set in the attrs instanceoptions- list, tuple, or enum of allowed options