Skip to content

Commit fdf10ba

Browse files
Merge pull request #73 from glitchassassin/develop
v0.5.6
2 parents bf6fd1a + f611f86 commit fdf10ba

File tree

7 files changed

+216
-190
lines changed

7 files changed

+216
-190
lines changed

lackey/App.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
import re
44
import time
5+
import pyperclip
56
import platform
67
import subprocess
78

@@ -37,6 +38,7 @@ def __init__(self, identifier=None):
3738
self._exec = ""
3839
self._params = ""
3940
self._process = None
41+
self._devnull = None
4042
self._defaultScanRate = 0.1
4143
self.proc = None
4244

@@ -148,6 +150,7 @@ def close(cls, appName):
148150
def _close_instance(self):
149151
if self._process:
150152
self._process.terminate()
153+
self._devnull.close()
151154
elif self.getPID() != -1:
152155
PlatformManager.killProcess(self.getPID())
153156

@@ -162,7 +165,8 @@ def open(self, executable):
162165
def _open_instance(self, waitTime=0):
163166
if self._exec != "":
164167
# Open from an executable + parameters
165-
self._process = subprocess.Popen([self._exec] + self._params, shell=False)
168+
self._devnull = open(os.devnull, 'w')
169+
self._process = subprocess.Popen([self._exec] + self._params, shell=False, stderr=self._devnull, stdout=self._devnull)
166170
self._pid = self._process.pid
167171
elif self._title != "":
168172
# Capture an existing window that matches self._title
@@ -240,4 +244,4 @@ def isRunning(self, waitTime=0):
240244
@classmethod
241245
def getClipboard(cls):
242246
""" Gets the contents of the clipboard (as classmethod) """
243-
return PlatformManager.getClipboard()
247+
return pyperclip.paste()

lackey/KeyCodes.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ class Button():
44
RIGHT = 2
55

66
class Key():
7-
""" Key codes for PlatformManager.TypeKeys() function.
7+
""" Key codes for InputEmulation.Keyboard object.
88
9-
Can be entered directly or concatenated with an existing string. """
9+
Can be entered directly or concatenated with an existing string, e.g. ``type(Key.TAB)`` """
1010
ENTER = "{ENTER}"
1111
ESC = "{ESC}"
1212
BACKSPACE = "{BACKSPACE}"
@@ -66,7 +66,7 @@ class Key():
6666
DIVIDE = "{DIVIDE}"
6767

6868
class KeyModifier():
69-
""" Key modifiers precede either a single key [e.g., ^v] or a set of characters within parentheses [e.g., +(hello)] """
69+
""" Can be used with type() to modify another key, e.g. ``type(Key.DELETE, Key.CTRL+Key.ALT)`` """
7070
CTRL = "{CTRL}"
7171
SHIFT = "{SHIFT}"
7272
ALT = "{ALT}"

lackey/RegionMatching.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -400,11 +400,6 @@ def findAll(self, pattern):
400400
raise FindFailed("Region outside all visible screens")
401401
return None
402402
seconds = self.autoWaitTimeout
403-
if isinstance(pattern, int):
404-
time.sleep(pattern)
405-
return
406-
if not pattern:
407-
time.sleep(seconds)
408403
if not isinstance(pattern, Pattern):
409404
if not isinstance(pattern, basestring):
410405
raise TypeError("find expected a string [image path] or Pattern object")
@@ -442,16 +437,21 @@ def findAll(self, pattern):
442437
self._lastMatchTime = (time.time() - find_time) * 1000 # Capture find time in milliseconds
443438
return self._lastMatches
444439

445-
def wait(self, pattern, seconds=3):
440+
def wait(self, pattern, seconds=None):
446441
""" Searches for an image pattern in the given region, given a specified timeout period
447442
448-
Functionally identical to find()
443+
Functionally identical to find(). If a number is passed instead of a pattern,
444+
just waits the specified number of seconds.
449445
Sikuli supports OCR search with a text parameter. This does not (yet).
450446
"""
451-
if seconds:
452-
timeout = time.time() + seconds
453-
else:
454-
timeout = time.time()
447+
if isinstance(pattern, (int, float)):
448+
time.sleep(pattern)
449+
return None
450+
451+
if seconds is None:
452+
seconds = self.autoWaitTimeout
453+
454+
timeout = time.time() + seconds
455455
while True:
456456
match = self.exists(pattern)
457457
if match:
@@ -473,11 +473,6 @@ def waitVanish(self, pattern, seconds=None):
473473
return None
474474
if seconds is None:
475475
seconds = self.autoWaitTimeout
476-
if isinstance(pattern, int):
477-
time.sleep(pattern)
478-
return
479-
if not pattern:
480-
time.sleep(seconds)
481476
if not isinstance(pattern, Pattern):
482477
if not isinstance(pattern, basestring):
483478
raise TypeError("find expected a string [image path] or Pattern object")

lackey/Settings.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import os
44
import __main__
55

6+
from io import open # For Python 2 native line endings compatibility
7+
68
from ._version import __version__, __sikuli_version__
79

810
class DebugMaster(object):
@@ -98,7 +100,13 @@ def setLoggerDebug(self, mthd):
98100
""" Sends debug messages to ``logger.[mthd]()`` for handling """
99101
self._logger_methods["debug"] = mthd
100102
def setLogFile(self, filepath):
101-
""" Defines the file to which output log messages should be sent """
103+
""" Defines the file to which output log messages should be sent.
104+
105+
Set to `None` to print to STDOUT instead.
106+
"""
107+
if filepath is None:
108+
self._log_file = None
109+
return
102110
parsed_path = os.path.abspath(filepath)
103111
# Checks if the provided log filename is in a real directory, and that
104112
# the filename itself is not a directory.
@@ -119,8 +127,11 @@ def _write_log(self, log_type, log_time, message):
119127
)(message if self._logger_no_prefix else log_entry)
120128
elif self._log_file:
121129
# Otherwise write to file, if a file has been specified
122-
with open(self._log_file) as logfile:
123-
logfile.write(log_entry)
130+
with open(self._log_file, 'a') as logfile:
131+
try:
132+
logfile.write(unicode(log_entry + "\n"))
133+
except NameError: # `unicode` only works in Python 2
134+
logfile.write(log_entry + "\n")
124135
else:
125136
# Otherwise, print to STDOUT
126137
print(log_entry)

lackey/__init__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515

1616
import platform
1717
import keyboard
18+
try:
19+
import thread
20+
except ImportError:
21+
import _thread as thread
1822
import sys
1923
import time
2024
import os
@@ -36,7 +40,11 @@
3640
VALID_PLATFORMS = ["Windows"]
3741

3842
## Define script abort hotkey (Alt+Shift+C)
39-
keyboard.add_hotkey("alt+shift+c", sys.exit)
43+
44+
def _abort_script():
45+
thread.interrupt_main()
46+
47+
keyboard.add_hotkey("alt+shift+c", _abort_script, suppress=True)
4048

4149
## Sikuli patching: Functions that map to the global Screen region
4250
## Don't try this at home, kids!

lackey/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
33
"""
44

5-
__version__ = "0.5.5"
5+
__version__ = "0.5.6"
66
__sikuli_version__ = "1.1.0"

0 commit comments

Comments
 (0)