Ejemplo n.º 1
0
def validate_scratch(scratch) -> str:
    """Returns the parameter if it's a valid scratch config entry, otherwise
        raises an exception.

        Key path is optional, non-empty string.

        :param scratch: Object to validate.

        :raises TypeError: On wrong type.

        :raises ValueError: On regex mismatch.

    """
    if not isinstance(scratch, str):
        raise TypeError(
            validation_error_message(label='scratch',
                                     value=scratch,
                                     expected=VALID_SCRATCH_DESCRIPTION,
                                     regex=VALID_SCRATCH_REGEX))

    if not __COMPILED.match(scratch):
        raise ValueError(
            validation_error_message(label='scratch',
                                     value=scratch,
                                     expected=VALID_SCRATCH_DESCRIPTION,
                                     regex=VALID_SCRATCH_REGEX))

    return scratch
Ejemplo n.º 2
0
def validate_non_negative_int(value, label: str) -> int:
    """Returns the parameter if it's a non negative int, otherwise raises
        an exception.

        :param value: Object to validate.

        :param label: Object label for error message.

        :raises TypeError: On wrong type.

        :raises ValueError: When integer is out of range.

    """
    if not isinstance(value, int):
        raise TypeError(
            validation_error_message(label=label,
                                     value=value,
                                     expected=VALID_NON_NEGATIVE_INT))

    if value < 0:
        raise ValueError(
            validation_error_message(label=label,
                                     value=value,
                                     expected=VALID_NON_NEGATIVE_INT))

    return value
Ejemplo n.º 3
0
def validate_key_path(path) -> Optional[str]:
    """Returns the parameter if it's a valid key path config entry, otherwise
        raises an exception.

        Key path is optional, non-empty string.

        :param path: Object to validate.

        :raises TypeError: On wrong type.

        :raises ValueError: On empty string.

    """
    if path is None:
        return path

    if not isinstance(path, str):
        raise TypeError(
            validation_error_message(label='path',
                                     value=path,
                                     expected=VALID_PATH_DESCRIPTION))

    if not path:
        raise ValueError(
            validation_error_message(label='path',
                                     value=path,
                                     expected=VALID_PATH_DESCRIPTION))

    return path
Ejemplo n.º 4
0
def validate_log_level(log_level) -> int:
    """Returns the parameter, if it's a valid log level, otherwise raises
        an exception.

        Valid log level is a positive integer.

        :param log_level: Object to validate.

        :raises TypeError: On wrong type.

        :raises ValueError: On negative integer.

    """
    if not isinstance(log_level, int):
        raise TypeError(validation_error_message(
            label='log_level',
            value=log_level,
            expected=VALID_LOG_LEVEL_DESCRIPTION))

    if log_level < 0:
        raise ValueError(validation_error_message(
            label='log_level',
            value=log_level,
            expected=VALID_LOG_LEVEL_DESCRIPTION))

    return log_level
Ejemplo n.º 5
0
def validate_port(port) -> int:
    """Returns the parameter if it's a valid port, otherwise raises
        an exception.

        Valid port is an integer in range(1, 2**16).

        :param port: Object to validate.

        :raises TypeError: On wrong type.

        :raises ValueError: When integer is out of range.

    """
    if not isinstance(port, int):
        raise TypeError(
            validation_error_message(label='port',
                                     value=port,
                                     expected=VALID_PORT_RANGE_DESCRIPTION))

    if port not in VALID_PORT_RANGE:
        raise ValueError(
            validation_error_message(label='port',
                                     value=port,
                                     expected=VALID_PORT_RANGE_DESCRIPTION))

    return port
Ejemplo n.º 6
0
def test_validation_message():
    assert validation_error_message(label='Label',
                                    value='Value') == "Invalid Label: 'Value'."

    assert validation_error_message(label='LLL', value='VVV',
                                    expected='EEE.') == ("Invalid LLL: 'VVV'. "
                                                         "Expected: EEE.")

    assert validation_error_message(label='LLL',
                                    value='VVV',
                                    expected='EEE.',
                                    regex=r'\s') == ("Invalid LLL: 'VVV'. "
                                                     "Expected: EEE. "
                                                     "Regex: r\"\\s\".")
Ejemplo n.º 7
0
def validate_bool(value, label: Optional[str] = None) -> bool:
    """Returns the parameter, if it's a :class:`bool`, otherwise raises
        an exception.

        :param value: Object to validate.

        :param label: Object label for error message.

        :raises TypeError: On wrong type.

    """
    if isinstance(value, bool):
        return value

    raise TypeError(validation_error_message(label=label, value=value))
Ejemplo n.º 8
0
def validate_notebook_defaults(value):
    """Returns the parameter, if it's a dict, otherwise raises an exception.

        :param value: Object to validate.

        :raises TypeError: On wrong type.

    """

    if isinstance(value, dict):
        return value

    raise TypeError(
        validation_error_message(label='notebook_defaults',
                                 value=value,
                                 expected=VALID_NOTEBOOK_DEFAULTS_DESCRIPTION))
Ejemplo n.º 9
0
def validate_setup_actions_config(setup_actions) -> SetupActionsConfigImpl:
    """Returns the parameter, if it's a :class:`.SetupActionsConfigImpl`
        instance, otherwise raises an exception.

        :param setup_actions: Object to validate.

        :raises TypeError: On wrong type.

    """

    if isinstance(setup_actions, SetupActionsConfigImpl):
        return setup_actions

    raise TypeError(
        validation_error_message(
            label='setup_actions',
            value=setup_actions,
            expected=VALID_SETUP_ACTIONS_CONFIG_DESCRIPTION))
Ejemplo n.º 10
0
def validate_hostname(hostname) -> str:
    """Returns the parameter if it's a valid hostname, otherwise raises
        an exception.

        Valid hostname is a string matching :attr:`.VALID_HOSTNAME_REGEX`.

        :param hostname: Object to validate.

        :raises ValueError: On regex mismatch.

    """
    if not __COMPILED.match(hostname):
        raise ValueError(
            validation_error_message(label='hostname',
                                     value=hostname,
                                     expected=VALID_HOSTNAME_REGEX_DESCRIPTION,
                                     regex=VALID_HOSTNAME_REGEX))
    return hostname
Ejemplo n.º 11
0
def validate_username(username) -> str:
    """Returns the parameter, if it's a valid username, otherwise raises
        an exception

        A valid username is a string matching :attr:`.VALID_USERNAME_REGEX`.

        :param username: Object to validate.

        :raises ValueError: On regex mismatch.

    """
    if not __COMPILED.match(username):
        raise ValueError(
            validation_error_message(label='username',
                                     value=username,
                                     expected=VALID_USERNAME_REGEX_DESCRIPTION,
                                     regex=VALID_USERNAME_REGEX))
    return username
Ejemplo n.º 12
0
def validate_setup_actions(value, label: Optional[str] = None) -> List[str]:
    """Returns the parameter, if it's a valid setup actions entry, otherwise
        raises an exception.

        A valid setup actions entry is a list of strings.

        :param value: Object to validate.

        :param label: Object label for error message.

        :raises TypeError: On wrong type.

    """

    if isinstance(value, list) and all([isinstance(i, str) for i in value]):
        return value

    raise TypeError(
        validation_error_message(label=label,
                                 value=value,
                                 expected=VALID_SETUP_ACTIONS_DESCRIPTION))
Ejemplo n.º 13
0
def validate_retry_config_dict(value, label: str) -> Dict[Retry, RetryConfig]:
    """Returns the parameter, if it's dict of with :class:`.Retry` keys and
        :class:`.RetryConfig' values.

        :param value: Object to validate.

        :param label: Error message label.

        :raises TypeError: On wrong type.

    """

    if isinstance(value, dict) and all([
            isinstance(key, Retry) and isinstance(val, RetryConfig)
            for key, val in value.items()
    ]):
        return value

    raise TypeError(
        validation_error_message(label=label,
                                 value=value,
                                 expected=VALID_RETRY_CONFIG_DICT_DESCRIPTION))