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;
}
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);
}
}
}
}
}
}
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);
}
}
}
}
}
}
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());
}
}
}
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;
}
Aggregations