Thursday 8 March 2012

Moving a function into a module

Encapsulation is very important for Sikuli scripts. Because progress can be made very quickly with the tool, and because the process does not necessarily resemble formal programming it is really easy to forget basic software design principles.

Sikuli Library Script

  1. Move the function into a new file
  2. Make the encapsulated import script Sikuli aware.
  3. from sikuli import *

..\\mLib.sikuli\\HelloWorldLib.py
# Import Script - Filename: .\HelloWorldLib.sikuli
from sikuli import *

# Instantiate a Hello World Popup Window
# The parameters are comma delimited
def helloWorld(Title, Body):
    popup( Body, Title)

Sikuli Main Script
  1. Add the import script path to the Jython path
  2. myScriptPath = "c:\\..\\<library>\\"
    if not myScriptPath in sys.path: sys.path.append(myScriptPath)
  3. import the library
  4. import <library>
  5. Reload the library to incorporate changes
  6. reload(<library>)
  7. Add the library image path to the Sikuli image path
  8. myImagePath = "c:\\..\\<image library>\\"
    addImagePath(myImagePath)
  9. Make the call to the library
  10. library.function()

..\\HelloWorld.sikuli\\HelloWorld.py
# Jython will recursively search this path for .py files
# Backslash must be escaped. \\ instead of \
myScriptPath = "c:\\Documents and Settings\\admin\\My Documents\\Sikuli\\mLib.sikuli"

# Since the Sikuli IDE does not reload the modules while running a script each
# time, Import and reload the library to make sure new changes are incorporated
import HelloWorldLib
reload(HelloWorldLib)

# Add search path for images
myDefaultImagePath = "c:\\Documents and Settings\\admin\\My Documents\\Sikuli\\images\\"
myImagePath = "c:\\Documents and Settings\\admin\\My Documents\\Sikuli\\mLib.sikuli\\images"
addImagePath(myDefaultImagePath)
addImagePath(myImagePath)


# The parameter order is Title, Body
HelloWorldLib.helloWorld("Hello", "World")

As always, check all the changes into source control.


Further Reading:
Importing Sikuli Scripts¶
Image Search Path



03-08-2012
Sikuli X-1.0rc3 (r905)

No comments:

Post a Comment