-
-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved isort integration to use isort from the environment / extern…
…al executable instead of using the Jython integration.
- Loading branch information
Showing
21 changed files
with
222 additions
and
3,036 deletions.
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
plugins/org.python.pydev.ast/src/org/python/pydev/core/pep8/ISortRunner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package org.python.pydev.core.pep8; | ||
|
||
import java.io.File; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
import org.eclipse.core.runtime.NullProgressMonitor; | ||
import org.python.pydev.ast.interpreter_managers.InterpreterManagersAPI; | ||
import org.python.pydev.ast.runners.SimpleRunner; | ||
import org.python.pydev.ast.runners.UniversalRunner.PythonRunner; | ||
import org.python.pydev.core.IInterpreterManager; | ||
import org.python.pydev.core.IPythonNature; | ||
import org.python.pydev.core.MisconfigurationException; | ||
import org.python.pydev.core.log.Log; | ||
import org.python.pydev.plugin.nature.SystemPythonNature; | ||
import org.python.pydev.shared_core.io.FileUtils; | ||
import org.python.pydev.shared_core.process.ProcessUtils; | ||
import org.python.pydev.shared_core.structure.Tuple; | ||
import org.python.pydev.shared_core.utils.ArrayUtils; | ||
|
||
public class ISortRunner { | ||
|
||
public static String formatWithISort(File targetFile, IPythonNature nature, String fileContents, String encoding, | ||
File workingDir, String[] isortArguments, Set<String> knownThirdParty, | ||
Optional<String> executableLocation) { | ||
try { | ||
Process process; | ||
String cmdarrayAsStr; | ||
|
||
if (executableLocation.isPresent() && !executableLocation.get().isEmpty() | ||
&& FileUtils.enhancedIsFile(new File(executableLocation.get()))) { | ||
SimpleRunner simpleRunner = new SimpleRunner(); | ||
String[] args = ArrayUtils.concatArrays(new String[] { executableLocation.get(), "-" }, isortArguments); | ||
Tuple<Process, String> r = simpleRunner.run(args, workingDir, null, null); | ||
process = r.o1; | ||
cmdarrayAsStr = r.o2; | ||
} else { | ||
if (nature == null) { | ||
IInterpreterManager manager = InterpreterManagersAPI.getPythonInterpreterManager(); | ||
try { | ||
nature = new SystemPythonNature(manager); | ||
} catch (MisconfigurationException e) { | ||
Log.log(e); | ||
} | ||
} | ||
PythonRunner pythonRunner = new PythonRunner(nature); | ||
String filepath = targetFile != null ? FileUtils.getFileAbsolutePath(targetFile) | ||
: null; | ||
|
||
String[] pathArgs = filepath != null && filepath.length() > 0 | ||
? new String[] { "--filename", filepath } | ||
: new String[0]; | ||
|
||
List<String> known = new ArrayList<>(); | ||
for (String s : knownThirdParty) { | ||
known.add("-o"); | ||
known.add(s); | ||
} | ||
final String[] args = ArrayUtils.concatArrays(pathArgs, isortArguments, known.toArray(new String[0]), | ||
new String[] { "-d", "-" }); | ||
Tuple<Process, String> processInfo = pythonRunner.createProcessFromModuleName("isort", | ||
args, | ||
workingDir, new NullProgressMonitor()); | ||
process = processInfo.o1; | ||
cmdarrayAsStr = processInfo.o2; | ||
} | ||
|
||
boolean failedWrite = false; | ||
try { | ||
process.getOutputStream().write(fileContents.getBytes(encoding)); | ||
} catch (Exception e) { | ||
failedWrite = true; | ||
} | ||
Tuple<String, String> processOutput = ProcessUtils.getProcessOutput(process, cmdarrayAsStr, | ||
new NullProgressMonitor(), encoding); | ||
|
||
if (process.exitValue() != 0 || failedWrite) { | ||
Log.log("isort exited with: " + process.exitValue() + " failedWrite: " + failedWrite | ||
+ "\nStdout:\n" + processOutput.o1 | ||
+ "\nStderr:\n" + processOutput.o2); | ||
return null; | ||
} | ||
return processOutput.o1; | ||
} catch (Exception e) { | ||
Log.log(e); | ||
} | ||
return null; | ||
} | ||
|
||
} |
1 change: 0 additions & 1 deletion
1
plugins/org.python.pydev.core/pysrc/third_party/isort_container/backports/__init__.py
This file was deleted.
Oops, something went wrong.
196 changes: 0 additions & 196 deletions
196
.../org.python.pydev.core/pysrc/third_party/isort_container/backports/functools_lru_cache.py
This file was deleted.
Oops, something went wrong.
28 changes: 0 additions & 28 deletions
28
plugins/org.python.pydev.core/pysrc/third_party/isort_container/isort/__init__.py
This file was deleted.
Oops, something went wrong.
9 changes: 0 additions & 9 deletions
9
plugins/org.python.pydev.core/pysrc/third_party/isort_container/isort/__main__.py
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.