|
11 | 11 | PRECICE_CONFIG_FILE_NAME: str = "precice-config.xml" |
12 | 12 | GENERATED_DIR_NAME: str = "_generated" |
13 | 13 | LOG_DIR_NAME: str = ".logs" |
14 | | -DEFAULT_TOPOLOGY_NAME:str = "topology.yaml" |
| 14 | +DEFAULT_TOPOLOGY_NAME: str = "topology.yaml" |
15 | 15 |
|
16 | 16 |
|
17 | | - |
18 | | -def validate_args(args: argparse.Namespace) -> int: |
| 17 | +def yaml_file(filepath: str) -> Path: |
19 | 18 | """ |
20 | | - Validate the arguments passed to the CLI. |
21 | | - This checks if the input file exists and is a YAML file. |
22 | | - :param args: The parsed arguments. |
23 | | - :return: 0 if the arguments are valid, 1 otherwise. |
| 19 | + Check if the filepath points to an existing YAML file. |
| 20 | + Otherwise, raise an argparse.ArgumentTypeError. |
| 21 | + :param filepath: The path to the input file as a string. |
| 22 | + :return: The path to the input file as a Path object. |
24 | 23 | """ |
25 | | - logger.debug(f"Arguments parsed. Arguments: {vars(args)}. Checking if given file exists.") |
26 | | - |
27 | | - input_file: Path = Path(args.input_file).resolve() |
| 24 | + input_file = Path(filepath).resolve() |
28 | 25 |
|
29 | 26 | # Check if the file exists |
30 | 27 | if not input_file.is_file(): |
31 | 28 | logger.critical(f"File {input_file.resolve()} does not exist. Aborting program.") |
32 | | - return 1 |
| 29 | + raise argparse.ArgumentTypeError(f"File '{input_file.resolve()}' does not exist.") |
33 | 30 | logger.debug(f"File {input_file.resolve()} exists.") |
34 | 31 |
|
35 | 32 | # Check if the file is a YAML file |
36 | | - if input_file.suffix.lower() in [".yaml", ".yml"]: |
37 | | - logger.debug(f"File {input_file.resolve()} is a YAML file.") |
38 | | - else: |
| 33 | + if input_file.suffix.lower() not in [".yaml", ".yml"]: |
39 | 34 | logger.critical(f"File {input_file.resolve()} is not a YAML file. Aborting program.") |
40 | | - return 1 |
41 | | - return 0 |
| 35 | + raise argparse.ArgumentTypeError(f"The file '{input_file}' is not a YAML file.") |
| 36 | + logger.debug(f"File {input_file.resolve()} is a YAML file.") |
| 37 | + |
| 38 | + return input_file |
0 commit comments