|
1 | 1 | # argparse
|
2 | 2 | import argparse
|
| 3 | +import json |
| 4 | +import sys |
3 | 5 |
|
| 6 | +from je_auto_control.utils.exception.exception_tag import argparse_get_wrong_data |
| 7 | +from je_auto_control.utils.exception.exceptions import AutoControlArgparseException |
4 | 8 | from je_auto_control.utils.json.json_file import read_action_json
|
5 | 9 | from je_auto_control.utils.file_process.get_dir_file_list import get_dir_files_as_list
|
6 | 10 | from je_auto_control.utils.executor.action_executor import execute_action
|
7 | 11 | from je_auto_control.utils.executor.action_executor import execute_files
|
8 | 12 |
|
9 | 13 | if __name__ == "__main__":
|
10 |
| - def preprocess_execute_action(file_path: str): |
11 |
| - execute_action(read_action_json(file_path)) |
| 14 | + try: |
| 15 | + def preprocess_execute_action(file_path: str): |
| 16 | + execute_action(read_action_json(file_path)) |
12 | 17 |
|
13 | 18 |
|
14 |
| - def preprocess_execute_files(file_path: str): |
15 |
| - execute_files(get_dir_files_as_list(file_path)) |
| 19 | + def preprocess_execute_files(file_path: str): |
| 20 | + execute_files(get_dir_files_as_list(file_path)) |
16 | 21 |
|
| 22 | + def preprocess_read_str_execute_action(execute_str: str): |
| 23 | + execute_str = json.loads(execute_str) |
| 24 | + execute_action(execute_str) |
17 | 25 |
|
18 |
| - argparse_event_dict = { |
19 |
| - "execute_file": preprocess_execute_action, |
20 |
| - "execute_dir": preprocess_execute_files |
21 |
| - } |
22 |
| - parser = argparse.ArgumentParser() |
23 |
| - parser.add_argument("-e", "--execute_file", type=str, help="choose action file to execute") |
24 |
| - parser.add_argument("-d", "--execute_dir", type=str, help="choose dir include action file to execute") |
25 |
| - args = parser.parse_args() |
26 |
| - args = vars(args) |
27 |
| - for key, value in args.items(): |
28 |
| - if value is not None: |
29 |
| - argparse_event_dict.get(key)(value) |
| 26 | + argparse_event_dict = { |
| 27 | + "execute_file": preprocess_execute_action, |
| 28 | + "execute_dir": preprocess_execute_files, |
| 29 | + "execute_str": preprocess_read_str_execute_action |
| 30 | + } |
| 31 | + parser = argparse.ArgumentParser() |
| 32 | + parser.add_argument("-e", "--execute_file", type=str, help="choose action file to execute") |
| 33 | + parser.add_argument("-d", "--execute_dir", type=str, help="choose dir include action file to execute") |
| 34 | + parser.add_argument("--execute_str", type=str, help="execute json str") |
| 35 | + args = parser.parse_args() |
| 36 | + args = vars(args) |
| 37 | + for key, value in args.items(): |
| 38 | + if value is not None: |
| 39 | + argparse_event_dict.get(key)(value) |
| 40 | + if all(value is None for value in args.values()): |
| 41 | + raise AutoControlArgparseException(argparse_get_wrong_data) |
| 42 | + except Exception as error: |
| 43 | + print(repr(error), file=sys.stderr) |
| 44 | + sys.exit(1) |
30 | 45 |
|
0 commit comments