Skip to content

Commit 3c833c9

Browse files
committedJul 17, 2022
update stable and dev
add exception tag executor add dict support now argparse error will sys exit
1 parent 6ed64a7 commit 3c833c9

File tree

5 files changed

+65
-32
lines changed

5 files changed

+65
-32
lines changed
 

‎.idea/workspace.xml

+17-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎je_auto_control/__main__.py

+31-16
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,45 @@
11
# argparse
22
import argparse
3+
import json
4+
import sys
35

6+
from je_auto_control.utils.exception.exception_tag import argparse_get_wrong_data
7+
from je_auto_control.utils.exception.exceptions import AutoControlArgparseException
48
from je_auto_control.utils.json.json_file import read_action_json
59
from je_auto_control.utils.file_process.get_dir_file_list import get_dir_files_as_list
610
from je_auto_control.utils.executor.action_executor import execute_action
711
from je_auto_control.utils.executor.action_executor import execute_files
812

913
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))
1217

1318

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))
1621

22+
def preprocess_read_str_execute_action(execute_str: str):
23+
execute_str = json.loads(execute_str)
24+
execute_action(execute_str)
1725

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)
3045

‎je_auto_control/utils/exception/exception_tag.py

+4
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,7 @@
4848
html_generate_no_data_tag = "record is None"
4949
# add command
5050
add_command_exception_tag = "command value type should be as method or function"
51+
# executor
52+
executor_list_error = "executor receive wrong data list is none or wrong type"
53+
# argparse
54+
argparse_get_wrong_data = "argparse receive wrong data"

‎je_auto_control/utils/exception/exceptions.py

+4
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,7 @@ class AutoControlAddCommandException(AutoControlException):
7272
pass
7373

7474

75+
class AutoControlArgparseException(AutoControlException):
76+
pass
77+
78+

‎je_auto_control/utils/executor/action_executor.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
from je_auto_control import mouse_table, check_key_is_press, position, press_mouse, release_mouse, click_mouse, scroll
77
from je_auto_control import set_position
88
from je_auto_control import screenshot, size, special_table
9-
from je_auto_control.utils.exception.exception_tag import action_is_null_error, add_command_exception_tag
9+
from je_auto_control.utils.exception.exception_tag import action_is_null_error, add_command_exception_tag, \
10+
executor_list_error
1011
from je_auto_control.utils.exception.exception_tag import cant_execute_action_error
1112
from je_auto_control.utils.exception.exceptions import AutoControlActionException, AutoControlAddCommandException
1213
from je_auto_control.utils.exception.exceptions import AutoControlActionNullException
@@ -59,15 +60,18 @@ def _execute_event(self, action: list):
5960
elif len(action) == 1:
6061
event()
6162
else:
62-
raise AutoControlActionException(cant_execute_action_error)
63+
raise AutoControlActionException(cant_execute_action_error + " " + str(action))
6364

64-
def execute_action(self, action_list: list) -> dict:
65+
def execute_action(self, action_list: [list, dict]) -> dict:
6566
"""
6667
use to execute all action on action list(action file or program list)
6768
:param action_list the list include action
6869
for loop the list and execute action
6970
"""
70-
71+
if type(action_list) is dict:
72+
action_list = action_list.get("web_runner", None)
73+
if action_list is None:
74+
raise AutoControlActionNullException(executor_list_error)
7175
execute_record_dict = dict()
7276
try:
7377
if len(action_list) > 0 or type(action_list) is list:
@@ -84,6 +88,7 @@ def execute_action(self, action_list: list) -> dict:
8488
execute_record_dict.update({execute_record: event_response})
8589
except Exception as error:
8690
print(repr(error), file=sys.stderr)
91+
print(action, file=sys.stderr)
8792
record_action_to_list("execute_action", None, repr(error))
8893
return execute_record_dict
8994

0 commit comments

Comments
 (0)