Search in sources :

Example 11 with MisconfigurationException

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));
}
Also used : MisconfigurationException(org.python.pydev.core.MisconfigurationException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node)

Example 12 with MisconfigurationException

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;
}
Also used : MisconfigurationException(org.python.pydev.core.MisconfigurationException) IDocument(org.eclipse.jface.text.IDocument) SimpleNode(org.python.pydev.parser.jython.SimpleNode)

Example 13 with MisconfigurationException

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;
}
Also used : MisconfigurationException(org.python.pydev.core.MisconfigurationException) IPythonNature(org.python.pydev.core.IPythonNature) SystemPythonNature(org.python.pydev.plugin.nature.SystemPythonNature) IInterpreterManager(org.python.pydev.core.IInterpreterManager) Tuple(org.python.pydev.shared_core.structure.Tuple) CoreException(org.eclipse.core.runtime.CoreException) MisconfigurationException(org.python.pydev.core.MisconfigurationException)

Example 14 with MisconfigurationException

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;
}
Also used : Path(org.eclipse.core.runtime.Path) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) MisconfigurationException(org.python.pydev.core.MisconfigurationException) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SystemPythonNature(org.python.pydev.plugin.nature.SystemPythonNature) IOException(java.io.IOException) IInterpreterManager(org.python.pydev.core.IInterpreterManager) CoreException(org.eclipse.core.runtime.CoreException) ICallback(org.python.pydev.shared_core.callbacks.ICallback) IInterpreterInfo(org.python.pydev.core.IInterpreterInfo) SimplePythonRunner(org.python.pydev.ast.runners.SimplePythonRunner) File(java.io.File)

Example 15 with MisconfigurationException

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);
                }
            }
        }
    }
}
Also used : MisconfigurationException(org.python.pydev.core.MisconfigurationException) IPythonNature(org.python.pydev.core.IPythonNature) ICallback(org.python.pydev.shared_core.callbacks.ICallback) IDebugTarget(org.eclipse.debug.core.model.IDebugTarget) ReloadCodeCommand(org.python.pydev.debug.model.remote.ReloadCodeCommand) File(java.io.File) PyEdit(org.python.pydev.editor.PyEdit)

Aggregations

MisconfigurationException (org.python.pydev.core.MisconfigurationException)99 IPythonNature (org.python.pydev.core.IPythonNature)28 ArrayList (java.util.ArrayList)23 File (java.io.File)21 CoreException (org.eclipse.core.runtime.CoreException)18 IOException (java.io.IOException)14 SourceModule (org.python.pydev.ast.codecompletion.revisited.modules.SourceModule)14 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)13 IDocument (org.eclipse.jface.text.IDocument)13 IInterpreterInfo (org.python.pydev.core.IInterpreterInfo)13 IInterpreterManager (org.python.pydev.core.IInterpreterManager)12 PythonNatureWithoutProjectException (org.python.pydev.core.PythonNatureWithoutProjectException)12 HashSet (java.util.HashSet)11 IModule (org.python.pydev.core.IModule)11 BadLocationException (org.eclipse.jface.text.BadLocationException)10 SimpleNode (org.python.pydev.parser.jython.SimpleNode)10 AbstractAdditionalDependencyInfo (com.python.pydev.analysis.additionalinfo.AbstractAdditionalDependencyInfo)9 IFile (org.eclipse.core.resources.IFile)9 IGrammarVersionProvider (org.python.pydev.core.IGrammarVersionProvider)9 TokensList (org.python.pydev.core.TokensList)9