← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #268512]: Import Python Module from Jar

 

Question #268512 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/268512

RaiMan posted a new comment:
ok, thanks for the feedback and additional information.

--- with version 1.0.1
- load() does not search on sys.path for the jar, so in your case you have to use:
load("<path-to>/mystuff.jar")
import module1.py

if you stick to the solution, you found, then use
if not "<path-to>" in sys.path: sys.path.append("<path-to>")

to avoid double entries (this is what load assures)

--- with version 1.1.0
Motivated by your request I revised the load() feature (the java docs):

  /**
   * add a jar to the scripting environment<br>
   * Jython: added to sys.path<br>
   * JRuby: not yet supported<br>
   * JavaScript: not yet supported<br>
   * if no scripting active (API usage), jar is added to classpath if available
   * @param fpJar absolute path to a jar (relative: searched according to Extension concept)
   * @return the absolute path to the jar or null, if not available
   */
  public static String load(String fpJar) {

  /**
   * add a jar to the scripting environment or to classpath<br>
   * Jython: added to sys.path<br>
   * JRuby: only added to classpath<br>
   * JavaScript: only added to classpath<br>
   * if no scripting is active (API usage), jar is added to classpath if available<br>
   * additionally: fpJar/fpJarImagePath is added to ImagePath (not checked)
   * @param fpJar absolute path to a jar (relative: searched according to Extension concept)
   * @param fpJarImagePath path relative to jar root inside jar
   * @return the absolute path to the jar or null, if not available
   */
  public static String load(String fpJar, String fpJarImagePath) {

... and I added support functions for your situation:

  /**
   * build a jar on the fly at runtime from a folder.<br>
   * special for Jython: if the folder contains a __init__.py on first level, 
   * the folder will be copied to the jar root (hence preserving module folders)
   * @param targetJar absolute path to the created jar (parent folder must exist, jar is overwritten) 
   * @param sourceFolder absolute path to a folder, the contained folder structure 
   * will be copied to the jar root level
   * @return
   */
  public static boolean buildJarFromFolder(String targetJar, String sourceFolder) {

/**
   * the foo.py files in the given source folder are compiled to JVM-ByteCode-classfiles foo$py.class 
   * and stored in the target folder (thus securing your code against changes).<br>
   * A folder structure is preserved. All files not ending as .py will be copied also.
   * The target folder might then be packed to a jar using buildJarFromFolder.<br>
   * Be aware: you will get no feedback about any compile problems, 
   * so make sure your code compiles error free. Currently there is no support for running such a jar,
   * it can only be used with load()/import, but you might provide a simple script that does load()/import
   * and then runs something based on available functions in the jar code.
   * @param fpSource
   * @param fpTarget
   * @return false if anything goes wrong, true means should have worked
   */
  public static boolean compileJythonFolder(String fpSource, String fpTarget) {

the stuff is not ready yet, but will surely be in the next nightly after
today.

In the scripting context, these features are available as 
Sikulix.compileJythonFolder(source, target)

this compileJythonFolder feature would finally solve your should-not-be-
changed-problem:

Sikulix.compileJythonFolder(source, target)
Sikulix. buildJarFromFolder(finalJar, target)

... and you have a finalJar, that does not contain .py files anymore,
but their compiled versions ...$py.class, that cannot be modified.

Be aware: these features will not be available in version 1.0.1, but only in 1.1.0+
So I recommend to start testing with 1.1.0 as soon as possible and plan your productive environment with 1.1.0 final version (available during the next week)

-- 
You received this question notification because you are a member of
Sikuli Drivers, which is an answer contact for Sikuli.