Search in sources :

Example 1 with IPythonInterpreter

use of org.python.pydev.shared_core.jython.IPythonInterpreter in project Pydev by fabioz.

the class JythonModules method makeISort.

public static String makeISort(String fileContents, File f, Set<String> knownThirdParty) {
    IPythonInterpreter iPythonInterpreter = iSortThreadLocalInterpreter.get();
    IPythonInterpreter interpreter;
    String outputLine = "output = getattr(isort.SortImports(file_contents=fileContents, settings_path=settingsPath, known_third_party=knownThirdParty), 'output', None)\n";
    if (iPythonInterpreter == null) {
        // The first call may be slow because doing the imports is slow, but subsequent calls should be
        // fast as we'll be reusing the same interpreter.
        String s = "" + "import sys\n" + "import os\n" + "add_to_pythonpath = '%s'\n" + "os.chdir(add_to_pythonpath)\n" + "if add_to_pythonpath not in sys.path:\n" + "    sys.path.append(add_to_pythonpath)\n" + "import isort\n" + outputLine;
        boolean useConsole = false;
        interpreter = JythonPlugin.newPythonInterpreter(useConsole, false);
        String isortContainerLocation = null;
        try {
            isortContainerLocation = CorePlugin.getScriptWithinPySrc(new Path("third_party").append("isort_container").toString()).toString();
            File isortContainer = new File(isortContainerLocation);
            if (!isortContainer.exists()) {
                Log.log("Specified location for isort_container does not exist (" + isortContainerLocation + ").");
                return null;
            }
        } catch (CoreException e) {
            Log.log("Error getting isort_container location", e);
            return null;
        }
        interpreter.set("fileContents", fileContents);
        if (f != null) {
            interpreter.set("settingsPath", f.getAbsoluteFile().getParent());
        } else {
            interpreter.set("settingsPath", "");
        }
        interpreter.set("knownThirdParty", new PyList(knownThirdParty));
        s = StringUtils.format(s, StringUtils.replaceAllSlashes(isortContainerLocation));
        interpreter.exec(s);
        iSortThreadLocalInterpreter.set(interpreter);
    } else {
        interpreter = iPythonInterpreter;
        // Found interpreter in thread local storage, just use it to do the sort.
        interpreter.set("fileContents", fileContents);
        if (f != null) {
            interpreter.set("settingsPath", f.getAbsoluteFile().getParent());
        } else {
            interpreter.set("settingsPath", "");
        }
        interpreter.set("knowhThirdParty", new PyList(knownThirdParty));
        // Note that we have to clear the global caches that isort has for it to reload the settings (otherwise,
        // eclipse needs to be restarted just to get the updated caches).
        interpreter.exec("" + "isort.settings._get_config_data.cache_clear()\n" + "isort.settings.from_path.cache_clear()\n" + outputLine);
    }
    PyObject pyObject = (PyObject) interpreter.get("output");
    if (pyObject != null && pyObject.__nonzero__()) {
        return pyObject.toString();
    }
    return null;
}
Also used : IPythonInterpreter(org.python.pydev.shared_core.jython.IPythonInterpreter) Path(org.eclipse.core.runtime.Path) CoreException(org.eclipse.core.runtime.CoreException) PyList(org.python.core.PyList) File(java.io.File) PyObject(org.python.core.PyObject)

Example 2 with IPythonInterpreter

use of org.python.pydev.shared_core.jython.IPythonInterpreter in project Pydev by fabioz.

the class JythonTest method testJythonTests.

public void testJythonTests() throws Exception {
    if (TestDependent.JYTHON_ANT_JAR_LOCATION == null) {
        System.out.println("Skipped running: JythonTest");
        return;
    }
    if (RUN_TESTS_ON_SAME_PROCESS) {
        // unittest.TestCase format: the __main__ is required in the global namespace
        HashMap<String, Object> locals = new HashMap<String, Object>();
        locals.put("__name__", "__main__");
        IPythonInterpreter interpreter = JythonPlugin.newPythonInterpreter(false, false);
        ByteArrayOutputStream stdErr = new ByteArrayOutputStream();
        ByteArrayOutputStream stdOut = new ByteArrayOutputStream();
        interpreter.setErr(stdErr);
        interpreter.setOut(stdOut);
        for (File f : foldersWithTestContentsOnSameProcess) {
            System.out.println("\n\nRunning tests from dir: " + f);
            List<Throwable> errors = JythonPlugin.execAll(locals, "test", interpreter, new File[] { f }, getAdditionalPythonpathFolders());
            System.out.println(stdOut);
            System.out.println(stdErr);
            failIfErrorsRaised(errors, stdErr);
        }
    }
}
Also used : IPythonInterpreter(org.python.pydev.shared_core.jython.IPythonInterpreter) HashMap(java.util.HashMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File)

Example 3 with IPythonInterpreter

use of org.python.pydev.shared_core.jython.IPythonInterpreter in project Pydev by fabioz.

the class JythonPep8 method analyzePep8WithJython.

public static void analyzePep8WithJython(JythonPep8Core pep8Params) {
    FastStringBuffer args = new FastStringBuffer(pep8Params.pep8CommandLine.length * 20);
    for (String string : pep8Params.pep8CommandLine) {
        args.append(',').append("r'").append(string).append('\'');
    }
    // It's important that the interpreter is created in the Thread and not outside the thread (otherwise
    // it may be that the output ends up being shared, which is not what we want.)
    IPythonInterpreter interpreter = JythonPlugin.newPythonInterpreter(pep8Params.useConsole, false);
    String file = StringUtils.replaceAllSlashes(pep8Params.absolutePath);
    interpreter.set("visitor", pep8Params.visitor);
    List<String> splitInLines = StringUtils.splitInLines(pep8Params.document.get());
    interpreter.set("lines", splitInLines);
    PyObject tempReportError = reportError;
    if (tempReportError != null) {
        interpreter.set("ReportError", tempReportError);
    } else {
        interpreter.set("ReportError", Py.None);
    }
    PyObject pep8Module = JythonModules.getPep8Module(interpreter);
    interpreter.set("pycodestyle", pep8Module);
    String formatted = StringUtils.format(EXECUTE_PEP8, file, args.toString(), file);
    interpreter.exec(formatted);
    if (reportError == null) {
        synchronized (lock) {
            if (reportError == null) {
                reportError = (PyObject) interpreter.get("ReportError");
            }
        }
    }
}
Also used : IPythonInterpreter(org.python.pydev.shared_core.jython.IPythonInterpreter) FastStringBuffer(org.python.pydev.shared_core.string.FastStringBuffer) PyObject(org.python.core.PyObject)

Example 4 with IPythonInterpreter

use of org.python.pydev.shared_core.jython.IPythonInterpreter in project Pydev by fabioz.

the class JythonPlugin method newPythonInterpreter.

/**
 * Creates a new Python interpreter (with jython) and returns it.
 *
 * Note that if the sys is not shared, clients should be in a Thread for it to be really separate).
 */
public static IPythonInterpreter newPythonInterpreter(boolean redirect, boolean shareSys) {
    // Important: setup the pythonpath for the jython process.
    setupJython();
    IPythonInterpreter interpreter;
    if (shareSys) {
        interpreter = new PythonInterpreterWrapper();
    } else {
        interpreter = new PythonInterpreterWrapperNotShared();
    }
    if (redirect) {
        interpreter.setOut(new ScriptOutput(new ICallback0<IOConsoleOutputStream>() {

            @Override
            public IOConsoleOutputStream call() {
                // Just to make sure it's initialized.
                getConsole();
                return fOutputStream;
            }
        }));
        interpreter.setErr(new ScriptOutput(new ICallback0<IOConsoleOutputStream>() {

            @Override
            public IOConsoleOutputStream call() {
                // Just to make sure it's initialized.
                getConsole();
                return fErrorStream;
            }
        }));
    } else {
        interpreter.setErr(NullOutputStream.singleton);
        interpreter.setOut(NullOutputStream.singleton);
    }
    return interpreter;
}
Also used : IPythonInterpreter(org.python.pydev.shared_core.jython.IPythonInterpreter) ICallback0(org.python.pydev.shared_core.callbacks.ICallback0)

Example 5 with IPythonInterpreter

use of org.python.pydev.shared_core.jython.IPythonInterpreter 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)

Aggregations

IPythonInterpreter (org.python.pydev.shared_core.jython.IPythonInterpreter)5 File (java.io.File)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 HashMap (java.util.HashMap)2 PyObject (org.python.core.PyObject)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 List (java.util.List)1 CoreException (org.eclipse.core.runtime.CoreException)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 Path (org.eclipse.core.runtime.Path)1 IInputValidator (org.eclipse.jface.dialogs.IInputValidator)1 InputDialog (org.eclipse.jface.dialogs.InputDialog)1 ProgressMonitorDialog (org.eclipse.jface.dialogs.ProgressMonitorDialog)1 IRunnableWithProgress (org.eclipse.jface.operation.IRunnableWithProgress)1 DisposeEvent (org.eclipse.swt.events.DisposeEvent)1 SelectionEvent (org.eclipse.swt.events.SelectionEvent)1 GridData (org.eclipse.swt.layout.GridData)1 Button (org.eclipse.swt.widgets.Button)1