Search in sources :

Example 1 with IInterpreterManager

use of org.python.pydev.core.IInterpreterManager in project ibex_gui by ISISComputingGroup.

the class GeniePythonConsoleFactory method createGeniePydevInterpreter.

/**
 * Creates a Genie Python Interpreter.
 *
 * @return Interpreter
 * @throws Exception
 *             can throw several different exceptions
 */
PydevConsoleInterpreter createGeniePydevInterpreter() throws Exception {
    IInterpreterManager manager = InterpreterManagersAPI.getPythonInterpreterManager();
    IInterpreterInfo interpreterInfo = manager.createInterpreterInfo(PreferenceSupplier.getPythonPath(), monitor, false);
    PydevIProcessFactory iprocessFactory = new PydevIProcessFactory();
    PydevConsoleLaunchInfo launchAndProcess = iprocessFactory.createLaunch(manager, interpreterInfo, interpreterInfo.getPythonPath(), null, null);
    return createPydevInterpreter(launchAndProcess, null, null);
}
Also used : PydevConsoleLaunchInfo(org.python.pydev.debug.newconsole.env.PydevIProcessFactory.PydevConsoleLaunchInfo) PydevIProcessFactory(org.python.pydev.debug.newconsole.env.PydevIProcessFactory) IInterpreterInfo(org.python.pydev.core.IInterpreterInfo) IInterpreterManager(org.python.pydev.core.IInterpreterManager)

Example 2 with IInterpreterManager

use of org.python.pydev.core.IInterpreterManager 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 3 with IInterpreterManager

use of org.python.pydev.core.IInterpreterManager in project Pydev by fabioz.

the class InterpreterManagersAPI method getAllInterpreterInfos.

public static List<IInterpreterInfo> getAllInterpreterInfos() {
    List<IInterpreterInfo> infos = new ArrayList<>();
    IInterpreterManager[] allInterpreterManagers = getAllInterpreterManagers();
    for (IInterpreterManager iInterpreterManager : allInterpreterManagers) {
        if (iInterpreterManager != null) {
            infos.addAll(Arrays.asList(iInterpreterManager.getInterpreterInfos()));
        }
    }
    return infos;
}
Also used : IInterpreterInfo(org.python.pydev.core.IInterpreterInfo) ArrayList(java.util.ArrayList) IInterpreterManager(org.python.pydev.core.IInterpreterManager)

Example 4 with IInterpreterManager

use of org.python.pydev.core.IInterpreterManager 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 5 with IInterpreterManager

use of org.python.pydev.core.IInterpreterManager in project Pydev by fabioz.

the class DjangoNewProjectPage method getNextPage.

@Override
public IWizardPage getNextPage() {
    String projectType = this.getProjectType();
    IInterpreterManager interpreterManager;
    if (IPythonNature.Versions.ALL_JYTHON_VERSIONS.contains(projectType)) {
        interpreterManager = InterpreterManagersAPI.getJythonInterpreterManager();
    } else if (IPythonNature.Versions.ALL_IRONPYTHON_VERSIONS.contains(projectType)) {
        interpreterManager = InterpreterManagersAPI.getIronpythonInterpreterManager();
    } else {
        // if others fail, consider it python
        interpreterManager = InterpreterManagersAPI.getPythonInterpreterManager();
    }
    try {
        String projectInterpreter = this.getProjectInterpreter();
        IInterpreterInfo interpreterInfo;
        if (projectInterpreter.toLowerCase().equals("default")) {
            interpreterInfo = interpreterManager.getDefaultInterpreterInfo(false);
        } else {
            interpreterInfo = interpreterManager.getInterpreterInfo(projectInterpreter, new NullProgressMonitor());
        }
        IModule module = interpreterInfo.getModulesManager().getModuleWithoutBuiltins("django.core.__init__", null, false, new BaseModuleRequest(false));
        if (module == null) {
            DjangoNotAvailableWizardPage page = new DjangoNotAvailableWizardPage("Django not available", interpreterInfo);
            page.setWizard(this.getWizard());
            return page;
        }
    } catch (MisconfigurationException e) {
        ErrorWizardPage page = new ErrorWizardPage("Unexpected error.", "An unexpected error happened:\n" + e.getMessage());
        page.setWizard(this.getWizard());
        return page;
    }
    return super.getNextPage();
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IModule(org.python.pydev.core.IModule) IInterpreterInfo(org.python.pydev.core.IInterpreterInfo) BaseModuleRequest(org.python.pydev.core.BaseModuleRequest) MisconfigurationException(org.python.pydev.core.MisconfigurationException) IInterpreterManager(org.python.pydev.core.IInterpreterManager)

Aggregations

IInterpreterManager (org.python.pydev.core.IInterpreterManager)61 IInterpreterInfo (org.python.pydev.core.IInterpreterInfo)30 MisconfigurationException (org.python.pydev.core.MisconfigurationException)14 InterpreterInfo (org.python.pydev.ast.interpreter_managers.InterpreterInfo)13 CoreException (org.eclipse.core.runtime.CoreException)12 File (java.io.File)11 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)11 IPythonNature (org.python.pydev.core.IPythonNature)11 ArrayList (java.util.ArrayList)10 HashMap (java.util.HashMap)8 IProject (org.eclipse.core.resources.IProject)7 SystemPythonNature (org.python.pydev.plugin.nature.SystemPythonNature)7 Map (java.util.Map)6 Tuple (org.python.pydev.shared_core.structure.Tuple)6 AdditionalSystemInterpreterInfo (com.python.pydev.analysis.additionalinfo.AdditionalSystemInterpreterInfo)5 Collection (java.util.Collection)5 HashSet (java.util.HashSet)5 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)5 PyEdit (org.python.pydev.editor.PyEdit)4 DataAndImageTreeNode (org.python.pydev.shared_core.structure.DataAndImageTreeNode)4