Search in sources :

Example 21 with IInterpreterManager

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

the class RunEditorBasedOnNatureTypeAction method run.

@Override
public void run(IAction action) {
    PyEdit pyEdit = getPyEdit();
    final Tuple<String, IInterpreterManager> launchConfigurationTypeAndInterpreterManager = this.getLaunchConfigurationTypeAndInterpreterManager(pyEdit, false);
    AbstractLaunchShortcut shortcut = new AbstractLaunchShortcut() {

        @Override
        protected String getLaunchConfigurationType() {
            return launchConfigurationTypeAndInterpreterManager.o1;
        }

        @Override
        protected IInterpreterManager getInterpreterManager(IProject project) {
            return launchConfigurationTypeAndInterpreterManager.o2;
        }
    };
    shortcut.launch(pyEdit, "run");
}
Also used : AbstractLaunchShortcut(org.python.pydev.debug.ui.launching.AbstractLaunchShortcut) IInterpreterManager(org.python.pydev.core.IInterpreterManager) PyEdit(org.python.pydev.editor.PyEdit) IProject(org.eclipse.core.resources.IProject)

Example 22 with IInterpreterManager

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

the class PythonPathBlock method initializeFrom.

/*
     * (non-Javadoc)
     * @see org.eclipse.debug.ui.ILaunchConfigurationTab#initializeFrom(org.eclipse.debug.core.ILaunchConfiguration)
     */
@Override
public void initializeFrom(ILaunchConfiguration configuration) {
    try {
        String id = configuration.getType().getIdentifier();
        IInterpreterManager manager = null;
        if (Constants.ID_JYTHON_LAUNCH_CONFIGURATION_TYPE.equals(id) || Constants.ID_JYTHON_UNITTEST_LAUNCH_CONFIGURATION_TYPE.equals(id)) {
            manager = InterpreterManagersAPI.getJythonInterpreterManager();
        } else if (Constants.ID_IRONPYTHON_LAUNCH_CONFIGURATION_TYPE.equals(id) || Constants.ID_IRONPYTHON_UNITTEST_LAUNCH_CONFIGURATION_TYPE.equals(id)) {
            manager = InterpreterManagersAPI.getIronpythonInterpreterManager();
        } else if (Constants.ID_PYTHON_REGULAR_LAUNCH_CONFIGURATION_TYPE.equals(id) || Constants.ID_PYTHON_COVERAGE_LAUNCH_CONFIGURATION_TYPE.equals(id) || Constants.ID_PYTHON_UNITTEST_LAUNCH_CONFIGURATION_TYPE.equals(id)) {
            manager = InterpreterManagersAPI.getPythonInterpreterManager();
        } else {
            // Get from the project
            try {
                // could throw core exception if project does not exist.
                IProject project = PythonRunnerConfig.getProjectFromConfiguration(configuration);
                PythonNature nature = PythonNature.getPythonNature(project);
                if (nature != null) {
                    manager = InterpreterManagersAPI.getInterpreterManager(nature);
                }
            } catch (Exception e) {
                Log.log(e);
            }
            if (manager == null) {
                Log.log("Could not recognize: '" + id + "' using default python interpreter manager.");
                manager = InterpreterManagersAPI.getPythonInterpreterManager();
            }
        }
        String pythonPath = PythonRunnerConfig.getPythonpathFromConfiguration(configuration, manager);
        fPythonPathList.removeAll();
        java.util.List<String> paths = SimpleRunner.splitPythonpath(pythonPath);
        for (String p : paths) {
            fPythonPathList.add(p);
        }
        setErrorMessage(null);
    } catch (Exception e) {
        // Exceptions here may have several reasons
        // - The interpreter is incorrectly configured
        // - The arguments use an unresolved variable.
        // In each case, the exception contains a meaningful message, that is displayed
        Log.log(e);
        String message = e.getMessage();
        if (message == null) {
            message = "null (see error log for the traceback).";
        }
        String errorMsg = StringUtils.replaceNewLines(message, " ");
        fPythonPathList.removeAll();
        fPythonPathList.add(errorMsg);
        setErrorMessage(errorMsg);
    }
}
Also used : PythonNature(org.python.pydev.plugin.nature.PythonNature) IInterpreterManager(org.python.pydev.core.IInterpreterManager) IProject(org.eclipse.core.resources.IProject)

Example 23 with IInterpreterManager

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

the class AbstractRunEditorAction method getLaunchConfigurationTypeAndInterpreterManager.

protected Tuple<String, IInterpreterManager> getLaunchConfigurationTypeAndInterpreterManager(PyEdit pyEdit, boolean isUnitTest) {
    String launchConfigurationType;
    String defaultType = Constants.ID_PYTHON_REGULAR_LAUNCH_CONFIGURATION_TYPE;
    IInterpreterManager interpreterManager = InterpreterManagersAPI.getPythonInterpreterManager();
    try {
        IPythonNature nature = pyEdit.getPythonNature();
        if (nature == null) {
            launchConfigurationType = defaultType;
        } else {
            int interpreterType = nature.getInterpreterType();
            interpreterManager = nature.getRelatedInterpreterManager();
            switch(interpreterType) {
                case IInterpreterManager.INTERPRETER_TYPE_PYTHON:
                    if (isUnitTest) {
                        launchConfigurationType = Constants.ID_PYTHON_UNITTEST_LAUNCH_CONFIGURATION_TYPE;
                    } else {
                        launchConfigurationType = Constants.ID_PYTHON_REGULAR_LAUNCH_CONFIGURATION_TYPE;
                    }
                    break;
                case IInterpreterManager.INTERPRETER_TYPE_IRONPYTHON:
                    if (isUnitTest) {
                        launchConfigurationType = Constants.ID_IRONPYTHON_UNITTEST_LAUNCH_CONFIGURATION_TYPE;
                    } else {
                        launchConfigurationType = Constants.ID_IRONPYTHON_LAUNCH_CONFIGURATION_TYPE;
                    }
                    break;
                case IInterpreterManager.INTERPRETER_TYPE_JYTHON:
                    if (isUnitTest) {
                        launchConfigurationType = Constants.ID_JYTHON_UNITTEST_LAUNCH_CONFIGURATION_TYPE;
                    } else {
                        launchConfigurationType = Constants.ID_JYTHON_LAUNCH_CONFIGURATION_TYPE;
                    }
                    break;
                default:
                    throw new RuntimeException("Cannot recognize type: " + interpreterType);
            }
        }
    } catch (Exception e) {
        Log.log(IStatus.INFO, "Problem determining nature type. Using regular python launch.", e);
        launchConfigurationType = defaultType;
    }
    return new Tuple<String, IInterpreterManager>(launchConfigurationType, interpreterManager);
}
Also used : IPythonNature(org.python.pydev.core.IPythonNature) IInterpreterManager(org.python.pydev.core.IInterpreterManager) Tuple(org.python.pydev.shared_core.structure.Tuple)

Example 24 with IInterpreterManager

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

the class DebugEditorBasedOnNatureTypeAction method run.

@Override
public void run(IAction action) {
    PyEdit pyEdit = getPyEdit();
    final Tuple<String, IInterpreterManager> launchConfigurationTypeAndInterpreterManager = this.getLaunchConfigurationTypeAndInterpreterManager(pyEdit, false);
    AbstractLaunchShortcut shortcut = new AbstractLaunchShortcut() {

        @Override
        protected String getLaunchConfigurationType() {
            return launchConfigurationTypeAndInterpreterManager.o1;
        }

        @Override
        protected IInterpreterManager getInterpreterManager(IProject project) {
            return launchConfigurationTypeAndInterpreterManager.o2;
        }
    };
    shortcut.launch(pyEdit, "debug");
}
Also used : AbstractLaunchShortcut(org.python.pydev.debug.ui.launching.AbstractLaunchShortcut) IInterpreterManager(org.python.pydev.core.IInterpreterManager) PyEdit(org.python.pydev.editor.PyEdit) IProject(org.eclipse.core.resources.IProject)

Example 25 with IInterpreterManager

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

the class AbstractLaunchShortcut method createDefaultLaunchConfigurationWithoutSaving.

public ILaunchConfigurationWorkingCopy createDefaultLaunchConfigurationWithoutSaving(FileOrResource[] resource) throws CoreException {
    IProject project;
    if (resource[0].resource != null) {
        project = resource[0].resource.getProject();
    } else {
        IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
        List<IProject> projectsLst = ArrayUtils.filter(projects, new ICallback<Boolean, IProject>() {

            @Override
            public Boolean call(IProject arg) {
                IProject project = arg;
                try {
                    return project.isOpen() && project.hasNature(PythonNature.PYTHON_NATURE_ID);
                } catch (CoreException e) {
                    return false;
                }
            }
        });
        final Object[] found = new Object[1];
        if (projectsLst.size() == 0) {
            found[0] = new CoreException(new StatusInfo(IStatus.ERROR, "Found no projects  with the Python nature in the workspace."));
        } else if (projectsLst.size() == 1) {
            found[0] = projectsLst.get(0);
        } else {
            RunInUiThread.sync(new Runnable() {

                @Override
                public void run() {
                    ProjectSelectionDialog dialog = new ProjectSelectionDialog(EditorUtils.getShell(), PythonNature.PYTHON_NATURE_ID);
                    dialog.setMessage("Choose the project that'll provide the interpreter and\n" + "PYTHONPATH to be used in the launch of the file.");
                    if (dialog.open() == Window.OK) {
                        Object firstResult = dialog.getFirstResult();
                        if (firstResult instanceof IProject) {
                            found[0] = firstResult;
                        } else {
                            found[0] = new CoreException(new StatusInfo(IStatus.ERROR, "Expected project to be selected."));
                        }
                    }
                }
            });
        }
        if (found[0] == null) {
            return null;
        }
        if (found[0] instanceof IProject) {
            project = (IProject) found[0];
        } else {
            if (found[0] instanceof CoreException) {
                throw (CoreException) found[0];
            } else {
                throw new CoreException(new StatusInfo(IStatus.ERROR, "Expected project, found: " + found[0]));
            }
        }
    }
    IInterpreterManager pythonInterpreterManager = getInterpreterManager(project);
    String projName = project.getName();
    ILaunchConfigurationWorkingCopy createdConfiguration = LaunchConfigurationCreator.createDefaultLaunchConfiguration(resource, getLaunchConfigurationType(), // it'll be made relative later on
    LaunchConfigurationCreator.getDefaultLocation(resource, false), pythonInterpreterManager, projName);
    // Common Tab Arguments
    CommonTab tab = new CommonTab();
    tab.setDefaults(createdConfiguration);
    tab.dispose();
    return createdConfiguration;
}
Also used : CommonTab(org.eclipse.debug.ui.CommonTab) ProjectSelectionDialog(org.python.pydev.shared_ui.dialogs.ProjectSelectionDialog) ILaunchConfigurationWorkingCopy(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy) IInterpreterManager(org.python.pydev.core.IInterpreterManager) IProject(org.eclipse.core.resources.IProject) CoreException(org.eclipse.core.runtime.CoreException) StatusInfo(org.python.pydev.plugin.StatusInfo)

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