Permalink
Browse files

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

  • Loading branch information...
jdf committed Sep 24, 2017
1 parent 0dc1252 commit c8362cad7c1f565ea598b6b9d5f38d9ed3f8d45d
@@ -29,6 +29,7 @@
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 static synchronized void runSketchBlocking(
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 static synchronized void runSketchBlocking(
// 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 static synchronized void runSketchBlocking(
searchForExtraStuff(dir, libs);
}
for (final String lib : libs) {
sys.path.insert(0, Py.newString(lib));
userLibs.add(lib);
}
}
}
@@ -32,6 +32,8 @@
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 PythonMode(final Base base, final File folder) {
* 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 Editor createEditor(final Base base, final String path, final EditorState
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;
}
@@ -4,11 +4,13 @@
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 @@
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 PdeSketch(
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 boolean shouldRun() {
if (code.exists()) {
entries.add(code);
}
entries.addAll(pythonLibraryDirs);
return entries;
}
}

0 comments on commit c8362ca

Please sign in to comment.