Create new site-packages feature for Python Mode. Fix
MODIFY
runtime/src/jycessing/Runner.java
runtime/src/jycessing/Runner.java
@@ -29,6 +29,7 @@ import java.util.HashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;
+import java.util.TreeSet;
import org.python.core.Py;
import org.python.core.PyList;
@@ -295,15 +296,12 @@ public class Runner {
final StringBuilder pythonPath = new StringBuilder();
- final List<File> libDirs = sketch.getLibraryDirectories();
-
// The code may be located in a temp dir, if the sketch is unsaved.
final String actualCodeLocation = sketch.getMainFile().getParentFile().getAbsolutePath();
pythonPath.append(File.pathSeparator).append(actualCodeLocation);
final String sketchDirPath = sketch.getHomeDirectory().getAbsolutePath();
pythonPath.append(File.pathSeparator).append(sketchDirPath);
-
props.setProperty("python.path", pythonPath.toString());
props.setProperty("python.main", sketch.getMainFile().getAbsolutePath());
props.setProperty("python.main.root", sketchDirPath);
@@ -326,15 +324,16 @@ public class Runner {
// Add all of the sketch's requested sys.path entries, and add all jar
// files found there, recursively.
- final Set<String> userLibs = new HashSet<>();
+ final Set<String> userLibs = new TreeSet<>();
for (final File entry : sketch.getPathEntries()) {
- sys.path.append(Py.newString(entry.getAbsolutePath()));
+ userLibs.add(entry.getAbsolutePath());
searchForExtraStuff(entry, userLibs);
}
// Add the add_library function to the sketch namespace.
+ final List<File> libDirs = sketch.getLibraryDirectories();
if (libDirs != null) {
- new LibraryImporter(sketch.getLibraryDirectories(), interp);
+ new LibraryImporter(libDirs, interp);
if (sketch.getLibraryPolicy() == LibraryPolicy.PROMISCUOUS) {
log("Promiscusouly adding all libraries in " + libDirs);
@@ -345,7 +344,7 @@ public class Runner {
searchForExtraStuff(dir, libs);
}
for (final String lib : libs) {
- sys.path.insert(0, Py.newString(lib));
+ userLibs.add(lib);
}
}
}

MODIFY
runtime/src/jycessing/mode/PythonMode.java
runtime/src/jycessing/mode/PythonMode.java
@@ -32,6 +32,8 @@ public class PythonMode extends Mode {
public static final boolean SKETCH_RUNNER_FIRST =
Boolean.parseBoolean(System.getenv("SKETCH_RUNNER_FIRST"));
+ public static final String SITE_PACKAGES = "site-packages";
+
/**
* Python auto-formatting is handled by a server. {@link FormatServer} handles the lifecycle of,
* and communication with, that server.
@@ -56,6 +58,20 @@ public class PythonMode extends Mode {
* and not on any API. May break in the future.
*/
librariesFolder = Platform.getContentFile("modes/java/libraries");
+ final File pythonLibs = getSitePackages();
+ if (pythonLibs.exists()) {
+ if (!pythonLibs.isDirectory()) {
+ System.err.println(pythonLibs + " exists but is not directory");
+ }
+ } else {
+ if (!pythonLibs.mkdirs()) {
+ System.err.println("cannot create " + pythonLibs);
+ }
+ }
+ }
+
+ public static File getSitePackages() {
+ return new File(Base.getSketchbookLibrariesFolder(), SITE_PACKAGES);
}
@Override
@@ -76,7 +92,7 @@ public class PythonMode extends Mode {
try {
return new PyEditor(base, path, state, this);
- } catch (EditorException e) {
+ } catch (final EditorException e) {
Messages.showError("Editor Exception", "Issue Creating Editor", e);
return null;
}

MODIFY
runtime/src/jycessing/mode/run/PdeSketch.java
runtime/src/jycessing/mode/run/PdeSketch.java
@@ -4,11 +4,13 @@ import java.awt.Point;
import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.List;
import jycessing.DisplayType;
import jycessing.RunnableSketch;
import jycessing.Runner.LibraryPolicy;
+import jycessing.mode.PythonMode;
import processing.app.Base;
import processing.app.Platform;
import processing.app.Sketch;
@@ -23,6 +25,7 @@ import processing.core.PApplet;
public class PdeSketch implements RunnableSketch, Serializable {
private final List<File> libraryDirs;
+ private final List<File> pythonLibraryDirs;
private final File mainFile;
private final String mainCode;
private final File sketchHome;
@@ -60,6 +63,8 @@ public class PdeSketch implements RunnableSketch, Serializable {
codeFileNames[i] = sketch.getCode(i).getFile().getName();
}
this.codeFileNames = codeFileNames;
+ this.pythonLibraryDirs =
+ Arrays.asList(PythonMode.getSitePackages().getAbsoluteFile());
}
public static enum LocationType {
@@ -132,6 +137,7 @@ public class PdeSketch implements RunnableSketch, Serializable {
if (code.exists()) {
entries.add(code);
}
+ entries.addAll(pythonLibraryDirs);
return entries;
}
}

Create new site-packages feature for Python Mode. Fix #279.