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
pull/189/head
Patrick Robertson 2025-02-04 13:36:05 +01:00
rodzic a873e56b87
commit b301f60ea3
3 zmienionych plików z 20 dodań i 5 usunięć

Wyświetl plik

@ -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

Wyświetl plik

@ -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
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

Wyświetl plik

@ -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,