Skip to content

Commit

Permalink
Add a dry-run param to args validation (#913)
Browse files Browse the repository at this point in the history
* Add a dry-run param to args validation

* Update docstrings
  • Loading branch information
ramo-j authored Sep 4, 2024
1 parent f998e53 commit 5871dfb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
9 changes: 6 additions & 3 deletions dftimewolf/cli/dftimewolf_recipes.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,12 @@ def ParseArguments(self, arguments: List[str]) -> None:

self._state.command_line_options = vars(self._command_line_options)

def ValidateArguments(self) -> None:
def ValidateArguments(self, dry_run: bool=False) -> None:
"""Validate the arguments.
Args:
dry_run: True if the tool is only testing parameters, False otherwise.
Raises:
errors.CriticalError: If one or more arguments could not be validated.
"""
Expand All @@ -329,7 +332,7 @@ def ValidateArguments(self) -> None:
if argument_mandatory or argument_value is not None:
try:
valid_value = validators_manager.ValidatorsManager.Validate(
argument_value, arg)
argument_value, arg, dry_run)
self.state.command_line_options[switch] = valid_value
except errors.RecipeArgsValidationFailure as exception:
error_messages.append(
Expand Down Expand Up @@ -519,7 +522,7 @@ def RunTool(cdm: Optional[CursesDisplayManager] = None) -> int:
recipe_name)

try:
tool.ValidateArguments()
tool.ValidateArguments(tool.dry_run)
except errors.CriticalError as exception:
if cdm:
cdm.EnqueueMessage('dftimewolf', str(exception), True)
Expand Down
9 changes: 7 additions & 2 deletions dftimewolf/lib/args_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ class AbstractValidator(abc.ABC):

NAME: str = None # type: ignore

def __init__(self) -> None:
"""Initialize."""
def __init__(self, dry_run: bool=False) -> None:
"""Initialize.
Args:
dry_run: True if the tool is only testing parameters, False otherwise.
"""
self._dry_run = dry_run

@abc.abstractmethod
def Validate(self,
Expand Down
6 changes: 4 additions & 2 deletions dftimewolf/lib/validators/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,14 @@ def GetValidatorByName(
@classmethod
def Validate(cls,
argument_value: Any,
recipe_argument: resources.RecipeArgument) -> Any:
recipe_argument: resources.RecipeArgument,
dry_run: bool=False) -> Any:
"""Validate an argument value.
Args:
argument_value: The argument value to validate.
recipe_argument: The definition of the argument.
dry_run: True if the tool is only testing parameters, False otherwise.
Returns:
The validated argument value. If the recipe argument doesn't specify a
Expand All @@ -116,6 +118,6 @@ def Validate(cls,
f'{validator_name} is not a registered validator')

validator_class = cls._validator_classes[validator_name]
validator = validator_class()
validator = validator_class(dry_run)

return validator.Validate(argument_value, recipe_argument)

0 comments on commit 5871dfb

Please sign in to comment.