|
| 1 | +# argparse |
| 2 | +import argparse |
| 3 | +import json |
| 4 | +import sys |
| 5 | + |
| 6 | +from file_automation.utils.exception.exception_tags import \ |
| 7 | + argparse_get_wrong_data |
| 8 | +from file_automation.utils.exception.exceptions import \ |
| 9 | + ArgparseException |
| 10 | +from file_automation.utils.executor.action_executor import execute_action |
| 11 | +from file_automation.utils.executor.action_executor import execute_files |
| 12 | +from file_automation.utils.file_process.get_dir_file_list import \ |
| 13 | + get_dir_files_as_list |
| 14 | +from file_automation.utils.json.json_file import read_action_json |
| 15 | +from file_automation.utils.project.create_project_structure import create_project_dir |
| 16 | + |
| 17 | +if __name__ == "__main__": |
| 18 | + try: |
| 19 | + def preprocess_execute_action(file_path: str): |
| 20 | + execute_action(read_action_json(file_path)) |
| 21 | + |
| 22 | + |
| 23 | + def preprocess_execute_files(file_path: str): |
| 24 | + execute_files(get_dir_files_as_list(file_path)) |
| 25 | + |
| 26 | + |
| 27 | + def preprocess_read_str_execute_action(execute_str: str): |
| 28 | + execute_str = json.loads(execute_str) |
| 29 | + execute_action(execute_str) |
| 30 | + |
| 31 | + |
| 32 | + argparse_event_dict = { |
| 33 | + "execute_file": preprocess_execute_action, |
| 34 | + "execute_dir": preprocess_execute_files, |
| 35 | + "execute_str": preprocess_read_str_execute_action, |
| 36 | + "create_project": create_project_dir |
| 37 | + } |
| 38 | + parser = argparse.ArgumentParser() |
| 39 | + parser.add_argument( |
| 40 | + "-e", "--execute_file", |
| 41 | + type=str, help="choose action file to execute" |
| 42 | + ) |
| 43 | + parser.add_argument( |
| 44 | + "-d", "--execute_dir", |
| 45 | + type=str, help="choose dir include action file to execute" |
| 46 | + ) |
| 47 | + parser.add_argument( |
| 48 | + "-c", "--create_project", |
| 49 | + type=str, help="create project with template" |
| 50 | + ) |
| 51 | + parser.add_argument( |
| 52 | + "--execute_str", |
| 53 | + type=str, help="execute json str" |
| 54 | + ) |
| 55 | + args = parser.parse_args() |
| 56 | + args = vars(args) |
| 57 | + for key, value in args.items(): |
| 58 | + if value is not None: |
| 59 | + argparse_event_dict.get(key)(value) |
| 60 | + if all(value is None for value in args.values()): |
| 61 | + raise ArgparseException(argparse_get_wrong_data) |
| 62 | + except Exception as error: |
| 63 | + print(repr(error), file=sys.stderr) |
| 64 | + sys.exit(1) |
0 commit comments