-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio_utils.py
79 lines (67 loc) · 2.58 KB
/
io_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# Python native imports
import logging
from time import sleep
# Third-party imports
import PIL.Image
import pyautogui
import pywinauto
from pathlib import Path
class Utils:
callback_shared: bool | str = False
image_cache: dict[Path, PIL.Image.Image] = {}
window_region: tuple[int, int, int, int] = (0, 0, 0, 0)
window_center: tuple[int, int] = (0, 0)
init_complete = False
def __init__(self, window_name: str):
Utils.window_region = self._getWindowRegion(window_name)
Utils.window_center = self._findCenter(Utils.window_region)
Utils.init_complete = True
@staticmethod
def _getWindowRegion(window_name: str) -> tuple[int, int, int, int]:
"""
Find and return window region
Returns:
- region: tuple - tuple of four elements (x, y of top left, width, height of region)
"""
for window in pywinauto.Desktop(backend="uia").windows():
if window.window_text() != window_name:
continue
logging.info("Window '%s' found!", window_name)
rectangle = window.rectangle()
return (rectangle.left,
rectangle.top,
rectangle.right - rectangle.left,
rectangle.bottom - rectangle.top)
raise RuntimeError("Window '%s' not found!", window_name)
@staticmethod
def _findCenter(region: tuple[int, int, int, int]) -> tuple[int, int]:
return (region[0] + int(region[2]/2),
region[1] + int(region[3]/2))
@staticmethod
def _getImage(sub: str, image: str) -> PIL.Image.Image:
path = Path(__file__).parent / "resources" / sub / image
if not path.is_file():
raise FileNotFoundError(f"{path} not found!")
if path not in Utils.image_cache:
logging.debug(f"Opening {path} and saving in cache")
Utils.image_cache[path] = PIL.Image.open(path)
return Utils.image_cache[path]
@staticmethod
def locateOnScreen(image: str, optional: bool = True, sub: str = "") -> tuple[int, int] | None:
try:
return pyautogui.locateCenterOnScreen(image=Utils._getImage(sub=sub, image=image),
confidence=0.9,
region=Utils.window_region) # type: ignore
except pyautogui.ImageNotFoundException:
if optional:
return None
raise
@staticmethod
def clickButton(image: str, button_loc: tuple[int, int] | None, sub: str = ""):
while True:
if button_loc:
pyautogui.leftClick(button_loc)
sleep(0.5)
button_loc = Utils.locateOnScreen(image=image, sub=sub)
if button_loc is None:
break