Search in sources :

Example 16 with PythonNature

use of org.python.pydev.plugin.nature.PythonNature in project Pydev by fabioz.

the class Py2To3 method confirmRun.

@Override
protected boolean confirmRun() {
    clearRunInput();
    PythonNature nature = null;
    for (IResource c : selectedResources) {
        PythonNature n2 = PythonNature.getPythonNature(c);
        if (n2 != null) {
            if (nature == null) {
                nature = n2;
            } else {
                if (n2 != nature) {
                    MessageBox message = new MessageBox(EditorUtils.getShell(), SWT.OK | SWT.ICON_ERROR);
                    message.setText("Multiple python natures");
                    message.setMessage("This action can only be applied in one project at a time.");
                    message.open();
                    return false;
                }
            }
        }
    }
    if (nature == null) {
        MessageBox message = new MessageBox(EditorUtils.getShell(), SWT.OK | SWT.ICON_ERROR);
        message.setText("No nature found");
        message.setMessage("This action can only be applied in a project that is configured as a Pydev project.");
        message.open();
        return false;
    }
    AbstractRunner runner = UniversalRunner.getRunner(nature);
    Tuple<String, String> tup = runner.runCodeAndGetOutput(RUN_2_TO_3_CODE, new String[] { "--help" }, null, new NullProgressMonitor());
    if (tup.o1.indexOf("ImportError") != -1 || tup.o2.indexOf("ImportError") != -1) {
        MessageBox message = new MessageBox(EditorUtils.getShell(), SWT.OK | SWT.ICON_ERROR);
        message.setText("Unable to run 2to3");
        message.setMessage("Unable to run 2to3. Details: \n" + tup.o1 + "\n" + tup.o2 + "\n\nNotes: check if lib2to3 is properly installed in your Python install.");
        message.open();
        return false;
    }
    String msg = "Please enter the parameters to be passed for 2to3.py\n\n" + tup.o1 + "\n\n" + "E.g.: \n" + "Leave empty for preview\n" + "-w to apply with backup\n" + "-w -n to apply without backup.";
    if (tup.o2.length() > 0) {
        msg += "\n";
        msg += tup.o2;
    }
    final List<String> splitInLines = StringUtils.splitInLines(msg);
    int max = 10;
    for (String string : splitInLines) {
        max = Math.max(string.length(), max);
    }
    final int maxChars = max;
    InputDialogWithLongMessage d = new InputDialogWithLongMessage(EditorUtils.getShell(), "Parameters for 2to3.py", msg, "", null) {

        int averageCharWidth;

        int height;

        @Override
        protected boolean isResizable() {
            return true;
        }

        @Override
        protected String getOkButtonLabel() {
            return "Run with specified parameters";
        }

        @Override
        protected Control createDialogArea(Composite parent) {
            try {
                FontData labelFontData = FontUtils.getFontData(IFontUsage.DIALOG, false);
                Display display = parent.getDisplay();
                Font font = new Font(display, labelFontData);
                parent.setFont(font);
                GC gc = new GC(display);
                gc.setFont(font);
                FontMetrics fontMetrics = gc.getFontMetrics();
                averageCharWidth = fontMetrics.getAverageCharWidth();
                height = fontMetrics.getHeight();
                gc.dispose();
            } catch (Throwable e) {
            // ignore
            }
            return super.createDialogArea(parent);
        }

        @Override
        protected Point getInitialSize() {
            Point result = super.getInitialSize();
            // Check if we were able to get proper values before changing it.
            if (averageCharWidth > 0 && maxChars > 0) {
                result.x = (int) (averageCharWidth * maxChars * 1.15);
            }
            if (height > 0 && splitInLines.size() > 0) {
                // put some lines extra (we need the input line too)
                result.y = Math.max(height * (int) (splitInLines.size() * 1.5), result.y);
            }
            return result;
        }
    };
    int retCode = d.open();
    if (retCode != InputDialog.OK) {
        return false;
    }
    MessageConsole console = MessageConsoles.getConsole("2To3", UIConstants.PY_INTERPRETER_ICON);
    console.clearConsole();
    parameters = StringUtils.split(d.getValue(), " ");
    natureUsed = nature;
    return true;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) MessageConsole(org.eclipse.ui.console.MessageConsole) PythonNature(org.python.pydev.plugin.nature.PythonNature) Composite(org.eclipse.swt.widgets.Composite) InputDialogWithLongMessage(org.python.pydev.shared_ui.dialogs.InputDialogWithLongMessage) FontData(org.eclipse.swt.graphics.FontData) Point(org.eclipse.swt.graphics.Point) Point(org.eclipse.swt.graphics.Point) Font(org.eclipse.swt.graphics.Font) MessageBox(org.eclipse.swt.widgets.MessageBox) AbstractRunner(org.python.pydev.ast.runners.UniversalRunner.AbstractRunner) FontMetrics(org.eclipse.swt.graphics.FontMetrics) GC(org.eclipse.swt.graphics.GC) IResource(org.eclipse.core.resources.IResource) Display(org.eclipse.swt.widgets.Display)

Example 17 with PythonNature

use of org.python.pydev.plugin.nature.PythonNature in project Pydev by fabioz.

the class PyLintVisitor method startVisit.

/**
 * When we start visiting some resource, we create the process which will do the PyLint analysis.
 */
@Override
public void startVisit() {
    if (resource == null || PyLintPreferences.usePyLint(resource) == false || (document == null && !(resource instanceof IContainer))) {
        deleteMarkers();
        return;
    }
    IProject project = resource.getProject();
    PythonNature pythonNature = PythonNature.getPythonNature(project);
    if (pythonNature == null) {
        deleteMarkers();
        return;
    }
    File pyLintLocation = PyLintPreferences.getPyLintLocation(pythonNature, resource);
    if (pyLintLocation == null || !FileUtils.enhancedIsFile(pyLintLocation)) {
        deleteMarkers();
        return;
    }
    try {
        // PyLint can only be used for python projects
        if (pythonNature.getInterpreterType() != IInterpreterManager.INTERPRETER_TYPE_PYTHON) {
            deleteMarkers();
            return;
        }
    } catch (Exception e) {
        deleteMarkers();
        return;
    }
    synchronized (lock) {
        if (pyLintRunnable != null) {
            // (we should be analyzing multiple resources in a single call).
            return;
        }
        if (project != null) {
            if (resource instanceof IFile) {
                IFile file = (IFile) resource;
                IPath location = file.getLocation();
                if (location != null) {
                    pyLintRunnable = new PyLintAnalysis(resource, document, location, new NullProgressMonitorWrapper(monitor), pyLintLocation);
                    try {
                        IExternalCodeAnalysisStream out = PyLintPreferences.getConsoleOutputStream(resource);
                        pyLintRunnable.createPyLintProcess(out);
                    } catch (final Exception e) {
                        Log.log(e);
                    }
                }
            } else if (resource instanceof IContainer) {
                IContainer dir = (IContainer) resource;
                IPath location = dir.getLocation();
                if (location != null) {
                    pyLintRunnable = new PyLintAnalysis(resource, null, location, new NullProgressMonitorWrapper(monitor), pyLintLocation);
                    try {
                        IExternalCodeAnalysisStream out = PyLintPreferences.getConsoleOutputStream(resource);
                        pyLintRunnable.createPyLintProcess(out);
                    } catch (final Exception e) {
                        Log.log(e);
                    }
                }
            }
        }
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) PythonNature(org.python.pydev.plugin.nature.PythonNature) IPath(org.eclipse.core.runtime.IPath) IExternalCodeAnalysisStream(com.python.pydev.analysis.external.IExternalCodeAnalysisStream) NullProgressMonitorWrapper(org.python.pydev.shared_core.progress.NullProgressMonitorWrapper) IContainer(org.eclipse.core.resources.IContainer) File(java.io.File) IFile(org.eclipse.core.resources.IFile) IProject(org.eclipse.core.resources.IProject)

Example 18 with PythonNature

use of org.python.pydev.plugin.nature.PythonNature in project Pydev by fabioz.

the class MypyVisitor method startVisit.

/**
 * When we start visiting some resource, we create the process which will do the Mypy analysis.
 */
@Override
public void startVisit() {
    if (resource == null || MypyPreferences.useMypy(resource) == false || (document == null && !(resource instanceof IContainer))) {
        deleteMarkers();
        return;
    }
    IProject project = resource.getProject();
    PythonNature pythonNature = PythonNature.getPythonNature(project);
    if (pythonNature == null) {
        deleteMarkers();
        return;
    }
    File mypyLocation = MypyPreferences.getMypyLocation(pythonNature);
    if (mypyLocation == null || !FileUtils.enhancedIsFile(mypyLocation)) {
        if (mypyLocation == null) {
            Log.log("Unable to find mypy. Project: " + project.getName());
        } else {
            Log.log("mypy location does not exist: " + mypyLocation);
        }
        deleteMarkers();
        return;
    }
    try {
        // Mypy can only be used for python projects
        if (pythonNature.getInterpreterType() != IInterpreterManager.INTERPRETER_TYPE_PYTHON) {
            deleteMarkers();
            return;
        }
    } catch (Exception e) {
        deleteMarkers();
        return;
    }
    synchronized (lock) {
        if (mypyRunnable != null) {
            // (we should be analyzing multiple resources in a single call).
            return;
        }
        if (project != null) {
            if (resource instanceof IFile) {
                IFile file = (IFile) resource;
                IPath location = file.getLocation();
                if (location != null) {
                    mypyRunnable = new MypyAnalysis(resource, document, location, new NullProgressMonitorWrapper(monitor), mypyLocation);
                    try {
                        IExternalCodeAnalysisStream out = MypyPreferences.getConsoleOutputStream(project);
                        mypyRunnable.createMypyProcess(out);
                    } catch (final Exception e) {
                        Log.log(e);
                    }
                }
            } else if (resource instanceof IContainer) {
                IContainer dir = (IContainer) resource;
                IPath location = dir.getLocation();
                if (location != null) {
                    mypyRunnable = new MypyAnalysis(resource, null, location, new NullProgressMonitorWrapper(monitor), mypyLocation);
                    try {
                        IExternalCodeAnalysisStream out = MypyPreferences.getConsoleOutputStream(project);
                        mypyRunnable.createMypyProcess(out);
                    } catch (final Exception e) {
                        Log.log(e);
                    }
                }
            }
        }
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) PythonNature(org.python.pydev.plugin.nature.PythonNature) IPath(org.eclipse.core.runtime.IPath) IExternalCodeAnalysisStream(com.python.pydev.analysis.external.IExternalCodeAnalysisStream) NullProgressMonitorWrapper(org.python.pydev.shared_core.progress.NullProgressMonitorWrapper) IContainer(org.eclipse.core.resources.IContainer) File(java.io.File) IFile(org.eclipse.core.resources.IFile) IProject(org.eclipse.core.resources.IProject)

Example 19 with PythonNature

use of org.python.pydev.plugin.nature.PythonNature in project Pydev by fabioz.

the class PyCodeCompletionVisitor method visitRemovedResource.

@Override
public void visitRemovedResource(IResource resource, ICallback0<IDocument> document, IProgressMonitor monitor) {
    PythonNature pythonNature = getPythonNature(resource);
    if (pythonNature != null) {
        ICodeCompletionASTManager astManager = pythonNature.getAstManager();
        if (astManager != null) {
            IPath location = resource.getLocation();
            astManager.removeModule(new File(location.toOSString()), resource.getProject(), new NullProgressMonitor());
        }
    }
}
Also used : ICodeCompletionASTManager(org.python.pydev.core.ICodeCompletionASTManager) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IPythonNature(org.python.pydev.core.IPythonNature) PythonNature(org.python.pydev.plugin.nature.PythonNature) IPath(org.eclipse.core.runtime.IPath) File(java.io.File)

Example 20 with PythonNature

use of org.python.pydev.plugin.nature.PythonNature in project Pydev by fabioz.

the class PyDevBuilderVisitor method isInPythonPath.

/**
 * @param resource the resource we want to know about
 * @return true if it is in the pythonpath
 */
public static boolean isInPythonPath(IResource resource) {
    if (resource == null) {
        return false;
    }
    IProject project = resource.getProject();
    PythonNature nature = PythonNature.getPythonNature(project);
    if (project != null && nature != null) {
        ICodeCompletionASTManager astManager = nature.getAstManager();
        if (astManager != null) {
            IModulesManager modulesManager = astManager.getModulesManager();
            return modulesManager.isInPythonPath(resource, project);
        }
    }
    return false;
}
Also used : ICodeCompletionASTManager(org.python.pydev.core.ICodeCompletionASTManager) IPythonNature(org.python.pydev.core.IPythonNature) PythonNature(org.python.pydev.plugin.nature.PythonNature) IModulesManager(org.python.pydev.core.IModulesManager) IProject(org.eclipse.core.resources.IProject)

Aggregations

PythonNature (org.python.pydev.plugin.nature.PythonNature)88 IProject (org.eclipse.core.resources.IProject)41 IPythonNature (org.python.pydev.core.IPythonNature)32 File (java.io.File)31 HashSet (java.util.HashSet)20 IFile (org.eclipse.core.resources.IFile)20 ArrayList (java.util.ArrayList)19 CoreException (org.eclipse.core.runtime.CoreException)18 IResource (org.eclipse.core.resources.IResource)17 IPath (org.eclipse.core.runtime.IPath)14 IContainer (org.eclipse.core.resources.IContainer)12 MisconfigurationException (org.python.pydev.core.MisconfigurationException)11 PythonSourceFolder (org.python.pydev.navigator.elements.PythonSourceFolder)10 HashMap (java.util.HashMap)9 IPythonPathNature (org.python.pydev.core.IPythonPathNature)9 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)8 ICodeCompletionASTManager (org.python.pydev.core.ICodeCompletionASTManager)8 IFolder (org.eclipse.core.resources.IFolder)7 IInterpreterInfo (org.python.pydev.core.IInterpreterInfo)7 Set (java.util.Set)6