From b301f60ea3ba8d8b10658ab2e5a8f592bb3e2af4 Mon Sep 17 00:00:00 2001 From: Patrick Robertson Date: Tue, 4 Feb 2025 13:36:05 +0100 Subject: [PATCH] Fix using validators set in __manifest__.py E.g. you can use the validator 'is_file' to check if a config is a valid file --- src/auto_archiver/core/orchestrator.py | 4 ++-- src/auto_archiver/core/validators.py | 18 +++++++++++++++--- .../modules/csv_feeder/__manifest__.py | 3 +++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/auto_archiver/core/orchestrator.py b/src/auto_archiver/core/orchestrator.py index dbc8a33..8a634de 100644 --- a/src/auto_archiver/core/orchestrator.py +++ b/src/auto_archiver/core/orchestrator.py @@ -207,9 +207,9 @@ class ArchivingOrchestrator: should_store = kwargs.pop('should_store', False) kwargs['dest'] = f"{module.name}.{kwargs.pop('dest', name)}" try: + kwargs['type'] = getattr(validators, kwargs.get('type', '__invalid__')) + except AttributeError: kwargs['type'] = __builtins__.get(kwargs.get('type'), str) - except KeyError: - kwargs['type'] = getattr(validators, kwargs['type']) arg = group.add_argument(f"--{module.name}.{name}", **kwargs) arg.should_store = should_store diff --git a/src/auto_archiver/core/validators.py b/src/auto_archiver/core/validators.py index 681d564..b868ddf 100644 --- a/src/auto_archiver/core/validators.py +++ b/src/auto_archiver/core/validators.py @@ -1,7 +1,19 @@ -# used as validators for config values. +# used as validators for config values. Should raise an exception if the value is invalid. +from pathlib import Path +import argparse def example_validator(value): - return "example" in value + if "example" not in value: + raise argparse.ArgumentTypeError(f"{value} is not a valid value for this argument") + return value def positive_number(value): - return value > 0 \ No newline at end of file + if value < 0: + raise argparse.ArgumentTypeError(f"{value} is not a positive number") + return value + + +def valid_file(value): + if not Path(value).is_file(): + raise argparse.ArgumentTypeError(f"File '{value}' does not exist.") + return value \ No newline at end of file diff --git a/src/auto_archiver/modules/csv_feeder/__manifest__.py b/src/auto_archiver/modules/csv_feeder/__manifest__.py index 7249395..b6d7543 100644 --- a/src/auto_archiver/modules/csv_feeder/__manifest__.py +++ b/src/auto_archiver/modules/csv_feeder/__manifest__.py @@ -13,6 +13,9 @@ "default": None, "help": "Path to the input file(s) to read the URLs from, comma separated. \ Input files should be formatted with one URL per line", + "required": True, + "type": "valid_file", + "nargs": "+", }, "column": { "default": None,