diff --git a/.idea/Python_JEAutoControl.iml b/.idea/Python_JEAutoControl.iml index d4912d6..6fab609 100644 --- a/.idea/Python_JEAutoControl.iml +++ b/.idea/Python_JEAutoControl.iml @@ -5,7 +5,7 @@ - + diff --git a/.idea/misc.xml b/.idea/misc.xml index b132445..33e9bc5 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index c9e1223..fb692ef 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -3,9 +3,7 @@ - - - + - - - + + + + - @@ -52,14 +50,15 @@ - - - - - - - - + @@ -76,8 +75,8 @@ - - + + - + @@ -377,6 +376,12 @@ + + + + + + @@ -409,90 +414,90 @@ - - - - - - + + + + + + - - - - - - - - - - - - - - - + + + + - - + - + - - - - - - - - - - - - - - + - - - - - - + - - - + + + + + - - - - - - + + + + + + + + + - - + + + + + + + + - - - - - - + + + + - - - - + + - - + + + + + + + + + + + + + + - + - + + + + + + + + + + + + \ No newline at end of file diff --git a/dev_setup.py b/dev_setup.py index 2321a34..f9eebd7 100644 --- a/dev_setup.py +++ b/dev_setup.py @@ -5,7 +5,7 @@ setuptools.setup( name="je_auto_control_dev", - version="0.0.18", + version="0.0.20", author="JE-Chen", author_email="zenmailman@gmail.com", description="auto testing", diff --git a/je_auto_control/linux_with_x11/keyboard/x11_linux_keyboard_control.py b/je_auto_control/linux_with_x11/keyboard/x11_linux_keyboard_control.py index ff690b2..5f22c1d 100644 --- a/je_auto_control/linux_with_x11/keyboard/x11_linux_keyboard_control.py +++ b/je_auto_control/linux_with_x11/keyboard/x11_linux_keyboard_control.py @@ -12,7 +12,7 @@ from Xlib import X -def press_key(keycode: int): +def press_key(keycode: int) -> None: """ :param keycode which keycode we want to press """ @@ -24,7 +24,7 @@ def press_key(keycode: int): print(repr(error), file=sys.stderr) -def release_key(keycode: int): +def release_key(keycode: int) -> None: """ :param keycode which keycode we want to release """ diff --git a/je_auto_control/linux_with_x11/listener/x11_linux_listener.py b/je_auto_control/linux_with_x11/listener/x11_linux_listener.py index cf0d27d..d1d5fce 100644 --- a/je_auto_control/linux_with_x11/listener/x11_linux_listener.py +++ b/je_auto_control/linux_with_x11/listener/x11_linux_listener.py @@ -1,4 +1,5 @@ import sys +from queue import Queue from je_auto_control.utils.exception.exceptions import AutoControlException from je_auto_control.utils.exception.exception_tag import linux_import_error @@ -38,7 +39,7 @@ def __init__(self, default_daemon: bool = True): self.event_position = 0, 0 # two times because press and release - def check_is_press(self, keycode: int): + def check_is_press(self, keycode: int) -> bool: """ :param keycode we want to check """ @@ -48,7 +49,7 @@ def check_is_press(self, keycode: int): else: return False - def run(self, reply): + def run(self, reply) -> None: """ :param reply listener return data get data @@ -70,14 +71,14 @@ def run(self, reply): except AutoControlException: raise AutoControlException(listener_error) - def record(self, record_queue): + def record(self, record_queue)-> None: """ :param record_queue the queue test_record action """ self.record_flag = True self.record_queue = record_queue - def stop_record(self): + def stop_record(self) -> Queue: self.record_flag = False return self.record_queue @@ -103,7 +104,7 @@ def check_is_press(self, keycode: int): """ return self.handler.check_is_press(keycode) - def run(self): + def run(self) -> None: """ while still listener get context @@ -139,10 +140,10 @@ def run(self): self.handler.still_listener = False self.still_listener = False - def record(self, record_queue): + def record(self, record_queue) -> None: self.handler.record(record_queue) - def stop_record(self): + def stop_record(self) -> Queue: return self.handler.stop_record() @@ -150,30 +151,23 @@ def stop_record(self): xwindows_listener.start() -def check_key_is_press(keycode: int): +def check_key_is_press(keycode: int) -> int: """ :param keycode check this keycode is press? """ return xwindows_listener.check_is_press(keycode) -def x11_linux_record(record_queue): +def x11_linux_record(record_queue) -> None: """ :param record_queue the queue test_record action """ xwindows_listener.record(record_queue) -def x11_linux_stop_record(): +def x11_linux_stop_record() -> None: """ stop test_record action """ return xwindows_listener.stop_record() - -if __name__ == "__main__": - from queue import Queue - test_queue = Queue() - xwindows_listener.record(test_queue) - while True: - pass diff --git a/je_auto_control/linux_with_x11/mouse/x11_linux_mouse_control.py b/je_auto_control/linux_with_x11/mouse/x11_linux_mouse_control.py index 28ff86b..7d720d6 100644 --- a/je_auto_control/linux_with_x11/mouse/x11_linux_mouse_control.py +++ b/je_auto_control/linux_with_x11/mouse/x11_linux_mouse_control.py @@ -1,5 +1,6 @@ import sys import time +from typing import Tuple from je_auto_control.utils.exception.exceptions import AutoControlException from je_auto_control.utils.exception.exception_tag import linux_import_error @@ -21,7 +22,7 @@ x11_linux_scroll_direction_right = 7 -def position(): +def position() -> Tuple[int, int]: """ get mouse current position """ @@ -29,7 +30,7 @@ def position(): return coord["root_x"], coord["root_y"] -def set_position(x: int, y: int): +def set_position(x: int, y: int) -> None: """ :param x we want to set mouse x position :param y we want to set mouse y position @@ -39,7 +40,7 @@ def set_position(x: int, y: int): display.sync() -def press_mouse(mouse_keycode: int): +def press_mouse(mouse_keycode: int) -> None: """ :param mouse_keycode mouse keycode we want to press """ @@ -48,7 +49,7 @@ def press_mouse(mouse_keycode: int): display.sync() -def release_mouse(mouse_keycode: int): +def release_mouse(mouse_keycode: int) -> None: """ :param mouse_keycode which mouse keycode we want to release """ @@ -57,7 +58,7 @@ def release_mouse(mouse_keycode: int): display.sync() -def click_mouse(mouse_keycode: int, x=None, y=None): +def click_mouse(mouse_keycode: int, x=None, y=None) -> None: """ :param mouse_keycode which mouse keycode we want to click :param x set mouse x position @@ -69,7 +70,7 @@ def click_mouse(mouse_keycode: int, x=None, y=None): release_mouse(mouse_keycode) -def scroll(scroll_value: int, scroll_direction: int): +def scroll(scroll_value: int, scroll_direction: int) -> None: """" :param scroll_value scroll unit :param scroll_direction what direction you want to scroll diff --git a/je_auto_control/linux_with_x11/record/x11_linux_record.py b/je_auto_control/linux_with_x11/record/x11_linux_record.py index d6b5900..18834b1 100644 --- a/je_auto_control/linux_with_x11/record/x11_linux_record.py +++ b/je_auto_control/linux_with_x11/record/x11_linux_record.py @@ -25,14 +25,14 @@ def __init__(self): self.record_queue = None self.result_queue = None - def record(self): + def record(self) -> None: """ create a new queue and start test_record """ self.record_queue = Queue() x11_linux_record(self.record_queue) - def stop_record(self): + def stop_record(self) -> None: """ stop test_record make a format action queue @@ -50,12 +50,3 @@ def stop_record(self): x11_linux_recoder = X11LinuxRecorder() -if __name__ == "__main__": - x11_record = X11LinuxRecorder() - x11_record.record() - from time import sleep - sleep(10) - temp = x11_record.stop_record() - for action in temp.queue: - print(action) - diff --git a/je_auto_control/linux_with_x11/screen/x11_linux_screen.py b/je_auto_control/linux_with_x11/screen/x11_linux_screen.py index e95f19d..ded8898 100644 --- a/je_auto_control/linux_with_x11/screen/x11_linux_screen.py +++ b/je_auto_control/linux_with_x11/screen/x11_linux_screen.py @@ -1,4 +1,5 @@ import sys +from typing import Tuple from je_auto_control.utils.exception.exceptions import AutoControlException from je_auto_control.utils.exception.exception_tag import linux_import_error @@ -9,7 +10,7 @@ from je_auto_control.linux_with_x11.core.utils.x11_linux_display import display -def size(): +def size() -> Tuple[int, int]: """ get screen size """ diff --git a/je_auto_control/osx/keyboard/osx_keyboard.py b/je_auto_control/osx/keyboard/osx_keyboard.py index 09da291..e809616 100644 --- a/je_auto_control/osx/keyboard/osx_keyboard.py +++ b/je_auto_control/osx/keyboard/osx_keyboard.py @@ -6,7 +6,6 @@ if sys.platform not in ["darwin"]: raise AutoControlException(osx_import_error) -import time import AppKit import Quartz @@ -41,7 +40,7 @@ } -def normal_key(keycode: int, is_shift: bool, is_down: bool): +def normal_key(keycode: int, is_shift: bool, is_down: bool) -> None: """ :param keycode which keycode we want to press or release :param is_shift use shift key ? @@ -67,7 +66,7 @@ def normal_key(keycode: int, is_shift: bool, is_down: bool): print(repr(error), file=sys.stderr) -def special_key(keycode: int, is_shift: bool): +def special_key(keycode: int, is_shift: bool) -> None: """ :param keycode which keycode we want to press or release :param is_shift use shift key ? @@ -89,7 +88,7 @@ def special_key(keycode: int, is_shift: bool): Quartz.CGEventPost(0, event) -def press_key(keycode: int, is_shift: bool): +def press_key(keycode: int, is_shift: bool) -> None: """ :param keycode which keycode we want to press :param is_shift is shift press? @@ -100,7 +99,7 @@ def press_key(keycode: int, is_shift: bool): normal_key(keycode, is_shift, True) -def release_key(keycode: int, is_shift: bool): +def release_key(keycode: int, is_shift: bool) -> None: """ :param keycode which keycode we want to release :param is_shift is shift press? diff --git a/je_auto_control/osx/keyboard/osx_keyboard_check.py b/je_auto_control/osx/keyboard/osx_keyboard_check.py index ff8e479..4921bd2 100644 --- a/je_auto_control/osx/keyboard/osx_keyboard_check.py +++ b/je_auto_control/osx/keyboard/osx_keyboard_check.py @@ -9,7 +9,7 @@ import Quartz -def check_key_is_press(keycode: int): +def check_key_is_press(keycode: int) -> bool: """ :param keycode which keycode we want to check """ diff --git a/je_auto_control/osx/listener/osx_listener.py b/je_auto_control/osx/listener/osx_listener.py index 82578dc..2b7b22f 100644 --- a/je_auto_control/osx/listener/osx_listener.py +++ b/je_auto_control/osx/listener/osx_listener.py @@ -8,7 +8,6 @@ from Cocoa import * -import time from Foundation import * from PyObjCTools import AppHelper @@ -27,17 +26,17 @@ def applicationDidFinishLaunching_(self, aNotification): NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(NSEventMaskRightMouseDown, mouse_right_handler) -def mouse_left_handler(event): +def mouse_left_handler(event) -> None: loc = NSEvent.mouseLocation() record_queue.put(("mouse_left", loc.x, loc.y)) -def mouse_right_handler(event): +def mouse_right_handler(event) -> None: loc = NSEvent.mouseLocation() record_queue.put(("mouse_right", loc.x, loc.y)) -def keyboard_handler(event): +def keyboard_handler(event) -> None: if int(event.keyCode()) == 98: pass else: @@ -45,17 +44,14 @@ def keyboard_handler(event): print(event) -def osx_record(): +def osx_record() -> None: record_queue = Queue() delegate = AppDelegate.alloc().init() app.setDelegate_(delegate) AppHelper.runEventLoop() -def osx_stop_record(): +def osx_stop_record() -> Queue: return record_queue -if __name__ == "__main__": - osx_record() - diff --git a/je_auto_control/osx/mouse/osx_mouse.py b/je_auto_control/osx/mouse/osx_mouse.py index 7d7166d..30fec47 100644 --- a/je_auto_control/osx/mouse/osx_mouse.py +++ b/je_auto_control/osx/mouse/osx_mouse.py @@ -1,5 +1,5 @@ import sys - +from typing import Tuple from je_auto_control.utils.exception.exception_tag import osx_import_error from je_auto_control.utils.exception.exceptions import AutoControlException @@ -16,14 +16,14 @@ from je_auto_control.osx.core.utils.osx_vk import osx_mouse_right -def position(): +def position() -> Tuple[int, int]: """ get mouse current position """ return (Quartz.NSEvent.mouseLocation().x, Quartz.NSEvent.mouseLocation().y) -def mouse_event(event, x: int, y: int, mouse_button: int): +def mouse_event(event, x: int, y: int, mouse_button: int) -> None: """ :param event which event we want to use :param x event x @@ -34,7 +34,7 @@ def mouse_event(event, x: int, y: int, mouse_button: int): Quartz.CGEventPost(Quartz.kCGHIDEventTap, curr_event) -def set_position(x: int, y: int): +def set_position(x: int, y: int) -> None: """ :param x we want to set mouse x position :param y we want to set mouse y position @@ -42,7 +42,7 @@ def set_position(x: int, y: int): mouse_event(Quartz.kCGEventMouseMoved, x, y, 0) -def press_mouse(x: int, y: int, mouse_button: int): +def press_mouse(x: int, y: int, mouse_button: int) -> None: """ :param x event x :param y event y @@ -56,7 +56,7 @@ def press_mouse(x: int, y: int, mouse_button: int): mouse_event(Quartz.kCGEventRightMouseDown, x, y, Quartz.kCGMouseButtonRight) -def release_mouse(x: int, y: int, mouse_button: int): +def release_mouse(x: int, y: int, mouse_button: int) -> None: """ :param x event x :param y event y @@ -70,7 +70,7 @@ def release_mouse(x: int, y: int, mouse_button: int): mouse_event(Quartz.kCGEventRightMouseUp, x, y, Quartz.kCGMouseButtonRight) -def click_mouse(x: int, y: int, mouse_button: int): +def click_mouse(x: int, y: int, mouse_button: int) -> None: """ :param x event x :param y event y @@ -90,7 +90,7 @@ def click_mouse(x: int, y: int, mouse_button: int): release_mouse(x, y, mouse_button) -def scroll(scroll_value: int): +def scroll(scroll_value: int) -> None: """ :param scroll_value scroll count """ diff --git a/je_auto_control/osx/record/osx_record.py b/je_auto_control/osx/record/osx_record.py index ee6e938..82fa1fa 100644 --- a/je_auto_control/osx/record/osx_record.py +++ b/je_auto_control/osx/record/osx_record.py @@ -1,5 +1,5 @@ import sys - +from queue import Queue from je_auto_control.utils.exception.exception_tag import osx_import_error from je_auto_control.utils.exception.exceptions import AutoControlException @@ -15,10 +15,10 @@ class OSXRecorder(object): - def record(self): + def record(self) -> None: osx_record() - def stop_record(self): + def stop_record(self) -> Queue: record_queue = osx_stop_record() if record_queue is None: raise AutoControlJsonActionException @@ -26,13 +26,3 @@ def stop_record(self): osx_recorder = OSXRecorder() - -if __name__ == "__main__": - test_osx_recorder = OSXRecorder() - test_osx_recorder.record() - temp = test_osx_recorder.stop_record() - print(temp) - for action in temp.queue: - print(action) - while True: - pass diff --git a/je_auto_control/osx/screen/osx_screen.py b/je_auto_control/osx/screen/osx_screen.py index 7d518d8..a9f5f40 100644 --- a/je_auto_control/osx/screen/osx_screen.py +++ b/je_auto_control/osx/screen/osx_screen.py @@ -1,4 +1,5 @@ import sys +from typing import Tuple from je_auto_control.utils.exception.exception_tag import osx_import_error from je_auto_control.utils.exception.exceptions import AutoControlException @@ -9,7 +10,7 @@ import Quartz -def size(): +def size() -> Tuple[int, int]: """ get screen size """ diff --git a/je_auto_control/utils/critical_exit/critcal_exit.py b/je_auto_control/utils/critical_exit/critcal_exit.py index 82b6f82..f1f4eac 100644 --- a/je_auto_control/utils/critical_exit/critcal_exit.py +++ b/je_auto_control/utils/critical_exit/critcal_exit.py @@ -20,7 +20,7 @@ def __init__(self, default_daemon: bool = True): self.setDaemon(default_daemon) self._exit_check_key = keys_table.get("f7") - def set_critical_key(self, keycode: [int, str] = None): + def set_critical_key(self, keycode: [int, str] = None) -> None: """ set interrupt key :param keycode interrupt key @@ -30,7 +30,7 @@ def set_critical_key(self, keycode: [int, str] = None): else: self._exit_check_key = keys_table.get(keycode) - def run(self): + def run(self) -> None: """ listener keycode _exit_check_key to interrupt """ @@ -41,7 +41,7 @@ def run(self): except Exception as error: print(repr(error), file=sys.stderr) - def init_critical_exit(self): + def init_critical_exit(self) -> None: """ should only use this to start critical exit may this function will add more diff --git a/je_auto_control/utils/executor/action_executor.py b/je_auto_control/utils/executor/action_executor.py index 01913cc..775ebe5 100644 --- a/je_auto_control/utils/executor/action_executor.py +++ b/je_auto_control/utils/executor/action_executor.py @@ -58,7 +58,7 @@ } -def execute_action(action_list: list): +def execute_action(action_list: list) -> str: """ use to execute all action on action list(action file or program list) :param action_list the list include action @@ -85,7 +85,7 @@ def execute_action(action_list: list): return execute_record_string -def execute_files(execute_files_list: list): +def execute_files(execute_files_list: list) -> list: """ :param execute_files_list: list include execute files path :return: every execute detail as list diff --git a/je_auto_control/utils/file_process/create_project_structure.py b/je_auto_control/utils/file_process/create_project_structure.py index 4eddf90..3089cf9 100644 --- a/je_auto_control/utils/file_process/create_project_structure.py +++ b/je_auto_control/utils/file_process/create_project_structure.py @@ -1,7 +1,7 @@ from pathlib import Path -def create_dir(dir_name: str): +def create_dir(dir_name: str) -> None: """ :param dir_name: create dir use dir name :return: None @@ -12,5 +12,5 @@ def create_dir(dir_name: str): ) -def create_template_dir(): +def create_template_dir() -> None: create_dir("auto_control/template") diff --git a/je_auto_control/utils/file_process/get_dir_file_list.py b/je_auto_control/utils/file_process/get_dir_file_list.py index e6abae4..50e94c5 100644 --- a/je_auto_control/utils/file_process/get_dir_file_list.py +++ b/je_auto_control/utils/file_process/get_dir_file_list.py @@ -2,9 +2,12 @@ from os import getcwd from os.path import abspath from os.path import join +from typing import List -def get_dir_files_as_list(dir_path: str = getcwd(), default_search_file_extension: str = ".json"): +def get_dir_files_as_list( + dir_path: str = getcwd(), + default_search_file_extension: str = ".json") -> List[str]: """ get dir file when end with default_search_file_extension :param dir_path: which dir we want to walk and get file list @@ -16,4 +19,3 @@ def get_dir_files_as_list(dir_path: str = getcwd(), default_search_file_extensio for file in files if file.endswith(default_search_file_extension.lower()) ] - diff --git a/je_auto_control/utils/html_report/html_report_generate.py b/je_auto_control/utils/html_report/html_report_generate.py index d2cf638..f2cbc04 100644 --- a/je_auto_control/utils/html_report/html_report_generate.py +++ b/je_auto_control/utils/html_report/html_report_generate.py @@ -5,9 +5,9 @@ from je_auto_control.utils.exception.exception_tag import html_generate_no_data_tag from threading import Lock -lock = Lock() +_lock = Lock() -html_string = \ +_html_string = \ r""" @@ -81,7 +81,7 @@ """.strip() -event_table = \ +_event_table = \ r""" @@ -112,11 +112,11 @@ """.strip() -def make_html_table(event_str: str, record_data: dict, table_head: str): +def make_html_table(event_str: str, record_data: dict, table_head: str) -> str: event_str = "".join( [ event_str, - event_table.format( + _event_table.format( table_head_class=table_head, function_name=record_data.get("function_name"), param=record_data.get("local_param"), @@ -128,7 +128,7 @@ def make_html_table(event_str: str, record_data: dict, table_head: str): return event_str -def generate_html(html_name: str = "default_name"): +def generate_html(html_name: str = "default_name") -> str: """ this function will create and save html report on current folder :param html_name: save html file name @@ -144,9 +144,9 @@ def generate_html(html_name: str = "default_name"): event_str = make_html_table(event_str, record_data, "event_table_head") else: event_str = make_html_table(event_str, record_data, "failure_table_head") - new_html_string = html_string.format(event_table=event_str) + new_html_string = _html_string.format(event_table=event_str) try: - lock.acquire() + _lock.acquire() with open(html_name + ".html", "w+") as file_to_write: file_to_write.write( new_html_string @@ -154,5 +154,5 @@ def generate_html(html_name: str = "default_name"): except Exception as error: print(repr(error), file=sys.stderr) finally: - lock.release() + _lock.release() return new_html_string diff --git a/je_auto_control/utils/image/screenshot.py b/je_auto_control/utils/image/screenshot.py index 8ae54d3..9c315b7 100644 --- a/je_auto_control/utils/image/screenshot.py +++ b/je_auto_control/utils/image/screenshot.py @@ -1,7 +1,7 @@ -from PIL import ImageGrab +from PIL import ImageGrab, Image -def pil_screenshot(file_path: str = None, region: list = None): +def pil_screenshot(file_path: str = None, region: list = None) -> Image: """ :param file_path save screenshot path (None is no save) :param region screenshot region (screenshot region on screen) diff --git a/je_auto_control/utils/image/template_detection.py b/je_auto_control/utils/image/template_detection.py index af97911..a335357 100644 --- a/je_auto_control/utils/image/template_detection.py +++ b/je_auto_control/utils/image/template_detection.py @@ -1,8 +1,10 @@ +from typing import List + from PIL import ImageGrab from je_open_cv import template_detection -def find_image(image, detect_threshold: float = 1, draw_image: bool = False): +def find_image(image, detect_threshold: float = 1, draw_image: bool = False) -> List[int]: """ :param image which image we want to find on screen :param detect_threshold detect precision 0.0 ~ 1.0; 1 is absolute equal @@ -13,7 +15,7 @@ def find_image(image, detect_threshold: float = 1, draw_image: bool = False): detect_threshold=detect_threshold, draw_image=draw_image) -def find_image_multi(image, detect_threshold: float = 1, draw_image: bool = False): +def find_image_multi(image, detect_threshold: float = 1, draw_image: bool = False) -> List[List[int]]: """ :param image which image we want to find on screen :param detect_threshold detect precision 0.0 ~ 1.0; 1 is absolute equal diff --git a/je_auto_control/utils/json/json_file.py b/je_auto_control/utils/json/json_file.py index b7dc2a7..bd7540d 100644 --- a/je_auto_control/utils/json/json_file.py +++ b/je_auto_control/utils/json/json_file.py @@ -6,16 +6,16 @@ from je_auto_control.utils.exception.exception_tag import cant_save_json_error from je_auto_control.utils.exception.exception_tag import cant_find_json_error -lock = Lock() +_lock = Lock() -def read_action_json(json_file_path: str): +def read_action_json(json_file_path: str) -> list: """ use to read action file :param json_file_path json file's path to read """ try: - lock.acquire() + _lock.acquire() file_path = Path(json_file_path) if file_path.exists() and file_path.is_file(): with open(json_file_path) as read_file: @@ -23,21 +23,21 @@ def read_action_json(json_file_path: str): except AutoControlJsonActionException: raise AutoControlJsonActionException(cant_find_json_error) finally: - lock.release() + _lock.release() -def write_action_json(json_save_path: str, action_json: list): +def write_action_json(json_save_path: str, action_json: list) -> None: """ use to save action file :param json_save_path json save path :param action_json the json str include action to write """ try: - lock.acquire() + _lock.acquire() with open(json_save_path, "w+") as file_to_write: file_to_write.write(json.dumps(action_json)) except AutoControlJsonActionException: raise AutoControlJsonActionException(cant_save_json_error) finally: - lock.release() + _lock.release() diff --git a/je_auto_control/utils/test_record/record_test_class.py b/je_auto_control/utils/test_record/record_test_class.py index 95b067f..f791839 100644 --- a/je_auto_control/utils/test_record/record_test_class.py +++ b/je_auto_control/utils/test_record/record_test_class.py @@ -7,14 +7,14 @@ def __init__(self, init_total_record: bool = False): self.init_total_record = init_total_record self.total_record_list = list() - def clean_record(self): + def clean_record(self) -> None: self.total_record_list = list() test_record = TestRecord() -def record_total(function_name: str, local_param, program_exception: str = None): +def record_total(function_name: str, local_param, program_exception: str = None) -> None: if not test_record.init_total_record: pass else: diff --git a/je_auto_control/windows/core/utils/win32_ctype_input.py b/je_auto_control/windows/core/utils/win32_ctype_input.py index e3138d1..c6b8e29 100644 --- a/je_auto_control/windows/core/utils/win32_ctype_input.py +++ b/je_auto_control/windows/core/utils/win32_ctype_input.py @@ -59,15 +59,14 @@ class INPUT_Union(ctypes.Union): ("_input", INPUT_Union)) -LPINPUT = ctypes.POINTER(Input) - - -def _check_count(result, func, args): +def _check_count(result, func, args) -> list: if result == 0: raise ctypes.WinError(ctypes.get_last_error()) return args +LPINPUT = ctypes.POINTER(Input) + SendInput = user32.SendInput user32.SendInput.errcheck = _check_count diff --git a/je_auto_control/windows/core/utils/win32_keypress_check.py b/je_auto_control/windows/core/utils/win32_keypress_check.py index 7ce742b..3913c83 100644 --- a/je_auto_control/windows/core/utils/win32_keypress_check.py +++ b/je_auto_control/windows/core/utils/win32_keypress_check.py @@ -9,7 +9,7 @@ import ctypes -def check_key_is_press(keycode: [int, str]): +def check_key_is_press(keycode: [int, str]) -> bool: if type(keycode) is int: temp = ctypes.windll.user32.GetKeyState(keycode) else: diff --git a/je_auto_control/windows/keyboard/win32_ctype_keyboard_control.py b/je_auto_control/windows/keyboard/win32_ctype_keyboard_control.py index 3f93b97..2400dc2 100644 --- a/je_auto_control/windows/keyboard/win32_ctype_keyboard_control.py +++ b/je_auto_control/windows/keyboard/win32_ctype_keyboard_control.py @@ -14,7 +14,7 @@ from je_auto_control.windows.core.utils.win32_ctype_input import win32_EventF_KEYUP -def press_key(keycode: int): +def press_key(keycode: int) -> None: """ :param keycode which keycode we want to press """ @@ -22,7 +22,7 @@ def press_key(keycode: int): SendInput(1, ctypes.byref(x), ctypes.sizeof(x)) -def release_key(keycode: int): +def release_key(keycode: int) -> None: """ :param keycode which keycode we want to release """ diff --git a/je_auto_control/windows/listener/win32_keyboard_listener.py b/je_auto_control/windows/listener/win32_keyboard_listener.py index b35fbfe..92ce138 100644 --- a/je_auto_control/windows/listener/win32_keyboard_listener.py +++ b/je_auto_control/windows/listener/win32_keyboard_listener.py @@ -13,10 +13,9 @@ from queue import Queue -user32 = windll.user32 -kernel32 = windll.kernel32 - -wm_keydown = 0x100 +_user32 = windll.user32 +_kernel32 = windll.kernel32 +_wm_keydown = 0x100 class Win32KeyboardListener(Thread): @@ -29,8 +28,8 @@ def __init__(self): self.record_flag = False self.hook_event_code_int = 13 - def _set_win32_hook(self, point): - self.hooked = user32.SetWindowsHookExA( + def _set_win32_hook(self, point) -> bool: + self.hooked = _user32.SetWindowsHookExA( self.hook_event_code_int, point, 0, @@ -40,51 +39,41 @@ def _set_win32_hook(self, point): return False return True - def _remove_win32_hook_proc(self): + def _remove_win32_hook_proc(self) -> None: if self.hooked is None: return - user32.UnhookWindowsHookEx(self.hooked) + _user32.UnhookWindowsHookEx(self.hooked) self.hooked = None - def _win32_hook_proc(self, code, w_param, l_param): - if w_param is not wm_keydown: - return user32.CallNextHookEx(self.hooked, code, w_param, l_param) + def _win32_hook_proc(self, code, w_param, l_param) -> _user32.CallNextHookEx: + if w_param is not _wm_keydown: + return _user32.CallNextHookEx(self.hooked, code, w_param, l_param) if self.record_flag is True: # int to hex temp = hex(l_param[0] & 0xFFFFFFFF) self.record_queue.put(("type_key", int(temp, 16))) - return user32.CallNextHookEx(self.hooked, code, w_param, l_param) + return _user32.CallNextHookEx(self.hooked, code, w_param, l_param) - def _get_function_pointer(self, function): + def _get_function_pointer(self, function) -> WINFUNCTYPE: win_function = WINFUNCTYPE(c_int, c_int, c_int, POINTER(c_void_p)) return win_function(function) - def _start_listener(self): + def _start_listener(self) -> None: pointer = self._get_function_pointer(self._win32_hook_proc) self._set_win32_hook(pointer) message = MSG() - user32.GetMessageA(byref(message), 0, 0, 0) + _user32.GetMessageA(byref(message), 0, 0, 0) - def record(self, want_to_record_queue): + def record(self, want_to_record_queue) -> None: self.record_flag = True self.record_queue = want_to_record_queue self.start() - def stop_record(self): + def stop_record(self) -> Queue: self.record_flag = False self._remove_win32_hook_proc() return self.record_queue - def run(self): + def run(self) -> None: self._start_listener() - -if __name__ == "__main__": - win32_keyboard_listener = Win32KeyboardListener() - record_queue = Queue() - win32_keyboard_listener.record(record_queue) - from time import sleep - sleep(3) - temp = win32_keyboard_listener.stop_record() - for i in temp.queue: - print(i) diff --git a/je_auto_control/windows/listener/win32_mouse_listener.py b/je_auto_control/windows/listener/win32_mouse_listener.py index cd6face..cc8d4fe 100644 --- a/je_auto_control/windows/listener/win32_mouse_listener.py +++ b/je_auto_control/windows/listener/win32_mouse_listener.py @@ -15,14 +15,14 @@ from je_auto_control.windows.mouse.win32_ctype_mouse_control import position -user32 = windll.user32 -kernel32 = windll.kernel32 +_user32 = windll.user32 +_kernel32 = windll.kernel32 """ Left mouse button down 0x0201 Right mouse button down 0x0204 Middle mouse button down 0x0207 """ -wm_mouse_key_code = [0x0201, 0x0204, 0x0207] +_wm_mouse_key_code = [0x0201, 0x0204, 0x0207] class Win32MouseListener(Thread): @@ -35,8 +35,8 @@ def __init__(self): self.record_flag = False self.hook_event_code_int = 14 - def _set_win32_hook(self, point): - self.hooked = user32.SetWindowsHookExA( + def _set_win32_hook(self, point) -> bool: + self.hooked = _user32.SetWindowsHookExA( self.hook_event_code_int, point, 0, @@ -46,58 +46,49 @@ def _set_win32_hook(self, point): return False return True - def _remove_win32_hook_proc(self): + def _remove_win32_hook_proc(self) -> None: if self.hooked is None: return - user32.UnhookWindowsHookEx(self.hooked) + _user32.UnhookWindowsHookEx(self.hooked) self.hooked = None - def _win32_hook_proc(self, code, w_param, l_param): - if w_param not in wm_mouse_key_code: - return user32.CallNextHookEx(self.hooked, code, w_param, l_param) - if w_param == wm_mouse_key_code[0] and self.record_flag is True: + def _win32_hook_proc(self, code, w_param, l_param) -> _user32.CallNextHookEx: + if w_param not in _wm_mouse_key_code: + return _user32.CallNextHookEx(self.hooked, code, w_param, l_param) + if w_param == _wm_mouse_key_code[0] and self.record_flag is True: x, y = position() self.record_queue.put(("mouse_left", x, y)) - elif w_param == wm_mouse_key_code[1] and self.record_flag is True: + elif w_param == _wm_mouse_key_code[1] and self.record_flag is True: x, y = position() self.record_queue.put(("mouse_right", x, y)) - elif w_param == wm_mouse_key_code[2] and self.record_flag is True: + elif w_param == _wm_mouse_key_code[2] and self.record_flag is True: x, y = position() self.record_queue.put(("mouse_middle", x, y)) - return user32.CallNextHookEx(self.hooked, code, w_param, l_param) + return _user32.CallNextHookEx(self.hooked, code, w_param, l_param) - def _get_function_pointer(self, function): + def _get_function_pointer(self, function) -> WINFUNCTYPE: win_function = WINFUNCTYPE(c_int, c_int, c_int, POINTER(c_void_p)) return win_function(function) - def _start_listener(self): + def _start_listener(self) -> None: pointer = self._get_function_pointer(self._win32_hook_proc) self._set_win32_hook(pointer) message = MSG() - user32.GetMessageA(byref(message), 0, 0, 0) + _user32.GetMessageA(byref(message), 0, 0, 0) - def record(self, want_to_record_queue): + def record(self, want_to_record_queue) -> None: self.record_flag = True self.record_queue = want_to_record_queue self.start() - def stop_record(self): + def stop_record(self) -> Queue: self.record_flag = False self._remove_win32_hook_proc() return self.record_queue - def run(self): + def run(self) -> None: self._start_listener() -if __name__ == "__main__": - win32_mouse_listener = Win32MouseListener() - record_queue = Queue() - win32_mouse_listener.record(record_queue) - from time import sleep - sleep(3) - temp = win32_mouse_listener.stop_record() - for i in temp.queue: - print(i) diff --git a/je_auto_control/windows/mouse/win32_ctype_mouse_control.py b/je_auto_control/windows/mouse/win32_ctype_mouse_control.py index 7f7dcae..1c0c7f4 100644 --- a/je_auto_control/windows/mouse/win32_ctype_mouse_control.py +++ b/je_auto_control/windows/mouse/win32_ctype_mouse_control.py @@ -1,4 +1,5 @@ import sys +from typing import Tuple from je_auto_control.utils.exception.exception_tag import windows_import_error from je_auto_control.utils.exception.exceptions import AutoControlException @@ -36,7 +37,7 @@ _set_cursor_pos = windll.user32.SetCursorPos -def mouse_event(event, x: int, y: int, dwData: int = 0): +def mouse_event(event, x: int, y: int, dwData: int = 0) -> None: """ :param event which event we use :param x event x @@ -49,7 +50,7 @@ def mouse_event(event, x: int, y: int, dwData: int = 0): ctypes.windll.user32.mouse_event(event, ctypes.c_long(convertedX), ctypes.c_long(convertedY), dwData, 0) -def position(): +def position() -> [Tuple[int, int], None]: """ get mouse position """ @@ -60,7 +61,7 @@ def position(): return None -def set_position(x: int, y: int): +def set_position(x: int, y: int) -> None: """ :param x set mouse position x :param y set mouse position y @@ -69,7 +70,7 @@ def set_position(x: int, y: int): _set_cursor_pos(*pos) -def press_mouse(press_button: int): +def press_mouse(press_button: int) -> None: """ :param press_button which button we want to press """ @@ -79,7 +80,7 @@ def press_mouse(press_button: int): ctypes.sizeof(Input)) -def release_mouse(release_button: int): +def release_mouse(release_button: int) -> None: """ :param release_button which button we want to release """ @@ -89,7 +90,7 @@ def release_mouse(release_button: int): ctypes.sizeof(Input)) -def click_mouse(mouse_keycode: int, x: int = None, y: int = None): +def click_mouse(mouse_keycode: int, x: int = None, y: int = None) -> None: """ :param mouse_keycode which mouse keycode we want to click :param x mouse x position @@ -101,7 +102,7 @@ def click_mouse(mouse_keycode: int, x: int = None, y: int = None): release_mouse(mouse_keycode) -def scroll(scroll_value: int, x: int = None, y: int = None): +def scroll(scroll_value: int, x: int = None, y: int = None) -> None: """ :param scroll_value scroll count :param x scroll x diff --git a/je_auto_control/windows/record/win32_record.py b/je_auto_control/windows/record/win32_record.py index 37476b0..5e13666 100644 --- a/je_auto_control/windows/record/win32_record.py +++ b/je_auto_control/windows/record/win32_record.py @@ -20,35 +20,35 @@ def __init__(self): self.record_queue = None self.result_queue = None - def record(self): + def record(self) -> None: self.mouse_record_listener = Win32MouseListener() self.keyboard_record_listener = Win32KeyboardListener() self.record_queue = Queue() self.mouse_record_listener.record(self.record_queue) self.keyboard_record_listener.record(self.record_queue) - def stop_record(self): + def stop_record(self) -> Queue: self.result_queue = self.mouse_record_listener.stop_record() self.result_queue = self.keyboard_record_listener.stop_record() self.record_queue = None return self.result_queue - def record_mouse(self): + def record_mouse(self) -> None: self.mouse_record_listener = Win32MouseListener() self.record_queue = Queue() self.mouse_record_listener.record(self.record_queue) - def stop_record_mouse(self): + def stop_record_mouse(self) -> Queue: self.result_queue = self.mouse_record_listener.stop_record() self.record_queue = None return self.result_queue - def record_keyboard(self): + def record_keyboard(self) -> None: self.keyboard_record_listener = Win32KeyboardListener() self.record_queue = Queue() - self.keyboard_record_listener.record(record_queue) + self.keyboard_record_listener.record(self.record_queue) - def stop_record_keyboard(self): + def stop_record_keyboard(self) -> Queue: self.result_queue = self.keyboard_record_listener.stop_record() self.record_queue = None return self.result_queue @@ -56,11 +56,3 @@ def stop_record_keyboard(self): win32_recorder = Win32Recorder() -if __name__ == "__main__": - win32_recorder = Win32Recorder() - win32_recorder.record() - from time import sleep - - sleep(10) - for i in win32_recorder.stop_record().queue: - print(i) diff --git a/je_auto_control/windows/screen/win32_screen.py b/je_auto_control/windows/screen/win32_screen.py index 385052e..dcc2602 100644 --- a/je_auto_control/windows/screen/win32_screen.py +++ b/je_auto_control/windows/screen/win32_screen.py @@ -1,4 +1,5 @@ import sys +from typing import List, Union from je_auto_control.utils.exception.exception_tag import windows_import_error from je_auto_control.utils.exception.exceptions import AutoControlException @@ -8,12 +9,12 @@ import ctypes -user32 = ctypes.windll.user32 -user32.SetProcessDPIAware() +_user32 = ctypes.windll.user32 +_user32.SetProcessDPIAware() -def size(): +def size() -> List[Union[int, int]]: """ get screen size """ - return [user32.GetSystemMetrics(0), user32.GetSystemMetrics(1)] + return [_user32.GetSystemMetrics(0), _user32.GetSystemMetrics(1)] diff --git a/je_auto_control/wrapper/auto_control_image.py b/je_auto_control/wrapper/auto_control_image.py index 24a3b70..ab50b1b 100644 --- a/je_auto_control/wrapper/auto_control_image.py +++ b/je_auto_control/wrapper/auto_control_image.py @@ -1,4 +1,5 @@ import sys +from typing import List, Union from je_auto_control.utils.image import template_detection from je_auto_control.utils.exception.exception_tag import cant_find_image @@ -10,7 +11,8 @@ from je_auto_control.utils.test_record.record_test_class import record_total -def locate_all_image(image, detect_threshold: [float, int] = 1, draw_image: bool = False): +def locate_all_image(image, detect_threshold: [float, int] = 1, + draw_image: bool = False) -> List[int]: """ use to locate all image that detected and then return detected images list :param image which image we want to find on screen (png or PIL ImageGrab.grab()) @@ -33,7 +35,7 @@ def locate_all_image(image, detect_threshold: [float, int] = 1, draw_image: bool print(repr(error), file=sys.stderr) -def locate_image_center(image, detect_threshold: [float, int] = 1, draw_image: bool = False): +def locate_image_center(image, detect_threshold: [float, int] = 1, draw_image: bool = False) -> List[Union[int, int]]: """ use to locate image and return image center position :param image which image we want to find on screen (png or PIL ImageGrab.grab()) @@ -59,7 +61,10 @@ def locate_image_center(image, detect_threshold: [float, int] = 1, draw_image: b print(repr(error), file=sys.stderr) -def locate_and_click(image, mouse_keycode: [int, str], detect_threshold: [float, int] = 1, draw_image: bool = False): +def locate_and_click( + image, mouse_keycode: [int, str], + detect_threshold: [float, int] = 1, + draw_image: bool = False) -> List[Union[int, int]]: """ use to locate image and click image center position and the return image center position :param image which image we want to find on screen (png or PIL ImageGrab.grab()) @@ -90,7 +95,7 @@ def locate_and_click(image, mouse_keycode: [int, str], detect_threshold: [float, print(repr(error), file=sys.stderr) -def screenshot(file_path: str = None, region: list = None): +def screenshot(file_path: str = None, region: list = None) -> List[Union[int, int]]: """ use to get now screen image return image :param file_path save screenshot path (None is no save) diff --git a/je_auto_control/wrapper/auto_control_keyboard.py b/je_auto_control/wrapper/auto_control_keyboard.py index 5e82fb0..84e7858 100644 --- a/je_auto_control/wrapper/auto_control_keyboard.py +++ b/je_auto_control/wrapper/auto_control_keyboard.py @@ -1,4 +1,5 @@ import sys +from typing import Tuple from je_auto_control.utils.exception.exception_tag import keyboard_hotkey from je_auto_control.utils.exception.exception_tag import keyboard_press_key @@ -15,7 +16,7 @@ from je_auto_control.utils.test_record.record_test_class import record_total -def press_key(keycode: [int, str], is_shift: bool = False, skip_record: bool = False): +def press_key(keycode: [int, str], is_shift: bool = False, skip_record: bool = False) -> str: """ use to press a key still press to use release key or use critical exit @@ -55,7 +56,7 @@ def press_key(keycode: [int, str], is_shift: bool = False, skip_record: bool = F print(repr(error), file=sys.stderr) -def release_key(keycode: [int, str], is_shift: bool = False, skip_record: bool = False): +def release_key(keycode: [int, str], is_shift: bool = False, skip_record: bool = False) -> str: """ use to release pressed key return keycode :param keycode which keycode we want to release @@ -93,7 +94,7 @@ def release_key(keycode: [int, str], is_shift: bool = False, skip_record: bool = print(repr(error), file=sys.stderr) -def type_key(keycode: [int, str], is_shift: bool = False, skip_record: bool = False): +def type_key(keycode: [int, str], is_shift: bool = False, skip_record: bool = False) -> str: """ press and release key return keycode :param keycode which keycode we want to type @@ -122,7 +123,7 @@ def type_key(keycode: [int, str], is_shift: bool = False, skip_record: bool = Fa print(repr(error), file=sys.stderr) -def check_key_is_press(keycode: [int, str]): +def check_key_is_press(keycode: [int, str]) -> bool: """ use to check key is press return True or False :param keycode check key is press or not @@ -140,7 +141,7 @@ def check_key_is_press(keycode: [int, str]): print(repr(error), file=sys.stderr) -def write(write_string: str, is_shift: bool = False): +def write(write_string: str, is_shift: bool = False) -> str: """ use to press and release whole we get this function str return all press and release str @@ -174,7 +175,7 @@ def write(write_string: str, is_shift: bool = False): print(repr(error), file=sys.stderr) -def hotkey(key_code_list: list, is_shift: bool = False): +def hotkey(key_code_list: list, is_shift: bool = False) -> Tuple[str, str]: """ use to press and release all key on key_code_list then reverse list press and release again diff --git a/je_auto_control/wrapper/auto_control_mouse.py b/je_auto_control/wrapper/auto_control_mouse.py index 5509762..6aeb609 100644 --- a/je_auto_control/wrapper/auto_control_mouse.py +++ b/je_auto_control/wrapper/auto_control_mouse.py @@ -1,5 +1,6 @@ import ctypes import sys +from typing import Tuple, Union from je_auto_control.utils.exception.exception_tag import mouse_click_mouse from je_auto_control.utils.exception.exception_tag import mouse_get_position @@ -18,7 +19,7 @@ from je_auto_control.utils.test_record.record_test_class import record_total -def mouse_preprocess(mouse_keycode: [int, str], x: int, y: int): +def mouse_preprocess(mouse_keycode: [int, str], x: int, y: int) -> Tuple[Union[int, str], int, int]: """ check mouse keycode is verified or not and then check current mouse position @@ -45,7 +46,7 @@ def mouse_preprocess(mouse_keycode: [int, str], x: int, y: int): return mouse_keycode, x, y -def position(): +def position() -> Tuple[int, int]: """ get mouse current position return mouse_x, mouse_y @@ -61,7 +62,7 @@ def position(): print(repr(error), file=sys.stderr) -def set_position(x: int, y: int): +def set_position(x: int, y: int) -> Tuple[int, int]: """ :param x set mouse position x :param y set mouse position y @@ -82,7 +83,7 @@ def set_position(x: int, y: int): print(repr(error), file=sys.stderr) -def press_mouse(mouse_keycode: [int, str], x: int = None, y: int = None): +def press_mouse(mouse_keycode: [int, str], x: int = None, y: int = None) -> Tuple[Union[int, str], int, int]: """ press mouse keycode on x, y return keycode, x, y @@ -109,7 +110,7 @@ def press_mouse(mouse_keycode: [int, str], x: int = None, y: int = None): print(repr(error), file=sys.stderr) -def release_mouse(mouse_keycode: [int, str], x: int = None, y: int = None): +def release_mouse(mouse_keycode: [int, str], x: int = None, y: int = None) -> Tuple[Union[int, str], int, int]: """ release mouse keycode on x, y return keycode, x, y @@ -136,7 +137,7 @@ def release_mouse(mouse_keycode: [int, str], x: int = None, y: int = None): print(repr(error), file=sys.stderr) -def click_mouse(mouse_keycode: [int, str], x: int = None, y: int = None): +def click_mouse(mouse_keycode: [int, str], x: int = None, y: int = None) -> Tuple[Union[int, str], int, int]: """ press and release mouse keycode on x, y return keycode, x, y @@ -162,7 +163,7 @@ def click_mouse(mouse_keycode: [int, str], x: int = None, y: int = None): print(repr(error), file=sys.stderr) -def scroll(scroll_value: int, x: int = None, y: int = None, scroll_direction: str = "scroll_down"): +def scroll(scroll_value: int, x: int = None, y: int = None, scroll_direction: str = "scroll_down") -> Tuple[int, str]: """" :param scroll_value scroll count :param x mouse click x position diff --git a/je_auto_control/wrapper/auto_control_record.py b/je_auto_control/wrapper/auto_control_record.py index 6959617..30b7712 100644 --- a/je_auto_control/wrapper/auto_control_record.py +++ b/je_auto_control/wrapper/auto_control_record.py @@ -1,6 +1,5 @@ import sys -from je_auto_control.utils.executor.action_executor import execute_action from je_auto_control.utils.exception.exception_tag import macos_record_error from je_auto_control.utils.exception.exceptions import AutoControlException from je_auto_control.utils.exception.exceptions import AutoControlJsonActionException @@ -8,7 +7,7 @@ from je_auto_control.utils.test_record.record_test_class import record_total -def record(): +def record() -> None: """ start record keyboard and mouse event until stop_record """ @@ -22,7 +21,7 @@ def record(): print(repr(error), file=sys.stderr) -def stop_record(): +def stop_record() -> list: """ stop current record """ diff --git a/je_auto_control/wrapper/auto_control_screen.py b/je_auto_control/wrapper/auto_control_screen.py index d2b0579..3020724 100644 --- a/je_auto_control/wrapper/auto_control_screen.py +++ b/je_auto_control/wrapper/auto_control_screen.py @@ -1,4 +1,5 @@ import sys +from typing import Tuple, List import cv2 import numpy as np @@ -11,7 +12,7 @@ from je_auto_control.utils.test_record.record_test_class import record_total -def size(): +def size() -> Tuple[int, int]: """ get screen size """ @@ -26,7 +27,7 @@ def size(): print(repr(error), file=sys.stderr) -def screenshot(file_path: str = None, region: list = None): +def screenshot(file_path: str = None, region: list = None) -> List[int]: """ use to capture current screen image :param file_path screenshot file save path diff --git a/setup.py b/setup.py index 0fb0296..07da2b3 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name="je_auto_control", - version="0.0.89", + version="0.0.90", author="JE-Chen", author_email="zenmailman@gmail.com", description="auto testing",