Search in sources :

Example 6 with IInterpreterManager

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

the class PythonRunnerConfigTestWorkbench method testPythonCommandLine.

public void testPythonCommandLine() throws Exception {
    PythonNature nature = PythonNature.getPythonNature(mod1);
    // Create a temporary variable for testing
    IStringVariableManager variableManager = VariablesPlugin.getDefault().getStringVariableManager();
    IValueVariable myCustomVariable = variableManager.newValueVariable("pydev_python_runner_config_test_var", "", true, "my_custom_value");
    variableManager.addVariables(new IValueVariable[] { myCustomVariable });
    try {
        IInterpreterManager manager = InterpreterManagersAPI.getPythonInterpreterManager(true);
        InterpreterInfo info = (InterpreterInfo) manager.getDefaultInterpreterInfo(false);
        info.setEnvVariables(new String[] { "MY_CUSTOM_VAR_FOR_TEST=FOO", "MY_CUSTOM_VAR_FOR_TEST2=FOO2", "MY_CUSTOM_VAR_WITH_VAR=${pydev_python_runner_config_test_var}" });
        // Make sure variable hasn't been expanded too early
        assertTrue(arrayContains(info.getEnvVariables(), "MY_CUSTOM_VAR_WITH_VAR=${pydev_python_runner_config_test_var}"));
        PythonRunnerConfig runnerConfig = createConfig();
        assertTrue(arrayContains(runnerConfig.envp, "MY_CUSTOM_VAR_FOR_TEST=FOO"));
        assertTrue(arrayContains(runnerConfig.envp, "MY_CUSTOM_VAR_FOR_TEST2=FOO2"));
        assertTrue(arrayContains(runnerConfig.envp, "MY_CUSTOM_VAR_WITH_VAR=my_custom_value"));
        String[] argv = runnerConfig.getCommandLine(false);
        assertFalse(arrayContains(argv, PythonRunnerConfig.getRunFilesScript()));
        assertTrue(arrayContains(argv, mod1.getLocation().toOSString()));
        nature.setVersion(IPythonNature.Versions.PYTHON_VERSION_LATEST, IPythonNature.DEFAULT_INTERPRETER);
        assertEquals(manager.getDefaultInterpreterInfo(false).getExecutableOrJar(), nature.getProjectInterpreter().getExecutableOrJar());
        runnerConfig = createConfig();
        argv = runnerConfig.getCommandLine(false);
        assertEquals(manager.getDefaultInterpreterInfo(false).getExecutableOrJar(), argv[0]);
        IInterpreterManager interpreterManager = nature.getRelatedInterpreterManager();
        InterpreterInfo info2 = new InterpreterInfo(IPythonNature.PYTHON_VERSION_2_6, "c:\\interpreter\\py25.exe", new ArrayList<String>());
        interpreterManager.setInfos(new IInterpreterInfo[] { info, info2 }, null, null);
        nature.setVersion(IPythonNature.Versions.PYTHON_VERSION_LATEST, "c:\\interpreter\\py25.exe");
        assertEquals("c:\\interpreter\\py25.exe", nature.getProjectInterpreter().getExecutableOrJar());
        runnerConfig = createConfig();
        argv = runnerConfig.getCommandLine(false);
        assertEquals("c:\\interpreter\\py25.exe", argv[0]);
        nature.setVersion(IPythonNature.Versions.PYTHON_VERSION_LATEST, IPythonNature.DEFAULT_INTERPRETER);
        ILaunchConfiguration config;
        config = new LaunchShortcut().createDefaultLaunchConfiguration(FileOrResource.createArray(new IResource[] { mod1 }));
        ILaunchConfigurationWorkingCopy workingCopy = config.getWorkingCopy();
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("VAR_SPECIFIED_IN_LAUNCH", "BAR");
        // The one in the launch configuration always has preference.
        map.put("MY_CUSTOM_VAR_FOR_TEST2", "BAR2");
        workingCopy.setAttribute(ILaunchManager.ATTR_ENVIRONMENT_VARIABLES, map);
        config = workingCopy.doSave();
        runnerConfig = new PythonRunnerConfig(config, ILaunchManager.RUN_MODE, PythonRunnerConfig.RUN_REGULAR);
        assertTrue(arrayContains(runnerConfig.envp, "VAR_SPECIFIED_IN_LAUNCH=BAR"));
        assertTrue(arrayContains(runnerConfig.envp, "MY_CUSTOM_VAR_FOR_TEST=FOO"));
        assertTrue(arrayContains(runnerConfig.envp, "MY_CUSTOM_VAR_FOR_TEST2=BAR2"));
        assertTrue(arrayContains(runnerConfig.envp, "MY_CUSTOM_VAR_WITH_VAR=my_custom_value"));
    } catch (Throwable e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    } finally {
        // restore the default!
        nature.setVersion(IPythonNature.Versions.PYTHON_VERSION_LATEST, IPythonNature.DEFAULT_INTERPRETER);
        variableManager.removeVariables(new IValueVariable[] { myCustomVariable });
    }
}
Also used : ILaunchConfiguration(org.eclipse.debug.core.ILaunchConfiguration) IPythonNature(org.python.pydev.core.IPythonNature) PythonNature(org.python.pydev.plugin.nature.PythonNature) HashMap(java.util.HashMap) ILaunchConfigurationWorkingCopy(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy) IInterpreterManager(org.python.pydev.core.IInterpreterManager) IStringVariableManager(org.eclipse.core.variables.IStringVariableManager) IValueVariable(org.eclipse.core.variables.IValueVariable) InterpreterInfo(org.python.pydev.ast.interpreter_managers.InterpreterInfo) IInterpreterInfo(org.python.pydev.core.IInterpreterInfo)

Example 7 with IInterpreterManager

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

the class PyGlobalsBrowser method getFromSystemManager.

/**
 * @param selectedText the text that should be selected in the beginning (may be null)
 */
private void getFromSystemManager(String selectedText) {
    // is null
    File editorFile = getPyEdit().getEditorFile();
    Tuple<IPythonNature, String> infoForFile;
    if (editorFile != null) {
        infoForFile = InterpreterManagersAPI.getInfoForFile(editorFile);
    } else {
        infoForFile = null;
    }
    if (infoForFile != null) {
        IPythonNature systemPythonNature = infoForFile.o1;
        if (systemPythonNature == null) {
            getFromWorkspace(selectedText);
            return;
        }
        IInterpreterManager manager = infoForFile.o1.getRelatedInterpreterManager();
        getFromManagerAndRelatedNatures(selectedText, manager);
    } else {
        getFromWorkspace(selectedText);
    }
}
Also used : IPythonNature(org.python.pydev.core.IPythonNature) File(java.io.File) IInterpreterManager(org.python.pydev.core.IInterpreterManager)

Example 8 with IInterpreterManager

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

the class PyGlobalsBrowser method run.

@Override
public void run(IAction action) {
    IPythonNature pythonNature;
    try {
        pythonNature = getPyEdit().getPythonNature();
    } catch (MisconfigurationException e1) {
        handle(e1);
        return;
    }
    PySelection ps = PySelectionFromEditor.createPySelectionFromEditor(this.getPyEdit());
    String selectedText;
    try {
        selectedText = ps.getSelectedText();
    } catch (BadLocationException e1) {
        selectedText = null;
    }
    if (selectedText == null || selectedText.length() == 0) {
        try {
            selectedText = ps.getCurrToken().o1;
        } catch (BadLocationException e) {
        // ignore
        }
    }
    if (pythonNature != null) {
        IInterpreterManager manager = pythonNature.getRelatedInterpreterManager();
        getFromManagerAndRelatedNatures(selectedText, manager);
    } else {
        getFromSystemManager(selectedText);
    }
}
Also used : MisconfigurationException(org.python.pydev.core.MisconfigurationException) IPythonNature(org.python.pydev.core.IPythonNature) PySelection(org.python.pydev.core.docutils.PySelection) IInterpreterManager(org.python.pydev.core.IInterpreterManager) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 9 with IInterpreterManager

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

the class InfoFactory method createElement.

public AdditionalInfoAndIInfo createElement(IMemento memento) {
    String[] attributeKeys = null;
    try {
        attributeKeys = memento.getAttributeKeys();
        HashSet<String> keys = new HashSet<String>(Arrays.asList(attributeKeys));
        if (!keys.contains(TAG_NAME) || !keys.contains(TAG_MODULE_NAME) || !keys.contains(TAG_PATH) || !keys.contains(TAG_TYPE) || !keys.contains(TAG_FILE) || !keys.contains(TAG_LINE) || !keys.contains(TAG_COL)) {
            return null;
        }
        final String name = memento.getString(TAG_NAME);
        final String moduleName = memento.getString(TAG_MODULE_NAME);
        final String path = memento.getString(TAG_PATH);
        final String file = memento.getString(TAG_FILE);
        final int line = memento.getInteger(TAG_LINE);
        final int col = memento.getInteger(TAG_COL);
        final int type = memento.getInteger(TAG_TYPE);
        String infoName = null;
        String infoModule = null;
        String infoPath = null;
        if (name != null && name.length() > 0) {
            infoName = name;
        }
        if (moduleName != null && moduleName.length() > 0) {
            infoModule = moduleName;
        }
        if (path != null && path.length() > 0) {
            infoPath = path;
        }
        String projectName = null;
        if (keys.contains(TAG_PROJECT_NAME)) {
            projectName = memento.getString(TAG_PROJECT_NAME);
        }
        IPythonNature nature = null;
        if (projectName != null) {
            IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
            if (project != null) {
                nature = PythonNature.getPythonNature(project);
            }
        }
        AbstractInfo info;
        if (type == IInfo.ATTRIBUTE_WITH_IMPORT_TYPE) {
            info = new AttrInfo(infoName, infoModule, infoPath, nature, file, line, col);
        } else if (type == IInfo.CLASS_WITH_IMPORT_TYPE) {
            info = new ClassInfo(infoName, infoModule, infoPath, nature, file, line, col);
        } else if (type == IInfo.METHOD_WITH_IMPORT_TYPE) {
            info = new FuncInfo(infoName, infoModule, infoPath, nature, file, line, col);
        } else if (type == IInfo.NAME_WITH_IMPORT_TYPE) {
            info = new NameInfo(infoName, infoModule, infoPath, nature, file, line, col);
        } else if (type == IInfo.MOD_IMPORT_TYPE) {
            info = new ModInfo(infoModule, nature, file, line, col);
        } else {
            throw new AssertionError("Cannot restore type: " + type);
        }
        IInterpreterManager manager = null;
        if (projectName != null) {
            if (nature != null) {
                AbstractAdditionalDependencyInfo additionalInfo;
                try {
                    additionalInfo = AdditionalProjectInterpreterInfo.getAdditionalInfoForProject(nature);
                } catch (Exception e) {
                    // don't even report the error (this could happen if a saved option doesn't exist anymore)
                    return null;
                }
                return new AdditionalInfoAndIInfo(additionalInfo, info);
            }
        } else if (keys.contains(TAG_MANAGER_INTERPRETER_TYPE) && keys.contains(TAG_MANAGER_INTERPRETER)) {
            Integer interpreterType = memento.getInteger(TAG_MANAGER_INTERPRETER_TYPE);
            if (interpreterType != null) {
                switch(interpreterType) {
                    case IInterpreterManager.INTERPRETER_TYPE_PYTHON:
                        manager = InterpreterManagersAPI.getPythonInterpreterManager();
                        break;
                    case IInterpreterManager.INTERPRETER_TYPE_JYTHON:
                        manager = InterpreterManagersAPI.getJythonInterpreterManager();
                        break;
                    case IInterpreterManager.INTERPRETER_TYPE_IRONPYTHON:
                        manager = InterpreterManagersAPI.getIronpythonInterpreterManager();
                        break;
                }
            }
        } else if (keys.contains(TAG_MANAGER_IS_PYTHON) && keys.contains(TAG_MANAGER_INTERPRETER)) {
            // Kept for backward compatibility
            Boolean isTagPython = memento.getBoolean(TAG_MANAGER_IS_PYTHON);
            if (isTagPython != null) {
                if (isTagPython) {
                    manager = InterpreterManagersAPI.getPythonInterpreterManager();
                } else {
                    manager = InterpreterManagersAPI.getJythonInterpreterManager();
                }
            }
        }
        // If it gets here, it MUST contain the TAG_MANAGER_INTERPRETER
        if (manager != null) {
            String interpreter = memento.getString(TAG_MANAGER_INTERPRETER);
            AbstractAdditionalTokensInfo additionalInfo;
            try {
                additionalInfo = AdditionalSystemInterpreterInfo.getAdditionalSystemInfo(manager, interpreter);
            } catch (Exception e) {
                // don't even report the error (this could happen if a saved option doesn't exist anymore)
                return null;
            }
            if (additionalInfo != null) {
                return new AdditionalInfoAndIInfo(additionalInfo, info);
            }
        }
    } catch (Throwable e) {
        // Don't fail because we weren't able to restore some info, just log and return null (which clients should expect).
        Log.log(e);
        return null;
    }
    return null;
}
Also used : IPythonNature(org.python.pydev.core.IPythonNature) IInterpreterManager(org.python.pydev.core.IInterpreterManager) IProject(org.eclipse.core.resources.IProject) HashSet(java.util.HashSet)

Example 10 with IInterpreterManager

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

the class PythonCompletionProcessor method computeCompletionProposals.

/**
 * This is the interface implemented to get the completions.
 *
 * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeCompletionProposals(org.eclipse.jface.text.ITextViewer, int)
 */
@Override
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
    updateStatus();
    ICompletionProposalHandle[] proposals;
    try {
        // FIRST: discover activation token and qualifier.
        IDocument doc = viewer.getDocument();
        // list for storing the proposals
        TokensOrProposalsList pythonAndTemplateProposals = new TokensOrProposalsList();
        IPythonNature nature = edit.getPythonNature();
        if (nature == null) {
            IInterpreterManager manager = ChooseInterpreterManager.chooseInterpreterManager();
            if (manager != null) {
                nature = new SystemPythonNature(manager);
            } else {
                CompletionError completionError = new CompletionError(new RuntimeException("No interpreter configured."));
                this.error = completionError.getErrorMessage();
                return new ICompletionProposal[] { completionError };
            }
        }
        if (nature == null || !nature.startRequests()) {
            return new ICompletionProposal[0];
        }
        try {
            CompletionRequest request = new CompletionRequest(edit.getEditorFile(), nature, doc, documentOffset, codeCompletion, PyCodeCompletionPreferences.getUseSubstringMatchInCodeCompletion());
            // Get code completion proposals
            if (PyCodeCompletionPreferences.useCodeCompletion()) {
                if (whatToShow == SHOW_ALL) {
                    try {
                        pythonAndTemplateProposals.addAll(getPythonProposals(documentOffset, doc, request));
                    } catch (Throwable e) {
                        Log.log(e);
                        CompletionError completionError = new CompletionError(e);
                        this.error = completionError.getErrorMessage();
                        // Make the error visible to the user!
                        return new ICompletionProposal[] { completionError };
                    }
                }
            }
            String[] strs = PySelection.getActivationTokenAndQualifier(doc, documentOffset, false);
            String activationToken = strs[0];
            String qualifier = strs[1];
            // THIRD: Get template proposals (if asked for)
            if (request.showTemplates && (activationToken == null || activationToken.trim().length() == 0)) {
                TokensOrProposalsList templateProposals = getTemplateProposals(viewer, documentOffset, activationToken, qualifier);
                pythonAndTemplateProposals.addAll(templateProposals);
            }
            // to show the valid ones, we'll get the qualifier from the initial request
            proposals = PyCodeCompletionUtils.onlyValid(pythonAndTemplateProposals, request.qualifier, request.isInCalltip, request.useSubstringMatchInCodeCompletion, nature.getProject());
        // Note: sorting happens later on.
        } finally {
            nature.endRequests();
        }
    } catch (Exception e) {
        Log.log(e);
        CompletionError completionError = new CompletionError(e);
        this.error = completionError.getErrorMessage();
        // Make the error visible to the user!
        return new ICompletionProposal[] { completionError };
    }
    doCycle();
    return ConvertCompletionProposals.convertHandlesToProposals(proposals);
}
Also used : IPythonNature(org.python.pydev.core.IPythonNature) CompletionRequest(org.python.pydev.ast.codecompletion.CompletionRequest) TokensOrProposalsList(org.python.pydev.core.TokensOrProposalsList) SystemPythonNature(org.python.pydev.plugin.nature.SystemPythonNature) IInterpreterManager(org.python.pydev.core.IInterpreterManager) CoreException(org.eclipse.core.runtime.CoreException) BadLocationException(org.eclipse.jface.text.BadLocationException) PythonNatureWithoutProjectException(org.python.pydev.core.PythonNatureWithoutProjectException) IOException(java.io.IOException) MisconfigurationException(org.python.pydev.core.MisconfigurationException) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) ICompletionProposalHandle(org.python.pydev.shared_core.code_completion.ICompletionProposalHandle) IDocument(org.eclipse.jface.text.IDocument)

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