use of org.python.pydev.core.MisconfigurationException in project Pydev by fabioz.
the class PythonNatureStore method getRootNodeInXml.
/**
* Get the root node of the project description
*
* @return the root Node object
* @throws MisconfigurationException
* @throws CoreException if root node is not present
*/
private synchronized Node getRootNodeInXml() throws MisconfigurationException {
traceFunc("getRootNodeInXml");
if (document == null) {
throw new MisconfigurationException("Found null XML document. Please check if " + this.xmlFile + " is a valid XML file.");
}
NodeList nodeList = document.getElementsByTagName("pydev_project");
Node ret = null;
if (nodeList != null && nodeList.getLength() > 0) {
ret = nodeList.item(0);
}
traceFunc("END getRootNodeInXml -- ", ret);
if (ret != null) {
return ret;
}
throw new RuntimeException(StringUtils.format("Error. Unable to get the %s tag by its name. Project: %s", "pydev_project", project));
}
use of org.python.pydev.core.MisconfigurationException in project Pydev by fabioz.
the class RefactoringRequest method getModule.
/**
* @return the module for the document (may return the ast from the pyedit if it is available).
*/
public IModule getModule() {
if (module == null) {
if (pyEdit != null) {
SimpleNode ast = (SimpleNode) pyEdit.getAST();
if (ast != null) {
IDocument doc = ps.getDoc();
long astModificationTimeStamp = pyEdit.getAstModificationTimeStamp();
if (astModificationTimeStamp != -1 && astModificationTimeStamp == (((IDocumentExtension4) doc).getModificationStamp())) {
// Matched time stamp -- so, we can use the ast without fear of being unsynched.
module = AbstractModule.createModule(ast, file, resolveModule(), nature);
} else {
// Did not match time stamp!! We'll reparse the document later on to get a synched version.
}
}
}
if (module == null) {
try {
module = AbstractModule.createModuleFromDoc(resolveModule(), file, ps.getDoc(), nature, false);
} catch (MisconfigurationException e) {
throw new RuntimeException(e);
}
}
}
return module;
}
use of org.python.pydev.core.MisconfigurationException in project Pydev by fabioz.
the class InterpreterManagersAPI method getInfoForFile.
/**
* @param file the file we want to get info on.
* @return a tuple with the nature to be used and the name of the module represented by the file in that scenario.
*/
public static Tuple<IPythonNature, String> getInfoForFile(File file) {
IInterpreterManager pythonInterpreterManager2 = getPythonInterpreterManager(false);
IInterpreterManager jythonInterpreterManager2 = getJythonInterpreterManager(false);
IInterpreterManager ironpythonInterpreterManager2 = getIronpythonInterpreterManager(false);
if (file != null) {
// Check if we can resolve the manager for the passed file...
Tuple<IPythonNature, String> infoForManager = getInfoForManager(file, pythonInterpreterManager2);
if (infoForManager != null) {
return infoForManager;
}
infoForManager = getInfoForManager(file, jythonInterpreterManager2);
if (infoForManager != null) {
return infoForManager;
}
infoForManager = getInfoForManager(file, ironpythonInterpreterManager2);
if (infoForManager != null) {
return infoForManager;
}
// Ok, the file is not part of the interpreter configuration, but it's still possible that it's part of a
// project... (external projects), so, let's go on and see if there's some match there.
List<IPythonNature> allPythonNatures = PythonNature.getAllPythonNatures();
int size = allPythonNatures.size();
for (int i = 0; i < size; i++) {
IPythonNature nature = allPythonNatures.get(i);
try {
// Note: only resolve in the project sources, as we've already checked the system and we'll be
// checking all projects anyways.
String modName = nature.resolveModuleOnlyInProjectSources(FileUtils.getFileAbsolutePath(file), true);
if (modName != null) {
return new Tuple<IPythonNature, String>(nature, modName);
}
} catch (Exception e) {
Log.log(e);
}
}
}
if (pythonInterpreterManager2.isConfigured()) {
try {
return new Tuple<IPythonNature, String>(new SystemPythonNature(pythonInterpreterManager2), getModNameFromFile(file));
} catch (MisconfigurationException e) {
}
}
if (jythonInterpreterManager2.isConfigured()) {
try {
return new Tuple<IPythonNature, String>(new SystemPythonNature(jythonInterpreterManager2), getModNameFromFile(file));
} catch (MisconfigurationException e) {
}
}
if (ironpythonInterpreterManager2.isConfigured()) {
try {
return new Tuple<IPythonNature, String>(new SystemPythonNature(ironpythonInterpreterManager2), getModNameFromFile(file));
} catch (MisconfigurationException e) {
}
}
// Ok, nothing worked, let's just do a call which'll ask to configure python and return null!
try {
pythonInterpreterManager2.getDefaultInterpreterInfo(true);
} catch (MisconfigurationException e) {
// Ignore
}
return null;
}
use of org.python.pydev.core.MisconfigurationException in project Pydev by fabioz.
the class Pep8Runner method runWithPep8BaseScript.
/**
* @param fileContents the contents to be passed in the stdin.
* @param parameters the parameters to pass. Note that a '-' is always added to the parameters to signal we'll pass the file as the input in stdin.
* @param script i.e.: pycodestyle.py, autopep8.py
* @return null if there was some error, otherwise returns the process stdout output.
*/
public static String runWithPep8BaseScript(IDocument doc, String parameters, String script) {
File autopep8File;
try {
autopep8File = CorePlugin.getScriptWithinPySrc(new Path("third_party").append("pep8").append(script).toString());
} catch (CoreException e) {
Log.log("Unable to get " + script + " location.");
return null;
}
if (!autopep8File.exists()) {
Log.log("Specified location for " + script + " does not exist (" + autopep8File + ").");
return null;
}
SimplePythonRunner simplePythonRunner = new SimplePythonRunner();
IInterpreterManager pythonInterpreterManager = InterpreterManagersAPI.getPythonInterpreterManager();
IInterpreterInfo defaultInterpreterInfo;
try {
defaultInterpreterInfo = pythonInterpreterManager.getDefaultInterpreterInfo(false);
} catch (MisconfigurationException e) {
Log.log("No default Python interpreter configured to run " + script);
return null;
}
String[] parseArguments = ProcessUtils.parseArguments(parameters);
List<String> lst = new ArrayList<>(Arrays.asList(parseArguments));
lst.add("-");
String[] cmdarray = SimplePythonRunner.preparePythonCallParameters(defaultInterpreterInfo.getExecutableOrJar(), autopep8File.toString(), lst.toArray(new String[0]));
// Try to find the file's encoding, but if none is given or the specified encoding is
// unsupported, then just default to utf-8
String pythonFileEncoding = null;
try {
pythonFileEncoding = FileUtils.getPythonFileEncoding(doc, null);
if (pythonFileEncoding == null) {
pythonFileEncoding = "utf-8";
}
} catch (UnsupportedEncodingException e) {
pythonFileEncoding = "utf-8";
}
final String encodingUsed = pythonFileEncoding;
SystemPythonNature nature = new SystemPythonNature(pythonInterpreterManager, defaultInterpreterInfo);
ICallback<String[], String[]> updateEnv = new ICallback<String[], String[]>() {
@Override
public String[] call(String[] arg) {
if (arg == null) {
arg = new String[] { "PYTHONIOENCODING=" + encodingUsed };
} else {
arg = ProcessUtils.addOrReplaceEnvVar(arg, "PYTHONIOENCODING", encodingUsed);
}
return arg;
}
};
Tuple<Process, String> r = simplePythonRunner.run(cmdarray, autopep8File.getParentFile(), nature, new NullProgressMonitor(), updateEnv);
try {
r.o1.getOutputStream().write(doc.get().getBytes(pythonFileEncoding));
r.o1.getOutputStream().close();
} catch (IOException e) {
Log.log("Error writing contents to " + script);
return null;
}
Tuple<String, String> processOutput = SimplePythonRunner.getProcessOutput(r.o1, r.o2, new NullProgressMonitor(), pythonFileEncoding);
if (processOutput.o2.length() > 0) {
Log.log(processOutput.o2);
}
if (processOutput.o1.length() > 0) {
return processOutput.o1;
}
return null;
}
use of org.python.pydev.core.MisconfigurationException in project Pydev by fabioz.
the class PyReloadCode method onSave.
@Override
public void onSave(BaseEditor baseEditor, IProgressMonitor monitor) {
if (!DebugPrefsPage.getReloadModuleOnChange()) {
return;
}
PyEdit edit = (PyEdit) baseEditor;
File file = edit.getEditorFile();
if (file != null) {
IDebugTarget[] debugTargets = DebugPlugin.getDefault().getLaunchManager().getDebugTargets();
if (debugTargets.length > 0) {
ICallback<Boolean, IDebugTarget> callbackThatFilters = new ICallback<Boolean, IDebugTarget>() {
@Override
public Boolean call(IDebugTarget arg) {
return arg instanceof AbstractDebugTarget;
}
};
List<IDebugTarget> filter = ArrayUtils.filter(debugTargets, callbackThatFilters);
if (filter.size() > 0) {
try {
IPythonNature pythonNature = edit.getPythonNature();
if (pythonNature != null) {
String moduleName = pythonNature.resolveModule(file);
if (moduleName != null) {
for (IDebugTarget iDebugTarget : filter) {
AbstractDebugTarget target = (AbstractDebugTarget) iDebugTarget;
target.postCommand(new ReloadCodeCommand(target, moduleName));
}
}
}
} catch (MisconfigurationException e) {
Log.log(e);
}
}
}
}
}
Aggregations