Skip to content

Commit

Permalink
Merge pull request #112 from glitchassassin/develop
Browse files Browse the repository at this point in the history
v0.7.1
  • Loading branch information
glitchassassin authored Dec 29, 2017
2 parents 614af6d + 41543ca commit 2a6f313
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 15 deletions.
6 changes: 3 additions & 3 deletions coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 56 additions & 0 deletions lackey/ImportHandler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import sys
import os.path

if sys.version_info[0] == 3:
from importlib.abc import MetaPathFinder
from importlib.util import spec_from_file_location
from importlib.machinery import SourceFileLoader

class SikuliFinder(MetaPathFinder):
def find_spec(self, fullname, path, target=None):
if "." in fullname:
name = fullname.split(".")[-1]
else:
name = fullname
for entry in sys.path:
if entry == "":
entry = os.getcwd()
sikuli_path = os.path.join(entry, "{}.sikuli".format(name))
filename = os.path.join(sikuli_path, "{}.py".format(name))
if not os.path.exists(filename):
continue

# Found what we're looking for. Add to path.
sys.path.append(sikuli_path)

return spec_from_file_location(fullname, filename, loader=SourceFileLoader(fullname, filename),
submodule_search_locations=None)

return None # we don't know how to import this
sys.meta_path.append(SikuliFinder())
elif sys.version_info[0] == 2:
import imp

class SikuliFinder(object):
def __init__(self, path):
self.path = path

@classmethod
def find_module(cls, name, path=None):
for entry in sys.path:
sikuli_path = os.path.join(entry, "{}.sikuli".format(name))
filename = os.path.join(sikuli_path, "{}.py".format(name))
if not os.path.exists(filename):
continue

# Found what we're looking for. Add to path.
sys.path.append(sikuli_path)
return cls(filename)

def load_module(self, name):
if name in sys.modules:
return sys.modules[name]
with open(self.path, "r") as project:
mod = imp.load_module(name, project, self.path, (".py", "r", imp.PY_SOURCE))
return mod
sys.meta_path.append(SikuliFinder)
19 changes: 12 additions & 7 deletions lackey/RegionMatching.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def setFilename(self, filename):
""" Set the filename of the pattern's image (and load it) """
## Loop through image paths to find the image
found = False
for image_path in [Settings.BundlePath, os.getcwd()] + Settings.ImagePaths:
for image_path in sys.path + [Settings.BundlePath, os.getcwd()] + Settings.ImagePaths:
full_path = os.path.join(image_path, filename)
if os.path.exists(full_path):
# Image file not found
Expand Down Expand Up @@ -1015,33 +1015,38 @@ def write(self, text):
def delayType(millisecs):
Settings.TypeDelay = millisecs
def isRegionValid(self):
""" Returns false if the whole region is outside any screen, otherwise true """
""" Returns false if the whole region is not even partially inside any screen, otherwise true """
screens = PlatformManager.getScreenDetails()
for screen in screens:
s_x, s_y, s_w, s_h = screen["rect"]
if (self.x+self.w < s_x or s_x+s_w < self.x or self.y+self.h < s_y or s_y+s_h < self.y):
if self.x+self.w >= s_x and s_x+s_w >= self.x and self.y+self.h >= s_y and s_y+s_h >= self.y:
# Rects overlap
return False
return True
return True
return False

def clipRegionToScreen(self):
""" Returns the part of the region that is visible on a screen
If the region equals to all visible screens, returns Screen(-1).
If the region is visible on multiple screens, returns the screen with the smallest ID.
Returns None if the region is outside the screen.
"""
if not self.isRegionValid():
return None
screens = PlatformManager.getScreenDetails()
total_x, total_y, total_w, total_h = Screen(-1).getBounds()
containing_screen = None
for screen in screens:
s_x, s_y, s_w, s_h = screen["rect"]
if self.x >= s_x and self.x+self.w <= s_x+s_w and self.y >= s_y and self.y+self.h <= s_y+s_h:
# Region completely inside screen
return self
elif self.x+self.w < s_x or s_x+s_w < self.x or self.y+self.h < s_y or s_y+s_h < self.y:
elif self.x+self.w <= s_x or s_x+s_w <= self.x or self.y+self.h <= s_y or s_y+s_h <= self.y:
# Region completely outside screen
continue
elif self.x == total_x and self.y == total_y and self.w == total_w and self.h == total_h:
# Region equals all screens, Screen(-1)
return self
else:
# Region partially inside screen
x = max(self.x, s_x)
Expand Down Expand Up @@ -1903,7 +1908,7 @@ def showMonitors(cls):
Debug.info("*** monitor configuration [ {} Screen(s)] ***".format(cls.getNumberScreens()))
Debug.info("*** Primary is Screen {}".format(cls.primaryScreen))
for index, screen in enumerate(PlatformManager.getScreenDetails()):
Debug.info("Screen {}: ({}, {}, {}, {})".format(index, *screen[rect]))
Debug.info("Screen {}: ({}, {}, {}, {})".format(index, *screen["rect"]))
Debug.info("*** end monitor configuration ***")
def resetMonitors(self):
""" Recalculates screen based on changed monitor setup """
Expand Down
2 changes: 2 additions & 0 deletions lackey/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
from .SikuliGui import PopupInput, PopupList, PopupTextarea
from ._version import __version__

from . import ImportHandler

VALID_PLATFORMS = ["Windows", "Darwin"]

## Define script abort hotkey (Alt+Shift+C)
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.7.0"
__version__ = "0.7.1"
__sikuli_version__ = "1.1.0"
Binary file added tests/preview_open_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 13 additions & 4 deletions tests/test_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ def setUp(self):
lackey.Screen(0).hover()
lackey.Screen(0).click()

def testImporter(self):
""" Tries to import the test_cases project file
(ignores FindFailed exception thrown by project) """
try:
sys.path.append(os.path.join(os.getcwd(), "tests"))
import test_import
except lackey.FindFailed:
pass

def testTypeCopyPaste(self):
""" Also tests the log file """
lackey.Debug.setLogFile("logfile.txt")
Expand All @@ -90,8 +99,8 @@ def testTypeCopyPaste(self):
app = lackey.App("+open -e")
lackey.sleep(2)
#r.debugPreview()
r.wait(lackey.Pattern("preview_open.png"), lackey.FOREVER)
r.click(lackey.Pattern("preview_open.png"))
r.wait(lackey.Pattern("preview_open_2.png"))
r.click(lackey.Pattern("preview_open_2.png"))
lackey.type("n", lackey.KeyModifier.CMD)
time.sleep(1)
app = lackey.App("Untitled")
Expand Down Expand Up @@ -129,7 +138,7 @@ def test_observer(appear_event):
r.doubleClick("notepad.png")
elif sys.platform == "darwin":
r.doubleClick("textedit.png")
r.wait("preview_open.png")
r.wait("preview_open_2.png")
r.type("n", lackey.KeyModifier.CMD)
time.sleep(2)
r.type("This is a test")
Expand All @@ -153,7 +162,7 @@ def test_observer(appear_event):
r.type("{F4}", lackey.Key.ALT)
elif sys.platform == "darwin":
r.type("w", lackey.KeyModifier.CMD)
r.click(lackey.Pattern("textedit_save.png").targetOffset(-86, 41))
r.click(lackey.Pattern("textedit_save_2.png").targetOffset(-86, 25))
lackey.sleep(0.5)
r.type("q", lackey.KeyModifier.CMD)

Expand Down
Binary file added tests/test_import.sikuli/1514401659995.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
76 changes: 76 additions & 0 deletions tests/test_import.sikuli/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@

<html>
<head>
<style type="text/css">
.sikuli-code {
font-size: 20px;
font-family: "Osaka-mono", Monospace;
line-height: 1.5em;
display:table-cell;
white-space: pre-wrap; /* css-3 */
white-space: -moz-pre-wrap !important; /* Mozilla, since 1999 */
white-space: -pre-wrap; /* Opera 4-6 */
white-space: -o-pre-wrap; /* Opera 7 */
word-wrap: break-word; /* Internet Explorer 5.5+ */
width: 99%; /* remove horizontal scroll-bar when viewing in IE7 */
}
.sikuli-code img {
vertical-align: middle;
margin: 2px;
border: 1px solid #ccc;
padding: 2px;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
-moz-box-shadow: 1px 1px 1px gray;
-webkit-box-shadow: 1px 1px 2px gray;
}
.kw {
color: blue;
}
.skw {
color: rgb(63, 127, 127);
}

.str {
color: rgb(128, 0, 0);
}

.dig {
color: rgb(128, 64, 0);
}

.cmt {
color: rgb(200, 0, 200);
}

h2 {
display: inline;
font-weight: normal;
}

.info {
border-bottom: 1px solid #ddd;
padding-bottom: 5px;
margin-bottom: 20px;
display: none;
}

a {
color: #9D2900;
}

body {
font-family: "Trebuchet MS", Arial, Sans-Serif;
}

</style>
</head>
<body>
<div class="info">
<h2>test.sikuli</h2> <a href="test.zip">(Download this script)</a>
</div>
<pre class="sikuli-code">
<span class="skw">find</span>(<img src="1514401659995.png" />)
</pre>
</body>
</html>
4 changes: 4 additions & 0 deletions tests/test_import.sikuli/test_import.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import sys
from lackey import *

find("1514401659995.png")
Binary file added tests/textedit_save_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2a6f313

Please sign in to comment.