|
| 1 | +# SPDX-License-Identifier: BSD-3-Clause |
| 2 | +# Copyright (c) 2024 ScicatProject contributors (https://github.com/ScicatProject) |
| 3 | +import argparse |
| 4 | +from dataclasses import dataclass |
| 5 | +from typing import Mapping, Optional |
| 6 | + |
| 7 | + |
| 8 | +def build_main_arg_parser() -> argparse.ArgumentParser: |
| 9 | + parser = argparse.ArgumentParser() |
| 10 | + |
| 11 | + group = parser.add_argument_group('Scicat Ingestor Options') |
| 12 | + |
| 13 | + group.add_argument( |
| 14 | + '-c', |
| 15 | + '--cf', |
| 16 | + '--config', |
| 17 | + '--config-file', |
| 18 | + default='config.20240405.json', |
| 19 | + dest='config_file', |
| 20 | + help='Configuration file name. Default: config.20240405.json', |
| 21 | + type=str, |
| 22 | + ) |
| 23 | + group.add_argument( |
| 24 | + '-v', |
| 25 | + '--verbose', |
| 26 | + dest='verbose', |
| 27 | + help='Provide logging on stdout', |
| 28 | + action='store_true', |
| 29 | + default=False, |
| 30 | + ) |
| 31 | + group.add_argument( |
| 32 | + '--file-log', |
| 33 | + dest='file_log', |
| 34 | + help='Provide logging on file', |
| 35 | + action='store_true', |
| 36 | + default=False, |
| 37 | + ) |
| 38 | + group.add_argument( |
| 39 | + '--log-file-suffix', |
| 40 | + dest='log_file_suffix', |
| 41 | + help='Suffix of the log file name', |
| 42 | + default='.scicat_ingestor_log', |
| 43 | + ) |
| 44 | + group.add_argument( |
| 45 | + '--file-log-timestamp', |
| 46 | + dest='file_log_timestamp', |
| 47 | + help='Provide logging on the system log', |
| 48 | + action='store_true', |
| 49 | + default=False, |
| 50 | + ) |
| 51 | + group.add_argument( |
| 52 | + '--system-log', |
| 53 | + dest='system_log', |
| 54 | + help='Provide logging on the system log', |
| 55 | + action='store_true', |
| 56 | + default=False, |
| 57 | + ) |
| 58 | + group.add_argument( |
| 59 | + '--system-log-facility', |
| 60 | + dest='system_log_facility', |
| 61 | + help='Facility for system log', |
| 62 | + default='mail', |
| 63 | + ) |
| 64 | + group.add_argument( |
| 65 | + '--log-prefix', |
| 66 | + dest='log_prefix', |
| 67 | + help='Prefix for log messages', |
| 68 | + default=' SFI: ', |
| 69 | + ) |
| 70 | + group.add_argument( |
| 71 | + '--log-level', dest='log_level', help='Logging level', default='INFO', type=str |
| 72 | + ) |
| 73 | + group.add_argument( |
| 74 | + '--check-by-job-id', |
| 75 | + dest='check_by_job_id', |
| 76 | + help='Check the status of a job by job_id', |
| 77 | + action='store_true', |
| 78 | + default=True, |
| 79 | + ) |
| 80 | + group.add_argument( |
| 81 | + '--pyscicat', |
| 82 | + dest='pyscicat', |
| 83 | + help='Location where a specific version of pyscicat is available', |
| 84 | + default=None, |
| 85 | + type=str, |
| 86 | + ) |
| 87 | + return parser |
| 88 | + |
| 89 | + |
| 90 | +@dataclass |
| 91 | +class RunOptions: |
| 92 | + config_file: str |
| 93 | + verbose: bool |
| 94 | + file_log: bool |
| 95 | + log_file_suffix: str |
| 96 | + file_log_timestamp: bool |
| 97 | + system_log: bool |
| 98 | + system_log_facility: str |
| 99 | + log_prefix: str |
| 100 | + log_level: str |
| 101 | + check_by_job_id: bool |
| 102 | + pyscicat: Optional[str] = None |
| 103 | + |
| 104 | + |
| 105 | +@dataclass |
| 106 | +class ScicatConfig: |
| 107 | + original_dict: Mapping |
| 108 | + """Original configuration dictionary in the json file.""" |
| 109 | + run_options: RunOptions |
| 110 | + """Merged configuration dictionary with command line arguments.""" |
| 111 | + |
| 112 | + |
| 113 | +def build_scicat_config(input_args: argparse.Namespace) -> ScicatConfig: |
| 114 | + """Merge configuration from the configuration file and input arguments.""" |
| 115 | + import copy |
| 116 | + import json |
| 117 | + import pathlib |
| 118 | + from types import MappingProxyType |
| 119 | + |
| 120 | + # Read configuration file |
| 121 | + if ( |
| 122 | + input_args.config_file |
| 123 | + and (config_file_path := pathlib.Path(input_args.config_file)).is_file() |
| 124 | + ): |
| 125 | + config_dict = json.loads(config_file_path.read_text()) |
| 126 | + else: |
| 127 | + config_dict = dict() |
| 128 | + |
| 129 | + # Overwrite deep-copied options with command line arguments |
| 130 | + run_option_dict: dict = copy.deepcopy(config_dict.setdefault('options', dict())) |
| 131 | + for arg_name, arg_value in vars(input_args).items(): |
| 132 | + if arg_value is not None: |
| 133 | + run_option_dict[arg_name] = arg_value |
| 134 | + |
| 135 | + # Protect original configuration by making it read-only |
| 136 | + for key, value in config_dict.items(): |
| 137 | + config_dict[key] = MappingProxyType(value) |
| 138 | + |
| 139 | + # Wrap configuration in a dataclass |
| 140 | + return ScicatConfig( |
| 141 | + original_dict=MappingProxyType(config_dict), |
| 142 | + run_options=RunOptions(**run_option_dict), |
| 143 | + ) |
0 commit comments