Skip to content

Commit e9ddca5

Browse files
Merge pull request #61 from welli7ngton/mnt/annotate_mouse_methods
MNT: Add type hints to Mouse methods
2 parents 8151961 + 1d6408e commit e9ddca5

File tree

2 files changed

+93
-75
lines changed

2 files changed

+93
-75
lines changed

botcity/core/bot.py

+58-48
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ def get_element_coords_centered(
761761
height: Optional[int] = None,
762762
matching: float = 0.9,
763763
best: bool = True,
764-
):
764+
) -> Union[Tuple[int, int], Tuple[None, None]]:
765765
"""
766766
Find an element defined by label on screen and returns its centered coordinates.
767767
@@ -807,7 +807,7 @@ def browse(self, url, location=0):
807807
# Mouse
808808
#######
809809

810-
def click_on(self, label):
810+
def click_on(self, label: str) -> None:
811811
"""
812812
Click on the element.
813813
@@ -819,7 +819,7 @@ def click_on(self, label):
819819
raise ValueError(f"Element not available. Cannot find {label}.")
820820
_mouse_click(self._mouse_controller, x, y)
821821

822-
def get_last_x(self):
822+
def get_last_x(self) -> int:
823823
"""
824824
Get the last X position for the mouse.
825825
@@ -828,7 +828,7 @@ def get_last_x(self):
828828
"""
829829
return self._mouse_controller.position[0]
830830

831-
def get_last_y(self):
831+
def get_last_y(self) -> int:
832832
"""
833833
Get the last Y position for the mouse.
834834
@@ -837,7 +837,7 @@ def get_last_y(self):
837837
"""
838838
return self._mouse_controller.position[1]
839839

840-
def mouse_move(self, x, y):
840+
def mouse_move(self, x: int, y: int) -> None:
841841
"""
842842
Move the mouse to the coordinate defined by x and y
843843
@@ -849,7 +849,7 @@ def mouse_move(self, x, y):
849849
self._mouse_controller.position = (x, y)
850850
self.sleep(config.DEFAULT_SLEEP_AFTER_ACTION)
851851

852-
def click_at(self, x, y):
852+
def click_at(self, x: int, y: int) -> None:
853853
"""
854854
Click at the coordinate defined by x and y
855855
@@ -862,12 +862,12 @@ def click_at(self, x, y):
862862
@only_if_element
863863
def click(
864864
self,
865-
wait_after=config.DEFAULT_SLEEP_AFTER_ACTION,
865+
wait_after: int = config.DEFAULT_SLEEP_AFTER_ACTION,
866866
*,
867-
clicks=1,
868-
interval_between_clicks=0,
869-
button="left",
870-
):
867+
clicks: int = 1,
868+
interval_between_clicks: int = 0,
869+
button: str = "left",
870+
) -> None:
871871
"""
872872
Click on the last found element.
873873
@@ -878,6 +878,7 @@ def click(
878878
button (str, optional): One of 'left', 'right', 'middle'. Defaults to 'left'
879879
"""
880880
x, y = self.state.center()
881+
881882
_mouse_click(
882883
self._mouse_controller, x, y, clicks, interval_between_clicks, button
883884
)
@@ -886,14 +887,14 @@ def click(
886887
@only_if_element
887888
def click_relative(
888889
self,
889-
x,
890-
y,
891-
wait_after=config.DEFAULT_SLEEP_AFTER_ACTION,
890+
x: int,
891+
y: int,
892+
wait_after: int = config.DEFAULT_SLEEP_AFTER_ACTION,
892893
*,
893-
clicks=1,
894-
interval_between_clicks=0,
895-
button="left",
896-
):
894+
clicks: int = 1,
895+
interval_between_clicks: int = 0,
896+
button: str = "left",
897+
) -> None:
897898
"""
898899
Click Relative on the last found element.
899900
@@ -907,13 +908,14 @@ def click_relative(
907908
"""
908909
x = self.state.x() + x
909910
y = self.state.y() + y
911+
910912
_mouse_click(
911913
self._mouse_controller, x, y, clicks, interval_between_clicks, button
912914
)
913915
self.sleep(wait_after)
914916

915917
@only_if_element
916-
def double_click(self, wait_after=config.DEFAULT_SLEEP_AFTER_ACTION):
918+
def double_click(self, wait_after: int = config.DEFAULT_SLEEP_AFTER_ACTION) -> None:
917919
"""
918920
Double Click on the last found element.
919921
@@ -925,11 +927,11 @@ def double_click(self, wait_after=config.DEFAULT_SLEEP_AFTER_ACTION):
925927
@only_if_element
926928
def double_click_relative(
927929
self,
928-
x,
929-
y,
930-
interval_between_clicks=0,
931-
wait_after=config.DEFAULT_SLEEP_AFTER_ACTION,
932-
):
930+
x: int,
931+
y: int,
932+
interval_between_clicks: int = 0,
933+
wait_after: int = config.DEFAULT_SLEEP_AFTER_ACTION,
934+
) -> None:
933935
"""
934936
Double Click Relative on the last found element.
935937
@@ -949,7 +951,7 @@ def double_click_relative(
949951
)
950952

951953
@only_if_element
952-
def triple_click(self, wait_after=config.DEFAULT_SLEEP_AFTER_ACTION):
954+
def triple_click(self, wait_after: int = config.DEFAULT_SLEEP_AFTER_ACTION) -> None:
953955
"""
954956
Triple Click on the last found element.
955957
@@ -961,11 +963,11 @@ def triple_click(self, wait_after=config.DEFAULT_SLEEP_AFTER_ACTION):
961963
@only_if_element
962964
def triple_click_relative(
963965
self,
964-
x,
965-
y,
966-
interval_between_clicks=0,
967-
wait_after=config.DEFAULT_SLEEP_AFTER_ACTION,
968-
):
966+
x: int,
967+
y: int,
968+
interval_between_clicks: int = 0,
969+
wait_after: int = config.DEFAULT_SLEEP_AFTER_ACTION,
970+
) -> None:
969971
"""
970972
Triple Click Relative on the last found element.
971973
@@ -985,8 +987,11 @@ def triple_click_relative(
985987
)
986988

987989
def mouse_down(
988-
self, wait_after=config.DEFAULT_SLEEP_AFTER_ACTION, *, button="left"
989-
):
990+
self,
991+
wait_after: int = config.DEFAULT_SLEEP_AFTER_ACTION,
992+
*,
993+
button: str = "left",
994+
) -> None:
990995
"""
991996
Holds down the requested mouse button.
992997
@@ -998,7 +1003,12 @@ def mouse_down(
9981003
self._mouse_controller.press(mouse_button)
9991004
self.sleep(wait_after)
10001005

1001-
def mouse_up(self, wait_after=config.DEFAULT_SLEEP_AFTER_ACTION, *, button="left"):
1006+
def mouse_up(
1007+
self,
1008+
wait_after: int = config.DEFAULT_SLEEP_AFTER_ACTION,
1009+
*,
1010+
button: str = "left",
1011+
) -> None:
10021012
"""
10031013
Releases the requested mouse button.
10041014
@@ -1010,7 +1020,7 @@ def mouse_up(self, wait_after=config.DEFAULT_SLEEP_AFTER_ACTION, *, button="left
10101020
self._mouse_controller.release(mouse_button)
10111021
self.sleep(wait_after)
10121022

1013-
def scroll_down(self, clicks):
1023+
def scroll_down(self, clicks: int) -> None:
10141024
"""
10151025
Scroll Down n clicks
10161026
@@ -1020,7 +1030,7 @@ def scroll_down(self, clicks):
10201030
self._mouse_controller.scroll(0, -1 * clicks)
10211031
self.sleep(config.DEFAULT_SLEEP_AFTER_ACTION)
10221032

1023-
def scroll_up(self, clicks):
1033+
def scroll_up(self, clicks: int) -> None:
10241034
"""
10251035
Scroll Up n clicks
10261036
@@ -1031,15 +1041,15 @@ def scroll_up(self, clicks):
10311041
self.sleep(config.DEFAULT_SLEEP_AFTER_ACTION)
10321042

10331043
@only_if_element
1034-
def move(self):
1044+
def move(self) -> None:
10351045
"""
10361046
Move to the center position of last found item.
10371047
"""
10381048
x, y = self.state.center()
10391049
self._mouse_controller.position = (x, y)
10401050
self.sleep(config.DEFAULT_SLEEP_AFTER_ACTION)
10411051

1042-
def move_relative(self, x, y):
1052+
def move_relative(self, x: int, y: int) -> None:
10431053
"""
10441054
Move the mouse relative to its current position.
10451055
@@ -1053,7 +1063,7 @@ def move_relative(self, x, y):
10531063
self._mouse_controller.position = (x, y)
10541064
self.sleep(config.DEFAULT_SLEEP_AFTER_ACTION)
10551065

1056-
def move_random(self, range_x, range_y):
1066+
def move_random(self, range_x: int, range_y: int) -> None:
10571067
"""
10581068
Move randomly along the given x, y range.
10591069
@@ -1070,11 +1080,11 @@ def move_random(self, range_x, range_y):
10701080
@only_if_element
10711081
def right_click(
10721082
self,
1073-
wait_after=config.DEFAULT_SLEEP_AFTER_ACTION,
1083+
wait_after: int = config.DEFAULT_SLEEP_AFTER_ACTION,
10741084
*,
1075-
clicks=1,
1076-
interval_between_clicks=0,
1077-
):
1085+
clicks: int = 1,
1086+
interval_between_clicks: int = 0,
1087+
) -> None:
10781088
"""
10791089
Right click on the last found element.
10801090
@@ -1094,7 +1104,7 @@ def right_click(
10941104
)
10951105
self.sleep(wait_after)
10961106

1097-
def right_click_at(self, x, y):
1107+
def right_click_at(self, x: int, y: int) -> None:
10981108
"""
10991109
Right click at the coordinate defined by x and y
11001110
@@ -1107,11 +1117,11 @@ def right_click_at(self, x, y):
11071117
@only_if_element
11081118
def right_click_relative(
11091119
self,
1110-
x,
1111-
y,
1112-
interval_between_clicks=0,
1113-
wait_after=config.DEFAULT_SLEEP_AFTER_ACTION,
1114-
):
1120+
x: int,
1121+
y: int,
1122+
interval_between_clicks: int = 0,
1123+
wait_after: int = config.DEFAULT_SLEEP_AFTER_ACTION,
1124+
) -> None:
11151125
"""
11161126
Right Click Relative on the last found element.
11171127

botcity/core/input_utils.py

+35-27
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@
55
from pynput.mouse import Button, Controller
66

77
keys_map = {
8-
"num0": '0',
9-
"num1": '1',
10-
"num2": '2',
11-
"num3": '3',
12-
"num4": '4',
13-
"num5": '5',
14-
"num6": '6',
15-
"num7": '7',
16-
"num8": '8',
17-
"num9": '9',
18-
"add": '+',
19-
"decimal": ',',
20-
"subtract": '-',
21-
"multiply": '*',
22-
"divide": '/',
23-
"separator": '|',
8+
"num0": "0",
9+
"num1": "1",
10+
"num2": "2",
11+
"num3": "3",
12+
"num4": "4",
13+
"num5": "5",
14+
"num6": "6",
15+
"num7": "7",
16+
"num8": "8",
17+
"num9": "9",
18+
"add": "+",
19+
"decimal": ",",
20+
"subtract": "-",
21+
"multiply": "*",
22+
"divide": "/",
23+
"separator": "|",
2424
"altleft": Key.alt_l,
2525
"altright": Key.alt_r,
2626
"capslock": Key.caps_lock,
@@ -43,14 +43,14 @@
4343
"volumeup": Key.media_volume_up,
4444
"prevtrack": Key.media_previous,
4545
"nexttrack": Key.media_next,
46-
"return": Key.enter
46+
"return": Key.enter,
4747
}
4848

4949
if platform.system() != "Darwin":
5050
keys_map.update(
5151
{
5252
"numlock": Key.num_lock,
53-
"prtsc": Key.print_screen,
53+
"prtsc": Key.print_screen,
5454
"prtscr": Key.print_screen,
5555
"printscreen": Key.print_screen,
5656
"prntscrn": Key.print_screen,
@@ -59,25 +59,33 @@
5959
}
6060
)
6161

62-
mouse_map = {
63-
"left": Button.left,
64-
"right": Button.right,
65-
"middle": Button.middle
66-
}
62+
mouse_map = {"left": Button.left, "right": Button.right, "middle": Button.middle}
6763

6864

69-
def _mouse_click(mouse_controller: Controller, x: int, y: int, clicks=1, interval_between_clicks=0, button='left'):
65+
def _mouse_click(
66+
mouse_controller: Controller,
67+
x: int,
68+
y: int,
69+
clicks: int = 1,
70+
interval_between_clicks: int = 0,
71+
button: str = "left",
72+
) -> None:
7073
"""
7174
Moves the mouse and clicks at the coordinate defined by x and y.
7275
"""
7376
if platform.system() == "Darwin":
7477
from . import os_compat
75-
os_compat.osx_click(x=x, y=y, clicks=clicks, interval=interval_between_clicks, button=button)
78+
79+
os_compat.osx_click(
80+
x=x, y=y, clicks=clicks, interval=interval_between_clicks, button=button
81+
)
7682
else:
7783
mouse_button = mouse_map.get(button, None)
7884
if not mouse_button:
79-
raise ValueError(f'''Invalid mouse button name.
80-
The mouse button has to be one of these values: {list(mouse_map.keys())}''')
85+
raise ValueError(
86+
f"""Invalid mouse button name.
87+
The mouse button has to be one of these values: {list(mouse_map.keys())}"""
88+
)
8189

8290
mouse_controller.position = (x, y)
8391
time.sleep(0.1)

0 commit comments

Comments
 (0)