Added validators (closes #59)

This commit is contained in:
2022-05-20 10:27:55 +02:00
parent ccca904cb8
commit dac3d9c6bb
9 changed files with 119 additions and 25 deletions

View File

@@ -13,6 +13,7 @@ from cpl_core.configuration.configuration_model_abc import ConfigurationModelABC
from cpl_core.configuration.configuration_variable_name_enum import ConfigurationVariableNameEnum
from cpl_core.configuration.executable_argument import ExecutableArgument
from cpl_core.configuration.flag_argument import FlagArgument
from cpl_core.configuration.validator_abc import ValidatorABC
from cpl_core.configuration.variable_argument import VariableArgument
from cpl_core.console.console import Console
from cpl_core.console.foreground_color_enum import ForegroundColorEnum
@@ -245,7 +246,7 @@ class Configuration(ConfigurationABC):
def create_console_argument(self, arg_type: ArgumentTypeEnum, token: str, name: str, aliases: list[str],
*args, **kwargs) -> ArgumentABC:
argument = ArgumentBuilder.build_argument(arg_type, token, name, aliases, *args, *kwargs)
argument = ArgumentBuilder.build_argument(arg_type, token, name, aliases, *args, **kwargs)
self._argument_types.append(argument)
return argument
@@ -285,6 +286,18 @@ class Configuration(ConfigurationABC):
for exe in executables:
if prevent:
continue
abort = False
for validator_type in exe.validators:
validator: ValidatorABC = services.get_service(validator_type)
result = validator.validate()
abort = not result
if abort:
break
if abort:
continue
cmd: CommandABC = services.get_service(exe.executable_type)
self.add_configuration('ACTIVE_EXECUTABLE', exe.name)
cmd.execute(self._additional_arguments)

View File

@@ -2,6 +2,8 @@ from typing import Type, Optional
from cpl_core.configuration.argument_executable_abc import ArgumentExecutableABC
from cpl_core.configuration.argument_abc import ArgumentABC
from cpl_core.configuration.validator_abc import ValidatorABC
from cpl_core.console import Console
class ExecutableArgument(ArgumentABC):
@@ -11,13 +13,16 @@ class ExecutableArgument(ArgumentABC):
name: str,
aliases: list[str],
executable: Type[ArgumentExecutableABC],
prevent_next_executable: bool = False,
validators: list[Type[ValidatorABC]] = None,
console_arguments: list['ArgumentABC'] = None
):
self._executable_type = executable
self._validators = validators
self._executable: Optional[ArgumentExecutableABC] = None
ArgumentABC.__init__(self, token, name, aliases, console_arguments)
ArgumentABC.__init__(self, token, name, aliases, prevent_next_executable, console_arguments)
@property
def executable_type(self) -> type:
@@ -26,6 +31,10 @@ class ExecutableArgument(ArgumentABC):
def set_executable(self, executable: ArgumentExecutableABC):
self._executable = executable
@property
def validators(self) -> list[Type[ValidatorABC]]:
return self._validators
def run(self, args: list[str]):
r"""Executes runnable if exists
"""

View File

@@ -7,7 +7,8 @@ class FlagArgument(ArgumentABC):
token: str,
name: str,
aliases: list[str],
prevent_next_executable: bool = False,
console_arguments: list['ArgumentABC'] = None
):
ArgumentABC.__init__(self, token, name, aliases, console_arguments)
ArgumentABC.__init__(self, token, name, aliases, prevent_next_executable, console_arguments)

View File

@@ -0,0 +1,10 @@
from abc import ABC, abstractmethod
class ValidatorABC(ABC):
@abstractmethod
def __init__(self): pass
@abstractmethod
def validate(self) -> bool: pass

View File

@@ -8,12 +8,13 @@ class VariableArgument(ArgumentABC):
name: str,
aliases: list[str],
value_token: str,
prevent_next_executable: bool = False,
console_arguments: list['ArgumentABC'] = None
):
self._value_token = value_token
self._value: str = ''
ArgumentABC.__init__(self, token, name, aliases, console_arguments)
ArgumentABC.__init__(self, token, name, aliases, prevent_next_executable, console_arguments)
@property
def value_token(self) -> str: