Skip to content

Commit

Permalink
Create new site-packages feature for Python Mode. Fix #279.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Feinberg committed Sep 24, 2017
1 parent 0dc1252 commit c8362ca
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
13 changes: 6 additions & 7 deletions runtime/src/jycessing/Runner.java
Expand Up @@ -29,6 +29,7 @@
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
import java.util.Set; import java.util.Set;
import java.util.TreeSet;


import org.python.core.Py; import org.python.core.Py;
import org.python.core.PyList; import org.python.core.PyList;
Expand Down Expand Up @@ -295,15 +296,12 @@ public static synchronized void runSketchBlocking(


final StringBuilder pythonPath = new StringBuilder(); 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. // The code may be located in a temp dir, if the sketch is unsaved.
final String actualCodeLocation = sketch.getMainFile().getParentFile().getAbsolutePath(); final String actualCodeLocation = sketch.getMainFile().getParentFile().getAbsolutePath();
pythonPath.append(File.pathSeparator).append(actualCodeLocation); pythonPath.append(File.pathSeparator).append(actualCodeLocation);


final String sketchDirPath = sketch.getHomeDirectory().getAbsolutePath(); final String sketchDirPath = sketch.getHomeDirectory().getAbsolutePath();
pythonPath.append(File.pathSeparator).append(sketchDirPath); pythonPath.append(File.pathSeparator).append(sketchDirPath);

props.setProperty("python.path", pythonPath.toString()); props.setProperty("python.path", pythonPath.toString());
props.setProperty("python.main", sketch.getMainFile().getAbsolutePath()); props.setProperty("python.main", sketch.getMainFile().getAbsolutePath());
props.setProperty("python.main.root", sketchDirPath); props.setProperty("python.main.root", sketchDirPath);
Expand All @@ -326,15 +324,16 @@ public static synchronized void runSketchBlocking(


// Add all of the sketch's requested sys.path entries, and add all jar // Add all of the sketch's requested sys.path entries, and add all jar
// files found there, recursively. // files found there, recursively.
final Set<String> userLibs = new HashSet<>(); final Set<String> userLibs = new TreeSet<>();
for (final File entry : sketch.getPathEntries()) { for (final File entry : sketch.getPathEntries()) {
sys.path.append(Py.newString(entry.getAbsolutePath())); userLibs.add(entry.getAbsolutePath());
searchForExtraStuff(entry, userLibs); searchForExtraStuff(entry, userLibs);
} }


// Add the add_library function to the sketch namespace. // Add the add_library function to the sketch namespace.
final List<File> libDirs = sketch.getLibraryDirectories();
if (libDirs != null) { if (libDirs != null) {
new LibraryImporter(sketch.getLibraryDirectories(), interp); new LibraryImporter(libDirs, interp);


if (sketch.getLibraryPolicy() == LibraryPolicy.PROMISCUOUS) { if (sketch.getLibraryPolicy() == LibraryPolicy.PROMISCUOUS) {
log("Promiscusouly adding all libraries in " + libDirs); log("Promiscusouly adding all libraries in " + libDirs);
Expand All @@ -345,7 +344,7 @@ public static synchronized void runSketchBlocking(
searchForExtraStuff(dir, libs); searchForExtraStuff(dir, libs);
} }
for (final String lib : libs) { for (final String lib : libs) {
sys.path.insert(0, Py.newString(lib)); userLibs.add(lib);
} }
} }
} }
Expand Down
18 changes: 17 additions & 1 deletion runtime/src/jycessing/mode/PythonMode.java
Expand Up @@ -32,6 +32,8 @@ public class PythonMode extends Mode {
public static final boolean SKETCH_RUNNER_FIRST = public static final boolean SKETCH_RUNNER_FIRST =
Boolean.parseBoolean(System.getenv("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, * Python auto-formatting is handled by a server. {@link FormatServer} handles the lifecycle of,
* and communication with, that server. * and communication with, that server.
Expand All @@ -56,6 +58,20 @@ public PythonMode(final Base base, final File folder) {
* and not on any API. May break in the future. * and not on any API. May break in the future.
*/ */
librariesFolder = Platform.getContentFile("modes/java/libraries"); 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 @Override
Expand All @@ -76,7 +92,7 @@ public Editor createEditor(final Base base, final String path, final EditorState


try { try {
return new PyEditor(base, path, state, this); return new PyEditor(base, path, state, this);
} catch (EditorException e) { } catch (final EditorException e) {
Messages.showError("Editor Exception", "Issue Creating Editor", e); Messages.showError("Editor Exception", "Issue Creating Editor", e);
return null; return null;
} }
Expand Down
6 changes: 6 additions & 0 deletions runtime/src/jycessing/mode/run/PdeSketch.java
Expand Up @@ -4,11 +4,13 @@
import java.io.File; import java.io.File;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;


import jycessing.DisplayType; import jycessing.DisplayType;
import jycessing.RunnableSketch; import jycessing.RunnableSketch;
import jycessing.Runner.LibraryPolicy; import jycessing.Runner.LibraryPolicy;
import jycessing.mode.PythonMode;
import processing.app.Base; import processing.app.Base;
import processing.app.Platform; import processing.app.Platform;
import processing.app.Sketch; import processing.app.Sketch;
Expand All @@ -23,6 +25,7 @@
public class PdeSketch implements RunnableSketch, Serializable { public class PdeSketch implements RunnableSketch, Serializable {


private final List<File> libraryDirs; private final List<File> libraryDirs;
private final List<File> pythonLibraryDirs;
private final File mainFile; private final File mainFile;
private final String mainCode; private final String mainCode;
private final File sketchHome; private final File sketchHome;
Expand Down Expand Up @@ -60,6 +63,8 @@ public PdeSketch(
codeFileNames[i] = sketch.getCode(i).getFile().getName(); codeFileNames[i] = sketch.getCode(i).getFile().getName();
} }
this.codeFileNames = codeFileNames; this.codeFileNames = codeFileNames;
this.pythonLibraryDirs =
Arrays.asList(PythonMode.getSitePackages().getAbsoluteFile());
} }


public static enum LocationType { public static enum LocationType {
Expand Down Expand Up @@ -132,6 +137,7 @@ public List<File> getPathEntries() {
if (code.exists()) { if (code.exists()) {
entries.add(code); entries.add(code);
} }
entries.addAll(pythonLibraryDirs);
return entries; return entries;
} }
} }

0 comments on commit c8362ca

Please sign in to comment.