Skip to content

Commit 6e859fb

Browse files
authored
Merge pull request #11 from JE-Chen/dev
refactor and add test case add timeout
2 parents e11ffcf + 7abbd2a commit 6e859fb

23 files changed

+230
-153
lines changed

.circleci/config.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
# keyboard test
2626
- run:
2727
command: python ./circle_ci_test/keyboard/keyboard_type_test.py
28-
name: keyboard_test
28+
name: keyboard_type_test
2929
- run:
3030
command: python ./circle_ci_test/keyboard/keyboard_write_test.py
3131
name: keyboard_write_test
@@ -75,6 +75,10 @@ jobs:
7575
- run:
7676
command: python ./circle_ci_test/json/json_test.py
7777
name: json_test
78+
# timeout
79+
- run:
80+
command: python ./circle_ci_test/timeout/timeout_test.py
81+
name: timeout_test
7882

7983
workflows:
8084
main:

.idea/Python_JEAutoControl.iml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/discord.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 97 additions & 47 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from itertools import count
2+
from time import sleep
3+
4+
from je_auto_control import multiprocess_timeout
5+
6+
counter = count(1)
7+
8+
9+
def time_not_out_function():
10+
print("Hello")
11+
12+
13+
def time_out_test_function():
14+
while True:
15+
sleep(1)
16+
print(next(counter))
17+
18+
19+
if __name__ == "__main__":
20+
print(multiprocess_timeout(time_not_out_function, 5))
21+
print(multiprocess_timeout(time_out_test_function, 5))

je_auto_control/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,6 @@
5353
from je_auto_control.utils.action_executer.action_executor import execute_action
5454
from je_auto_control.utils.action_file.json_file import read_action_json
5555
from je_auto_control.utils.action_file.json_file import write_action_json
56+
57+
# timeout
58+
from je_auto_control.utils.timeout.multiprocess_timeout import multiprocess_timeout
Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,45 @@
1-
"""
2-
error tags
3-
"""
1+
# error tags
42
je_auto_control_error = "Auto control error"
53
je_auto_control_critical_exit_error = "Auto control critical exit error"
6-
"""
7-
os tags
8-
"""
4+
# os tags
95
linux_import_error = "should be only loaded on linux"
106
osx_import_error = "should be only loaded on MacOS"
117
windows_import_error = "should be only loaded on windows"
12-
"""
13-
keyboard tags
14-
"""
8+
macos_record_error = "macos can't use recorder"
9+
# keyboard tags
1510
keyboard_error = "Auto control keyboard error"
1611
keyboard_press_key = "keyboard press key error"
1712
keyboard_release_key = "keyboard release key error"
1813
keyboard_type_key = "keyboard type key error"
1914
keyboard_write = "keyboard write error"
2015
keyboard_write_cant_find = "keyboard write error can't find key"
2116
keyboard_hotkey = "keyboard hotkey error"
22-
"""
23-
mouse tags
24-
"""
17+
# mouse tags
2518
mouse_error = "Auto control mouse error"
2619
mouse_get_position = "mouse get position error"
2720
mouse_set_position = "mouse set position error"
2821
mouse_press_mouse = "mouse press mouse error"
2922
mouse_release_mouse = "mouse release key error"
3023
mouse_click_mouse = "mouse click mouse error"
3124
mouse_scroll = "mouse scroll error"
32-
"""
33-
screen tags
34-
"""
25+
# screen tags
3526
screen_error = "Auto control screen error"
3627
screen_get_size = "screen get size error"
3728
screen_screenshot = "screen screenshot error"
38-
"""
39-
table tags
40-
"""
29+
# table tags
4130
table_cant_find_key = "can't find key error"
42-
"""
43-
image tags
44-
"""
31+
# image tags
4532
cant_find_image = "can't find image"
4633
find_image_error_variable = "variable error"
47-
"""
48-
listener tags
49-
"""
34+
# listener tags
5035
listener_error = "Auto control listener error"
51-
"""
52-
record tags
53-
"""
36+
# record tags
5437
record_queue_error = "can't get record queue it's none are you using stop record before record"
5538
record_not_found_action_error = "record action not found"
56-
"""
57-
json action file tag
58-
"""
39+
# json action file tag
5940
cant_execute_action_error = "can't execute action"
6041
cant_find_json_error = "can't find json file"
6142
cant_save_json_error = "can't save json file"
6243
action_is_null_error = "json action is null"
63-
"""
64-
macos error tag
65-
"""
66-
macos_record_error = "macos can't use recorder"
44+
# timeout tag
45+
timeout_need_on_main_error = "should put timeout function on main"
Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
1-
"""
2-
general
3-
"""
4-
5-
1+
# general
62
class AutoControlException(Exception):
73
pass
84

95

10-
"""
11-
Keyboard
12-
"""
6+
# Keyboard
137

148

159
class AutoControlKeyboardException(AutoControlException):
@@ -20,45 +14,35 @@ class AutoControlCantFindKeyException(AutoControlException):
2014
pass
2115

2216

23-
"""
24-
Mouse
25-
"""
17+
# Mouse
2618

2719

2820
class AutoControlMouseException(AutoControlException):
2921
pass
3022

3123

32-
"""
33-
Screen
34-
"""
24+
# Screen
3525

3626

3727
class AutoControlScreenException(AutoControlException):
3828
pass
3929

4030

41-
"""
42-
Image detect
43-
"""
31+
# Image detect
4432

4533

4634
class ImageNotFoundException(AutoControlException):
4735
pass
4836

4937

50-
"""
51-
Record
52-
"""
38+
# Record
5339

5440

5541
class AutoControlRecordException(AutoControlException):
5642
pass
5743

5844

59-
"""
60-
Execute action
61-
"""
45+
# Execute action
6246

6347

6448
class AutoControlJsonActionException(AutoControlException):
@@ -71,3 +55,8 @@ class AutoControlActionNullException(AutoControlException):
7155

7256
class AutoControlActionException(AutoControlException):
7357
pass
58+
59+
60+
# timeout
61+
class AutoControlTimeoutException(AutoControlException):
62+
pass
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from je_auto_control.utils.timeout import *
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from multiprocessing import Process
2+
from je_auto_control.utils.je_auto_control_exception.exceptions import AutoControlTimeoutException
3+
from je_auto_control.utils.je_auto_control_exception.exception_tag import timeout_need_on_main_error
4+
5+
6+
def multiprocess_timeout(check_function, time: int):
7+
try:
8+
new_process = Process(target=check_function)
9+
new_process.start()
10+
new_process.join(timeout=time)
11+
except AutoControlTimeoutException:
12+
raise AutoControlTimeoutException(timeout_need_on_main_error)
13+
new_process.terminate()
14+
if new_process.exitcode is None:
15+
return "timeout"
16+
else:
17+
return "success"

je_auto_control/wrapper/auto_control_keyboard.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def press_key(keycode: [int, str], is_shift: bool = False, **kwargs):
2929
keyboard.press_key(keycode)
3030
elif sys.platform in ["darwin"]:
3131
keyboard.press_key(keycode, is_shift=is_shift)
32+
return str(keycode)
3233
except Exception:
3334
raise AutoControlKeyboardException(keyboard_press_key)
3435

@@ -48,6 +49,7 @@ def release_key(keycode: [int, str], is_shift: bool = False, **kwargs):
4849
keyboard.release_key(keycode)
4950
elif sys.platform in ["darwin"]:
5051
keyboard.release_key(keycode, is_shift=is_shift)
52+
return str(keycode)
5153
except Exception:
5254
raise AutoControlKeyboardException(keyboard_release_key)
5355

@@ -60,6 +62,7 @@ def type_key(keycode: [int, str], is_shift: bool = False, **kwargs):
6062
try:
6163
press_key(keycode, is_shift)
6264
release_key(keycode, is_shift)
65+
return str(keycode)
6366
except AutoControlKeyboardException:
6467
raise AutoControlKeyboardException(keyboard_type_key)
6568

@@ -81,14 +84,16 @@ def write(write_string: str, is_shift: bool = False, **kwargs):
8184
:param is_shift shift is press?
8285
"""
8386
try:
87+
record_write_string = ""
8488
for single_string in write_string:
8589
try:
8690
if keys_table.get(single_string) is not None:
87-
type_key(single_string, is_shift)
91+
record_write_string = "".join([record_write_string, type_key(single_string, is_shift)])
8892
else:
8993
raise AutoControlKeyboardException(keyboard_write_cant_find)
9094
except AutoControlKeyboardException:
9195
print(keyboard_write_cant_find, single_string, sep="\t", file=sys.stderr)
96+
return record_write_string
9297
except AutoControlKeyboardException:
9398
raise AutoControlKeyboardException(keyboard_write)
9499

@@ -99,10 +104,13 @@ def hotkey(key_code_list: list, is_shift: bool = False, **kwargs):
99104
:param is_shift shift is press?
100105
"""
101106
try:
107+
record_hotkey_press_string = ""
108+
record_hotkey_release_string = ""
102109
for key in key_code_list:
103-
press_key(key, is_shift)
110+
record_hotkey_press_string = ",".join([record_hotkey_press_string, press_key(key, is_shift)])
104111
key_code_list.reverse()
105112
for key in key_code_list:
106-
release_key(key, is_shift)
113+
record_hotkey_release_string = ",".join([record_hotkey_release_string, release_key(key, is_shift)])
114+
return record_hotkey_press_string, record_hotkey_release_string
107115
except AutoControlKeyboardException:
108116
raise AutoControlKeyboardException(keyboard_hotkey)

je_auto_control/wrapper/auto_control_mouse.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def set_position(x: int, y: int, **kwargs):
3131
"""
3232
try:
3333
mouse.set_position(x=x, y=y)
34+
return x, y
3435
except Exception:
3536
raise AutoControlMouseException(mouse_set_position)
3637

@@ -58,6 +59,7 @@ def press_mouse(mouse_keycode: [int, str], x: int = None, y: int = None, **kwarg
5859
mouse.press_mouse(mouse_keycode)
5960
elif sys.platform in ["darwin"]:
6061
mouse.press_mouse(x, y, mouse_keycode)
62+
return mouse_keycode, x, y
6163
except Exception:
6264
raise AutoControlMouseException(mouse_press_mouse)
6365

@@ -88,6 +90,7 @@ def release_mouse(mouse_keycode: [int, str], x: int = None, y: int = None, **kwa
8890
mouse.release_mouse(mouse_keycode)
8991
elif sys.platform in ["darwin"]:
9092
mouse.release_mouse(x, y, mouse_keycode)
93+
return mouse_keycode, x, y
9194
except Exception:
9295
raise AutoControlMouseException(mouse_release_mouse)
9396

@@ -115,6 +118,7 @@ def click_mouse(mouse_keycode: [int, str], x: int = None, y: int = None, **kwarg
115118
raise AutoControlMouseException(mouse_get_position)
116119
try:
117120
mouse.click_mouse(mouse_keycode, x, y)
121+
return mouse_keycode, x, y
118122
except Exception:
119123
raise AutoControlMouseException(mouse_click_mouse)
120124

@@ -157,5 +161,6 @@ def scroll(scroll_value: int, x: int = None, y: int = None, scroll_direction: st
157161
elif sys.platform in ["linux", "linux2"]:
158162
scroll_direction = special_table.get(scroll_direction)
159163
mouse.scroll(scroll_value, scroll_direction)
164+
return scroll_value, scroll_direction
160165
except Exception:
161166
raise AutoControlMouseException(mouse_click_mouse)

je_auto_control/wrapper/auto_control_record.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def stop_record_keyboard():
3939
def record():
4040
if sys.platform == "darwin":
4141
raise AutoControlException(macos_record_error)
42-
recorder.record()
42+
return recorder.record()
4343

4444

4545
def stop_record():
@@ -49,7 +49,7 @@ def stop_record():
4949
if action_queue is None:
5050
raise AutoControlJsonActionException
5151
action_list = list(action_queue.queue)
52-
execute_action(action_list)
52+
return execute_action(action_list)
5353

5454

5555
if __name__ == "__main__":

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name="je_auto_control",
8-
version="0.0.71",
8+
version="0.0.73",
99
author="JE-Chen",
1010
author_email="zenmailman@gmail.com",
1111
description="auto testing",

test/platform_independent/test/keyboard/hotkey_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from je_auto_control import hotkey
44

55
if sys.platform in ["win32", "cygwin", "msys"]:
6-
hotkey(["lcontrol", "a"])
6+
hotkey_event = hotkey(["lcontrol", "a"])
77
hotkey(["lcontrol", "c"])
88
hotkey(["lcontrol", "v"])
99
hotkey(["lcontrol", "v"])

0 commit comments

Comments
 (0)