Search in sources :

Example 36 with IInterpreterManager

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

the class SyncSystemModulesManagerScheduler method start.

/**
 * To be called when we start the plugin.
 *
 * Should be called only once (when we'll make a full check for the current integrity of the information)
 * Later on, we'll start to check if things change in the PYTHONPATH based on changes in the filesystem.
 */
public void start() {
    boolean scheduleInitially = false;
    boolean reCheckOnFilesystemChanges = InterpreterGeneralPreferences.getReCheckOnFilesystemChanges();
    IInterpreterManager[] managers = InterpreterManagersAPI.getAllInterpreterManagers();
    for (IInterpreterManager iInterpreterManager : managers) {
        if (iInterpreterManager != null) {
            IInterpreterInfo[] interpreterInfos = iInterpreterManager.getInterpreterInfos();
            if (reCheckOnFilesystemChanges) {
                this.registerInterpreterManager(iInterpreterManager, interpreterInfos);
            }
            scheduleInitially = scheduleInitially || (interpreterInfos != null && interpreterInfos.length > 0);
        }
    }
    // Default is waiting 30 seconds after startup
    int timeout = 1000 * 30;
    IEclipsePreferences preferences = PydevPrefs.getEclipsePreferences();
    // Now we add builtin indexing on our checks (so, force it at least once).
    boolean alreadyChecked = preferences.getBoolean("INTERPRETERS_CHECKED_ONCE", false);
    boolean force = false;
    if (!alreadyChecked) {
        // Now we add builtin indexing on our checks (so, force it at least once).
        preferences.putBoolean("INTERPRETERS_CHECKED_ONCE", true);
        force = true;
        // In this case, wait only 7 seconds after startup
        timeout = 1000 * 7;
    }
    if (force || InterpreterGeneralPreferences.getCheckConsistentOnStartup()) {
        if (scheduleInitially) {
            // Only do the initial schedule if there's something to be tracked (otherwise, wait for some interpreter
            // to be configured and work only on deltas already).
            // The initial job will do a full check on what's available and if it's synched with the filesystem.
            job.addAllToTrack();
            // Wait a minute before starting our sync process.
            job.scheduleLater(timeout);
        }
    }
}
Also used : IInterpreterInfo(org.python.pydev.core.IInterpreterInfo) IEclipsePreferences(org.eclipse.core.runtime.preferences.IEclipsePreferences) IInterpreterManager(org.python.pydev.core.IInterpreterManager)

Example 37 with IInterpreterManager

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

the class SyncSystemModulesManagerScheduler method checkAllNow.

public void checkAllNow() {
    // Add all to be tracked
    Map<IInterpreterManager, Map<String, IInterpreterInfo>> addedToTrack = job.addAllToTrack();
    // remove from the preferences any ignore the user had set previously
    Set<Entry<IInterpreterManager, Map<String, IInterpreterInfo>>> entrySet = addedToTrack.entrySet();
    IEclipsePreferences preferences = PydevPrefs.getEclipsePreferences();
    for (Entry<IInterpreterManager, Map<String, IInterpreterInfo>> entry : entrySet) {
        Set<Entry<String, IInterpreterInfo>> entrySet2 = entry.getValue().entrySet();
        for (Entry<String, IInterpreterInfo> entry2 : entrySet2) {
            String key = SyncSystemModulesManager.createKeyForInfo(entry2.getValue());
            preferences.put(key, "");
        }
    }
    try {
        preferences.flush();
    } catch (BackingStoreException e) {
        Log.log(e);
    }
    // schedule changes to be executed.
    job.scheduleLater(0);
}
Also used : Entry(java.util.Map.Entry) IInterpreterInfo(org.python.pydev.core.IInterpreterInfo) IEclipsePreferences(org.eclipse.core.runtime.preferences.IEclipsePreferences) BackingStoreException(org.osgi.service.prefs.BackingStoreException) IInterpreterManager(org.python.pydev.core.IInterpreterManager) HashMap(java.util.HashMap) Map(java.util.Map)

Example 38 with IInterpreterManager

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

the class SystemModulesManager method getNature.

@Override
public IPythonNature getNature() {
    if (nature == null) {
        IInterpreterManager manager = getInterpreterManager();
        nature = new SystemPythonNature(manager, this.info);
    }
    return nature;
}
Also used : SystemPythonNature(org.python.pydev.plugin.nature.SystemPythonNature) IInterpreterManager(org.python.pydev.core.IInterpreterManager)

Example 39 with IInterpreterManager

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

the class PythonNature method getProjectInterpreter.

/**
 * @return info on the interpreter configured for this nature.
 * @throws MisconfigurationException
 *
 * @note that an exception will be raised if the
 */
@Override
public IInterpreterInfo getProjectInterpreter() throws MisconfigurationException, PythonNatureWithoutProjectException {
    if (this.project == null) {
        throw new PythonNatureWithoutProjectException("Project is not set.");
    }
    try {
        String projectInterpreterName = getProjectInterpreterName();
        IInterpreterInfo ret = null;
        IInterpreterManager relatedInterpreterManager = getRelatedInterpreterManager();
        if (relatedInterpreterManager == null) {
            if (IN_TESTS) {
                return null;
            }
            throw new ProjectMisconfiguredException("Did not expect the interpreter manager to be null.");
        }
        if (IPythonNature.DEFAULT_INTERPRETER.equals(projectInterpreterName)) {
            if (relatedInterpreterManager.getInterpreterType() == IPythonNature.INTERPRETER_TYPE_PYTHON) {
                IPath location = this.project.getLocation();
                if (location != null) {
                    File projectLocation = location.toFile();
                    ret = PipenvHelper.getPipenvInterpreterInfoForProjectLocation(relatedInterpreterManager.getInterpreterInfos(), projectLocation, relatedInterpreterManager);
                }
            }
            if (ret == null) {
                // if it's the default, let's translate it to the outside world
                ret = relatedInterpreterManager.getDefaultInterpreterInfo(true);
            }
        } else {
            ret = relatedInterpreterManager.getInterpreterInfo(projectInterpreterName, null);
        }
        if (ret == null) {
            final IProject p = this.getProject();
            final String projectName;
            if (p != null) {
                projectName = p.getName();
            } else {
                projectName = "null";
            }
            String msg = "Invalid interpreter: " + projectInterpreterName + " configured for project: " + projectName + ".";
            ProjectMisconfiguredException e = new ProjectMisconfiguredException(msg);
            Log.log(e);
            throw e;
        } else {
            return ret;
        }
    } catch (CoreException e) {
        throw new ProjectMisconfiguredException(e);
    }
}
Also used : ProjectMisconfiguredException(org.python.pydev.core.ProjectMisconfiguredException) IPath(org.eclipse.core.runtime.IPath) CoreException(org.eclipse.core.runtime.CoreException) IInterpreterInfo(org.python.pydev.core.IInterpreterInfo) PythonNatureWithoutProjectException(org.python.pydev.core.PythonNatureWithoutProjectException) IInterpreterManager(org.python.pydev.core.IInterpreterManager) File(java.io.File) IProject(org.eclipse.core.resources.IProject)

Example 40 with IInterpreterManager

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

the class BlackRunner method formatWithBlack.

public static String formatWithBlack(IPythonNature nature, IDocument doc, FormatStd std, File workingDir) {
    try {
        Process process;
        String[] parseArguments = ProcessUtils.parseArguments(std.blackParameters);
        String cmdarrayAsStr;
        String executableLocation = std.blackExecutableLocation;
        if (!std.searchBlackInInterpreter && executableLocation != null && !executableLocation.isEmpty() && FileUtils.enhancedIsFile(new File(executableLocation))) {
            SimpleRunner simpleRunner = new SimpleRunner();
            String[] args = ArrayUtils.concatArrays(new String[] { executableLocation, "-" }, parseArguments);
            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);
            Tuple<Process, String> processInfo = pythonRunner.createProcessFromModuleName("black", ArrayUtils.concatArrays(new String[] { "-" }, parseArguments), workingDir, new NullProgressMonitor());
            process = processInfo.o1;
            cmdarrayAsStr = processInfo.o2;
        }
        String pythonFileEncoding = FileUtils.getPythonFileEncoding(doc, null);
        if (pythonFileEncoding == null) {
            pythonFileEncoding = "utf-8";
        }
        process.getOutputStream().write(doc.get().getBytes(pythonFileEncoding));
        Tuple<String, String> processOutput = ProcessUtils.getProcessOutput(process, cmdarrayAsStr, new NullProgressMonitor(), pythonFileEncoding);
        if (process.exitValue() != 0) {
            Log.log("Black formatter exited with: " + process.exitValue() + "\nStdout:\n" + processOutput.o1 + "\nStderr:\n" + processOutput.o2);
            return null;
        }
        return processOutput.o1;
    } catch (Exception e) {
        Log.log(e);
    }
    return null;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) SimpleRunner(org.python.pydev.ast.runners.SimpleRunner) MisconfigurationException(org.python.pydev.core.MisconfigurationException) SystemPythonNature(org.python.pydev.plugin.nature.SystemPythonNature) IInterpreterManager(org.python.pydev.core.IInterpreterManager) MisconfigurationException(org.python.pydev.core.MisconfigurationException) File(java.io.File) PythonRunner(org.python.pydev.ast.runners.UniversalRunner.PythonRunner)

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