Search in sources :

Example 21 with InterpreterInfo

use of org.python.pydev.ast.interpreter_managers.InterpreterInfo in project Pydev by fabioz.

the class AbstractIOTestCase method createModuleAdapterFromDataSource.

/**
 * @param version IPythonNature.PYTHON_VERSION_XXX
 */
protected ModuleAdapter createModuleAdapterFromDataSource(String version) throws Throwable {
    codeCompletionTestsBase.restorePythonPath(FileUtils.getFileAbsolutePath(data.file.getParentFile()), true);
    PythonModuleManager pythonModuleManager = new PythonModuleManager(CodeCompletionTestsBase.nature);
    if (version != null) {
        // As the files will be found in the system, we need to set the system modules manager info.
        IModulesManager modulesManager = pythonModuleManager.getIModuleManager();
        SystemModulesManager systemModulesManager = (SystemModulesManager) modulesManager.getSystemModulesManager();
        systemModulesManager.setInfo(new InterpreterInfo(version, "", new ArrayList<String>()));
        CodeCompletionTestsBase.nature.setVersion(version, null);
    }
    ModuleAdapter module = VisitorFactory.createModuleAdapter(pythonModuleManager, data.file, new Document(data.source), CodeCompletionTestsBase.nature, CodeCompletionTestsBase.nature);
    return module;
}
Also used : ModuleAdapter(org.python.pydev.refactoring.ast.adapters.ModuleAdapter) ArrayList(java.util.ArrayList) InterpreterInfo(org.python.pydev.ast.interpreter_managers.InterpreterInfo) IModulesManager(org.python.pydev.core.IModulesManager) SystemModulesManager(org.python.pydev.ast.codecompletion.revisited.SystemModulesManager) Document(org.eclipse.jface.text.Document) PythonModuleManager(org.python.pydev.refactoring.ast.PythonModuleManager)

Example 22 with InterpreterInfo

use of org.python.pydev.ast.interpreter_managers.InterpreterInfo in project Pydev by fabioz.

the class AbstractInterpreterEditor method getTreeLibsControl.

/**
 * @param parent
 * @return
 */
private Tree getTreeLibsControl(Composite parent) {
    if (treeWithLibs == null) {
        treeWithLibs = new Tree(parent, SWT.BORDER | SWT.MULTI);
        treeWithLibs.setFont(parent.getFont());
        treeWithLibs.addDisposeListener(new DisposeListener() {

            @Override
            public void widgetDisposed(DisposeEvent event) {
                treeWithLibs = null;
            }
        });
        EnabledTreeDragReorder.enableDrag(treeWithLibs, false, false, new ICallback<Object, Object>() {

            @Override
            public Object call(Object arg) {
                if (treeWithInterpreters.getSelectionCount() == 1) {
                    TreeItem[] selection = treeWithInterpreters.getSelection();
                    String nameFromTreeItem = getNameFromTreeItem(selection[0]);
                    InterpreterInfo info = (InterpreterInfo) nameToInfo.get(nameFromTreeItem);
                    exeOrJarOfInterpretersToRestore.add(info.getExecutableOrJar());
                    info.libs.clear();
                    TreeItem[] items = treeWithLibs.getItems();
                    if (items.length == 1) {
                        TreeItem treeItem = items[0];
                        TreeItem[] items2 = treeItem.getItems();
                        for (TreeItem treeItem2 : items2) {
                            info.libs.add(treeItem2.getText(0));
                        }
                    }
                }
                return null;
            }
        });
    }
    return treeWithLibs;
}
Also used : DisposeListener(org.eclipse.swt.events.DisposeListener) TreeItem(org.eclipse.swt.widgets.TreeItem) Tree(org.eclipse.swt.widgets.Tree) InterpreterInfo(org.python.pydev.ast.interpreter_managers.InterpreterInfo) IInterpreterInfo(org.python.pydev.core.IInterpreterInfo) DisposeEvent(org.eclipse.swt.events.DisposeEvent)

Example 23 with InterpreterInfo

use of org.python.pydev.ast.interpreter_managers.InterpreterInfo in project Pydev by fabioz.

the class AbstractInterpreterEditor method doFillIntoGrid.

/**
 * @see org.eclipse.jface.preference.ListEditor#doFillIntoGrid(org.eclipse.swt.widgets.Composite, int)
 */
@Override
protected void doFillIntoGrid(Composite parent, int numColumns) {
    super.doFillIntoGrid(parent, numColumns);
    GridData gd = new GridData();
    tabFolder = new TabFolder(parent, SWT.None);
    gd = new GridData();
    gd.horizontalAlignment = SWT.FILL;
    gd.verticalAlignment = SWT.FILL;
    gd.grabExcessVerticalSpace = true;
    gd.horizontalSpan = numColumns;
    tabFolder.setLayoutData(gd);
    try {
        if (this.getShowPackageTab()) {
            packageTab.createPackageControlTab(tabFolder, exeOrJarOfInterpretersToRestore, interpreterManager);
        }
    } catch (Exception e1) {
        // Not really expected, just new code, so, let's protect until it matures.
        Log.log(e1);
    }
    createTreeLibsControlTab();
    // ----------------------- FORCED BUILTINS
    forcedBuiltins = new AbstractListWithNewRemoveControl(this) {

        @Override
        protected List<String> getStringsFromInfo(InterpreterInfo info) {
            ArrayList<String> ret = new ArrayList<String>();
            for (Iterator<String> iter = info.forcedLibsIterator(); iter.hasNext(); ) {
                ret.add(iter.next());
            }
            return ret;
        }

        @Override
        protected void removeSelectedFrominfo(InterpreterInfo info, String[] builtins) {
            for (String builtin : builtins) {
                info.removeForcedLib(builtin);
            }
            exeOrJarOfInterpretersWithBuiltinsChanged.add(info.getExecutableOrJar());
        }

        @Override
        protected String getInput() {
            IInputValidator validator = new IInputValidator() {

                @Override
                public String isValid(String newText) {
                    for (char c : newText.toCharArray()) {
                        if (!Character.isJavaIdentifierPart(c) && c != ' ' && c != ',' && c != '.') {
                            return "Can only accept valid python module names (char: '" + c + "' not accepted)";
                        }
                    }
                    return null;
                }
            };
            InputDialog d = new InputDialog(getShell(), "Builtin to add", "Builtin to add (comma separated)", "", validator);
            int retCode = d.open();
            String builtins = null;
            if (retCode == InputDialog.OK) {
                builtins = d.getValue();
            }
            return builtins;
        }

        @Override
        protected void addInputToInfo(InterpreterInfo info, String builtins) {
            java.util.List<String> split = StringUtils.splitAndRemoveEmptyTrimmed(builtins, ',');
            for (String string : split) {
                String trimmed = string.trim();
                if (trimmed.length() > 0) {
                    info.addForcedLib(trimmed);
                }
            }
            exeOrJarOfInterpretersWithBuiltinsChanged.add(info.getExecutableOrJar());
        }
    };
    forcedBuiltins.createTab("Forced Builtins", "Forced Builtins (check <a>Manual</a> for more info).");
    // ----------------------- PREDEFINED COMPLETIONS
    predefinedCompletions = new AbstractListWithNewRemoveControl(this) {

        private Button addAPIBt;

        @Override
        protected List<String> getStringsFromInfo(InterpreterInfo info) {
            return info.getPredefinedCompletionsPath();
        }

        @Override
        protected void removeSelectedFrominfo(InterpreterInfo info, String[] items) {
            for (String item : items) {
                info.removePredefinedCompletionPath(item);
            }
            exeOrJarOfInterpretersWithPredefinedChanged.add(info.getExecutableOrJar());
        }

        @Override
        protected String getInput() {
            DirectoryDialog dialog = new DirectoryDialog(getShell());
            dialog.setFilterPath(lastDirectoryDialogPath);
            String filePath = dialog.open();
            if (filePath != null) {
                lastDirectoryDialogPath = filePath;
            }
            return filePath;
        }

        @Override
        protected void addInputToInfo(InterpreterInfo info, String item) {
            info.addPredefinedCompletionsPath(item);
            exeOrJarOfInterpretersWithPredefinedChanged.add(info.getExecutableOrJar());
        }

        @Override
        protected void createButtons(AbstractInterpreterEditor interpreterEditor) {
            super.createButtons(interpreterEditor);
            // $NON-NLS-1$
            addAPIBt = interpreterEditor.createBt(box, "Add from QScintilla api file", this);
        }

        @Override
        public void widgetDisposed(DisposeEvent event) {
            super.widgetDisposed(event);
            if (addAPIBt != null) {
                addAPIBt.dispose();
                addAPIBt = null;
            }
        }

        @Override
        public void widgetSelected(SelectionEvent event) {
            super.widgetSelected(event);
            Widget widget = event.widget;
            if (widget == addAPIBt) {
                addAPIBt();
            }
        }

        private void addAPIBt() {
            final AbstractInterpreterEditor interpreterEditor = this.container.get();
            Assert.isNotNull(interpreterEditor);
            final InterpreterInfo info = interpreterEditor.getSelectedInfo();
            if (info != null) {
                FileDialog dialog = new FileDialog(getShell(), SWT.PRIMARY_MODAL | SWT.MULTI);
                dialog.setFilterExtensions(new String[] { "*.api" });
                dialog.setText("Select .api file to be converted to .pypredef.");
                dialog.setFilterPath(lastFileDialogPath);
                final String filePath = dialog.open();
                if (filePath != null) {
                    lastFileDialogPath = filePath;
                    File filePath1 = new File(filePath);
                    final String dir = filePath1.getParent();
                    IInputValidator validator = new IInputValidator() {

                        @Override
                        public String isValid(String newText) {
                            if (newText.length() == 0) {
                                return "Number not provided.";
                            }
                            try {
                                Integer.parseInt(newText);
                            } catch (NumberFormatException e) {
                                return "The string: " + newText + " is not a valid integer.";
                            }
                            return null;
                        }
                    };
                    final InputDialog d = new InputDialog(getShell(), "Number of tokens to consider module", "Please specify the number of tokens to consider a module from the .api file\n\n" + "i.e.: if there's a PyQt4.QtCore.QObject and PyQt4.QtCore is a module and QtObject " + "is the first class, the number of tokens to consider a module would be 2 (one for " + "PyQt4 and another for QtCore).", "", validator);
                    int retCode = d.open();
                    final ByteArrayOutputStream output = new ByteArrayOutputStream();
                    if (retCode == InputDialog.OK) {
                        ProgressMonitorDialog monitorDialog = new AsynchronousProgressMonitorDialog(getShell());
                        monitorDialog.setBlockOnOpen(false);
                        final Exception[] exception = new Exception[1];
                        try {
                            IRunnableWithProgress operation = new IRunnableWithProgress() {

                                @Override
                                public void run(final IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                                    monitor.beginTask("Restoring PYTHONPATH", IProgressMonitor.UNKNOWN);
                                    IPythonInterpreter interpreter = JythonPlugin.newPythonInterpreter(false, false);
                                    interpreter.setErr(output);
                                    interpreter.setOut(output);
                                    HashMap<String, Object> locals = new HashMap<String, Object>();
                                    locals.put("api_file", filePath);
                                    locals.put("parts_for_module", d.getValue());
                                    locals.put("cancel_monitor", monitor);
                                    try {
                                        JythonPlugin.exec(locals, "convert_api_to_pypredef.py", interpreter);
                                    } catch (Exception e) {
                                        Log.log(e + "\n\n" + output.toString());
                                        exception[0] = e;
                                    }
                                    monitor.done();
                                }
                            };
                            monitorDialog.run(true, true, operation);
                        } catch (Exception e) {
                            Log.log(e);
                        }
                        Exception e = exception[0];
                        String contents = output.toString();
                        if (e == null && contents.indexOf("SUCCESS") != -1) {
                            addInputToInfo(info, dir);
                            interpreterEditor.updateTree();
                        } else {
                            if (e != null) {
                                MessageDialog.openError(getShell(), "Error creating .pypredef files", e.getMessage() + "\n\n" + contents);
                            } else {
                                MessageDialog.openError(getShell(), "Error creating .pypredef files", contents);
                            }
                        }
                    }
                }
            }
        }
    };
    predefinedCompletions.createTab("Predefined", "Predefined completions (check <a>Manual</a> for more info).");
    createEnvironmentVariablesTab();
    createStringSubstitutionTab();
}
Also used : AsynchronousProgressMonitorDialog(org.python.pydev.shared_ui.utils.AsynchronousProgressMonitorDialog) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Widget(org.eclipse.swt.widgets.Widget) DisposeEvent(org.eclipse.swt.events.DisposeEvent) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) Button(org.eclipse.swt.widgets.Button) Iterator(java.util.Iterator) SelectionEvent(org.eclipse.swt.events.SelectionEvent) InterpreterInfo(org.python.pydev.ast.interpreter_managers.InterpreterInfo) IInterpreterInfo(org.python.pydev.core.IInterpreterInfo) List(java.util.List) ArrayList(java.util.ArrayList) DirectoryDialog(org.eclipse.swt.widgets.DirectoryDialog) InputDialog(org.eclipse.jface.dialogs.InputDialog) InterpreterInputDialog(org.python.pydev.ui.dialogs.InterpreterInputDialog) AsynchronousProgressMonitorDialog(org.python.pydev.shared_ui.utils.AsynchronousProgressMonitorDialog) ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) TabFolder(org.eclipse.swt.widgets.TabFolder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InvocationTargetException(java.lang.reflect.InvocationTargetException) CancelException(org.python.pydev.shared_core.progress.CancelException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IPythonInterpreter(org.python.pydev.shared_core.jython.IPythonInterpreter) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) GridData(org.eclipse.swt.layout.GridData) FileDialog(org.eclipse.swt.widgets.FileDialog) File(java.io.File) IInputValidator(org.eclipse.jface.dialogs.IInputValidator)

Example 24 with InterpreterInfo

use of org.python.pydev.ast.interpreter_managers.InterpreterInfo in project Pydev by fabioz.

the class RefactoringRenameTestBase method checkSize.

/**
 * checks if the size of the system modules manager and the project moule manager are coherent
 * (we must have more modules in the system than in the project)
 */
@Override
protected void checkSize() {
    try {
        IInterpreterManager iMan = getInterpreterManager();
        InterpreterInfo info = (InterpreterInfo) iMan.getDefaultInterpreterInfo(false);
        assertTrue(info.getModulesManager().getSize(true) > 0);
        int size = ((ASTManager) natureRefactoring.getAstManager()).getSize();
        assertTrue("Interpreter size:" + info.getModulesManager().getSize(true) + " should be smaller than project size:" + size + " " + "(because it contains system+project info)", info.getModulesManager().getSize(true) < size);
    } catch (MisconfigurationException e) {
        throw new RuntimeException(e);
    }
}
Also used : ASTManager(org.python.pydev.ast.codecompletion.revisited.ASTManager) MisconfigurationException(org.python.pydev.core.MisconfigurationException) InterpreterInfo(org.python.pydev.ast.interpreter_managers.InterpreterInfo) AdditionalProjectInterpreterInfo(com.python.pydev.analysis.additionalinfo.AdditionalProjectInterpreterInfo) IInterpreterManager(org.python.pydev.core.IInterpreterManager) PyRenameEntryPoint(com.python.pydev.analysis.refactoring.wizards.rename.PyRenameEntryPoint)

Example 25 with InterpreterInfo

use of org.python.pydev.ast.interpreter_managers.InterpreterInfo in project Pydev by fabioz.

the class CodeCompletionTestsBase method restoreSystemPythonPath.

/**
 * @return whether is was actually restored (given the force parameter)
 */
protected boolean restoreSystemPythonPath(boolean force, String path) {
    if (restoredSystem == null || restoredSystem != this.getClass() || force) {
        // restore manager and cache
        setInterpreterManager(path);
        restoredSystem = this.getClass();
        // get default and restore the pythonpath
        InterpreterInfo info = getDefaultInterpreterInfo();
        this.beforeRestore(info);
        info.restoreCompiledLibs(getProgressMonitor());
        if (ADD_MX_TO_FORCED_BUILTINS) {
            info.addForcedLib("mx");
        }
        if (ADD_NUMPY_TO_FORCED_BUILTINS) {
            info.addForcedLib("numpy");
        }
        // postconditions
        afterRestorSystemPythonPath(info);
        return true;
    }
    return false;
}
Also used : InterpreterInfo(org.python.pydev.ast.interpreter_managers.InterpreterInfo) IInterpreterInfo(org.python.pydev.core.IInterpreterInfo)

Aggregations

InterpreterInfo (org.python.pydev.ast.interpreter_managers.InterpreterInfo)34 IInterpreterInfo (org.python.pydev.core.IInterpreterInfo)27 IInterpreterManager (org.python.pydev.core.IInterpreterManager)13 ArrayList (java.util.ArrayList)11 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)9 AdditionalSystemInterpreterInfo (com.python.pydev.analysis.additionalinfo.AdditionalSystemInterpreterInfo)7 HashMap (java.util.HashMap)5 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)5 File (java.io.File)4 Collection (java.util.Collection)4 HashSet (java.util.HashSet)4 IEclipsePreferences (org.eclipse.core.runtime.preferences.IEclipsePreferences)4 PythonInterpreterManager (org.python.pydev.ast.interpreter_managers.PythonInterpreterManager)4 MisconfigurationException (org.python.pydev.core.MisconfigurationException)4 DataAndImageTreeNode (org.python.pydev.shared_core.structure.DataAndImageTreeNode)4 TreeNode (org.python.pydev.shared_core.structure.TreeNode)4 Tuple (org.python.pydev.shared_core.structure.Tuple)4 Map (java.util.Map)3 SyncSystemModulesManager (org.python.pydev.ast.codecompletion.revisited.SyncSystemModulesManager)3 IInfo (org.python.pydev.core.IInfo)3