Skip to content

Commit 2910770

Browse files
committed
Squashed commit of the following:
commit d95e646 Author: Dave Amies <[email protected]> Date: Sun Feb 27 13:23:14 2022 +1000 Add timeout option to keywords - Issue eficode#61 Keywords that now have the optional timeout are: - Click Image - Click To The Above Of Image - Click To The Below Of Image - Click To The Left Of Image - Click To The Right Of Image - Copy From The Above Of - Copy From The Below Of - Copy From The Left Of - Copy From The Right Of The default timeout is 0 sec so there is no functional change from previous version unless a timeout is set, this does require Pull request eficode#64 (fix for issue eficode#60) as a prerequisite for this change in order to work as expected. Please only merge after Pull request eficode#64
1 parent 4130dec commit 2910770

File tree

1 file changed

+29
-22
lines changed

1 file changed

+29
-22
lines changed

src/ImageHorizonLibrary/recognition/_recognize_images.py

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
class _RecognizeImages(object):
1414

15+
dflt_timeout = 0
1516
pixel_ratio = 0.0
1617

1718
def __get_pixel_ratio(self):
@@ -33,13 +34,15 @@ def __normalize(self, path):
3334
raise InvalidImageException('Image path not found: "%s".' % path)
3435
return path
3536

36-
def click_image(self, reference_image):
37-
'''Finds the reference image on screen and clicks it once.
37+
def click_image(self, reference_image, timeout=dflt_timeout):
38+
'''Finds the reference image on screen and clicks it's center point once.
3839
3940
``reference_image`` is automatically normalized as described in the
4041
`Reference image names`.
42+
43+
``timeout`` optional value, in whole seconds. default is 0
4144
'''
42-
center_location = self.locate(reference_image)
45+
center_location = self.wait_for(reference_image, timeout)
4346
LOGGER.info('Clicking image "%s" in position %s' % (reference_image,
4447
center_location))
4548
ag.click(center_location)
@@ -50,13 +53,13 @@ def _click_to_the_direction_of(self, direction, location, offset,
5053
raise NotImplementedError('This is defined in the main class.')
5154

5255
def _locate_and_click_direction(self, direction, reference_image, offset,
53-
clicks, button, interval):
54-
location = self.locate(reference_image)
56+
clicks, button, interval, timeout=dflt_timeout):
57+
location = self.wait_for(reference_image, timeout)
5558
self._click_to_the_direction_of(direction, location, offset, clicks,
5659
button, interval)
5760

5861
def click_to_the_above_of_image(self, reference_image, offset, clicks=1,
59-
button='left', interval=0.0):
62+
button='left', interval=0.0, timeout=dflt_timeout):
6063
'''Clicks above of reference image by given offset.
6164
6265
See `Reference image names` for documentation for ``reference_image``.
@@ -65,38 +68,40 @@ def click_to_the_above_of_image(self, reference_image, offset, clicks=1,
6568
image.
6669
6770
``clicks`` and ``button`` are documented in `Click To The Above Of`.
71+
72+
``timeout`` optional value, in whole seconds. default is 0
6873
'''
6974
self._locate_and_click_direction('up', reference_image, offset,
70-
clicks, button, interval)
75+
clicks, button, interval, timeout)
7176

7277
def click_to_the_below_of_image(self, reference_image, offset, clicks=1,
73-
button='left', interval=0.0):
78+
button='left', interval=0.0, timeout=dflt_timeout):
7479
'''Clicks below of reference image by given offset.
7580
7681
See argument documentation in `Click To The Above Of Image`.
7782
'''
7883
self._locate_and_click_direction('down', reference_image, offset,
79-
clicks, button, interval)
84+
clicks, button, interval, timeout)
8085

8186
def click_to_the_left_of_image(self, reference_image, offset, clicks=1,
82-
button='left', interval=0.0):
87+
button='left', interval=0.0, timeout=dflt_timeout):
8388
'''Clicks left of reference image by given offset.
8489
8590
See argument documentation in `Click To The Above Of Image`.
8691
'''
8792
self._locate_and_click_direction('left', reference_image, offset,
88-
clicks, button, interval)
93+
clicks, button, interval, timeout)
8994

9095
def click_to_the_right_of_image(self, reference_image, offset, clicks=1,
91-
button='left', interval=0.0):
96+
button='left', interval=0.0, timeout=dflt_timeout):
9297
'''Clicks right of reference image by given offset.
9398
9499
See argument documentation in `Click To The Above Of Image`.
95100
'''
96101
self._locate_and_click_direction('right', reference_image, offset,
97-
clicks, button, interval)
102+
clicks, button, interval, timeout)
98103

99-
def copy_from_the_above_of(self, reference_image, offset):
104+
def copy_from_the_above_of(self, reference_image, offset, timeout=dflt_timeout):
100105
'''Clicks three times above of reference image by given offset and
101106
copies.
102107
@@ -106,39 +111,41 @@ def copy_from_the_above_of(self, reference_image, offset):
106111
107112
Copy is done by pressing ``Ctrl+C`` on Windows and Linux and ``⌘+C``
108113
on OS X.
114+
115+
``timeout`` optional value, in whole seconds. default is 0
109116
'''
110117
self._locate_and_click_direction('up', reference_image, offset,
111-
clicks=3, button='left', interval=0.0)
118+
clicks=3, button='left', interval=0.0, timeout=timeout)
112119
return self.copy()
113120

114-
def copy_from_the_below_of(self, reference_image, offset):
121+
def copy_from_the_below_of(self, reference_image, offset, timeout=dflt_timeout):
115122
'''Clicks three times below of reference image by given offset and
116123
copies.
117124
118125
See argument documentation in `Copy From The Above Of`.
119126
'''
120127
self._locate_and_click_direction('down', reference_image, offset,
121-
clicks=3, button='left', interval=0.0)
128+
clicks=3, button='left', interval=0.0, timeout=timeout)
122129
return self.copy()
123130

124-
def copy_from_the_left_of(self, reference_image, offset):
131+
def copy_from_the_left_of(self, reference_image, offset, timeout=dflt_timeout):
125132
'''Clicks three times left of reference image by given offset and
126133
copies.
127134
128135
See argument documentation in `Copy From The Above Of`.
129136
'''
130137
self._locate_and_click_direction('left', reference_image, offset,
131-
clicks=3, button='left', interval=0.0)
138+
clicks=3, button='left', interval=0.0, timeout=timeout)
132139
return self.copy()
133140

134-
def copy_from_the_right_of(self, reference_image, offset):
141+
def copy_from_the_right_of(self, reference_image, offset, timeout=dflt_timeout):
135142
'''Clicks three times right of reference image by given offset and
136143
copies.
137144
138145
See argument documentation in `Copy From The Above Of`.
139146
'''
140147
self._locate_and_click_direction('right', reference_image, offset,
141-
clicks=3, button='left', interval=0.0)
148+
clicks=3, button='left', interval=0.0, timeout=timeout)
142149
return self.copy()
143150

144151
@contextmanager
@@ -244,7 +251,7 @@ def wait_for(self, reference_image, timeout=10):
244251
245252
See `Reference image names` for documentation for ``reference_image``.
246253
247-
``timeout`` is given in seconds.
254+
``timeout`` is given in whole seconds.
248255
249256
Returns Python tuple ``(x, y)`` of the coordinates.
250257
'''

0 commit comments

Comments
 (0)