Skip to content

Commit

Permalink
Merge pull request #73 from glitchassassin/develop
Browse files Browse the repository at this point in the history
v0.5.6
  • Loading branch information
glitchassassin authored Mar 21, 2017
2 parents bf6fd1a + f611f86 commit fdf10ba
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 190 deletions.
8 changes: 6 additions & 2 deletions lackey/App.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import re
import time
import pyperclip
import platform
import subprocess

Expand Down Expand Up @@ -37,6 +38,7 @@ def __init__(self, identifier=None):
self._exec = ""
self._params = ""
self._process = None
self._devnull = None
self._defaultScanRate = 0.1
self.proc = None

Expand Down Expand Up @@ -148,6 +150,7 @@ def close(cls, appName):
def _close_instance(self):
if self._process:
self._process.terminate()
self._devnull.close()
elif self.getPID() != -1:
PlatformManager.killProcess(self.getPID())

Expand All @@ -162,7 +165,8 @@ def open(self, executable):
def _open_instance(self, waitTime=0):
if self._exec != "":
# Open from an executable + parameters
self._process = subprocess.Popen([self._exec] + self._params, shell=False)
self._devnull = open(os.devnull, 'w')
self._process = subprocess.Popen([self._exec] + self._params, shell=False, stderr=self._devnull, stdout=self._devnull)
self._pid = self._process.pid
elif self._title != "":
# Capture an existing window that matches self._title
Expand Down Expand Up @@ -240,4 +244,4 @@ def isRunning(self, waitTime=0):
@classmethod
def getClipboard(cls):
""" Gets the contents of the clipboard (as classmethod) """
return PlatformManager.getClipboard()
return pyperclip.paste()
6 changes: 3 additions & 3 deletions lackey/KeyCodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ class Button():
RIGHT = 2

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

class KeyModifier():
""" Key modifiers precede either a single key [e.g., ^v] or a set of characters within parentheses [e.g., +(hello)] """
""" Can be used with type() to modify another key, e.g. ``type(Key.DELETE, Key.CTRL+Key.ALT)`` """
CTRL = "{CTRL}"
SHIFT = "{SHIFT}"
ALT = "{ALT}"
Expand Down
27 changes: 11 additions & 16 deletions lackey/RegionMatching.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,11 +400,6 @@ def findAll(self, pattern):
raise FindFailed("Region outside all visible screens")
return None
seconds = self.autoWaitTimeout
if isinstance(pattern, int):
time.sleep(pattern)
return
if not pattern:
time.sleep(seconds)
if not isinstance(pattern, Pattern):
if not isinstance(pattern, basestring):
raise TypeError("find expected a string [image path] or Pattern object")
Expand Down Expand Up @@ -442,16 +437,21 @@ def findAll(self, pattern):
self._lastMatchTime = (time.time() - find_time) * 1000 # Capture find time in milliseconds
return self._lastMatches

def wait(self, pattern, seconds=3):
def wait(self, pattern, seconds=None):
""" Searches for an image pattern in the given region, given a specified timeout period
Functionally identical to find()
Functionally identical to find(). If a number is passed instead of a pattern,
just waits the specified number of seconds.
Sikuli supports OCR search with a text parameter. This does not (yet).
"""
if seconds:
timeout = time.time() + seconds
else:
timeout = time.time()
if isinstance(pattern, (int, float)):
time.sleep(pattern)
return None

if seconds is None:
seconds = self.autoWaitTimeout

timeout = time.time() + seconds
while True:
match = self.exists(pattern)
if match:
Expand All @@ -473,11 +473,6 @@ def waitVanish(self, pattern, seconds=None):
return None
if seconds is None:
seconds = self.autoWaitTimeout
if isinstance(pattern, int):
time.sleep(pattern)
return
if not pattern:
time.sleep(seconds)
if not isinstance(pattern, Pattern):
if not isinstance(pattern, basestring):
raise TypeError("find expected a string [image path] or Pattern object")
Expand Down
17 changes: 14 additions & 3 deletions lackey/Settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import os
import __main__

from io import open # For Python 2 native line endings compatibility

from ._version import __version__, __sikuli_version__

class DebugMaster(object):
Expand Down Expand Up @@ -98,7 +100,13 @@ def setLoggerDebug(self, mthd):
""" Sends debug messages to ``logger.[mthd]()`` for handling """
self._logger_methods["debug"] = mthd
def setLogFile(self, filepath):
""" Defines the file to which output log messages should be sent """
""" Defines the file to which output log messages should be sent.
Set to `None` to print to STDOUT instead.
"""
if filepath is None:
self._log_file = None
return
parsed_path = os.path.abspath(filepath)
# Checks if the provided log filename is in a real directory, and that
# the filename itself is not a directory.
Expand All @@ -119,8 +127,11 @@ def _write_log(self, log_type, log_time, message):
)(message if self._logger_no_prefix else log_entry)
elif self._log_file:
# Otherwise write to file, if a file has been specified
with open(self._log_file) as logfile:
logfile.write(log_entry)
with open(self._log_file, 'a') as logfile:
try:
logfile.write(unicode(log_entry + "\n"))
except NameError: # `unicode` only works in Python 2
logfile.write(log_entry + "\n")
else:
# Otherwise, print to STDOUT
print(log_entry)
Expand Down
10 changes: 9 additions & 1 deletion lackey/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@

import platform
import keyboard
try:
import thread
except ImportError:
import _thread as thread
import sys
import time
import os
Expand All @@ -36,7 +40,11 @@
VALID_PLATFORMS = ["Windows"]

## Define script abort hotkey (Alt+Shift+C)
keyboard.add_hotkey("alt+shift+c", sys.exit)

def _abort_script():
thread.interrupt_main()

keyboard.add_hotkey("alt+shift+c", _abort_script, suppress=True)

## Sikuli patching: Functions that map to the global Screen region
## Don't try this at home, kids!
Expand Down
2 changes: 1 addition & 1 deletion lackey/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"""

__version__ = "0.5.5"
__version__ = "0.5.6"
__sikuli_version__ = "1.1.0"
Loading

0 comments on commit fdf10ba

Please sign in to comment.