private String checkValidSourceFolder(String text) throws CoreException { validatedSourceFolder = null; if (text == null || text.trim().length() == 0) { return "The source folder must be filled."; } IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); IResource resource = root.findMember(new Path(text)); if (resource == null) { return "The source folder was not found in the workspace."; } if (!(resource instanceof IContainer)) { return "The source folder was found in the workspace but is not a container."; } IProject project = resource.getProject(); if (project == null) { return "Unable to find the project related to the source folder."; } IPythonPathNature nature = PythonNature.getPythonPathNature(project); if (nature == null) { return "The pydev nature is not configured on the project: " + project.getName(); } String full = resource.getFullPath().toString(); String[] srcPaths = PythonNature.getStrAsStrItems(nature.getProjectSourcePath(true)); for (String str : srcPaths) { if (str.equals(full)) { validatedSourceFolder = (IContainer) resource; return null; } } return "The selected source folder is not recognized as a valid source folder."; }
/** * @param f * @return * @throws CoreException */ public String getSrcFolderFromFolder(IFolder f) throws CoreException { IPythonPathNature nature = PythonNature.getPythonPathNature(f.getProject()); if (nature != null) { String[] srcPaths = PythonNature.getStrAsStrItems(nature.getProjectSourcePath(true)); String relFolder = f.getFullPath().toString() + "/"; for (String src : srcPaths) { if (relFolder.startsWith(src + "/")) { return src; } } } return null; }
/** * Creates a string that can be passed as the PYTHONPATH * * @param project the project we want to get the settings from. If it is null, the system * pythonpath is returned * @param interpreter this is the interpreter to be used to create the env. * @return a string that can be used as the PYTHONPATH env variable */ public static String makePythonPathEnvString( IPythonNature pythonNature, IInterpreterInfo interpreter, IInterpreterManager manager) { if (pythonNature == null) { if (interpreter == null) { return makePythonPathEnvFromPaths( new ArrayList< String>()); // no pythonpath can be gotten (set to empty, so that the default is // gotten) } else { List<String> pythonPath = interpreter.getPythonPath(); return makePythonPathEnvFromPaths(pythonPath); } } List<String> paths; // if we have a project, get its complete pythonpath IPythonPathNature pythonPathNature = pythonNature.getPythonPathNature(); if (pythonPathNature == null) { IProject project = pythonNature.getProject(); String projectName; if (project == null) { projectName = "null?"; } else { projectName = project.getName(); } throw new RuntimeException( "The project " + projectName + " does not have the pythonpath configured, \n" + "please configure it correcly (please check the pydev getting started guide at \n" + "http://pydev.org/manual_101_root.html for better information on how to do it)."); } paths = pythonPathNature.getCompleteProjectPythonPath(interpreter, manager); return makePythonPathEnvFromPaths(paths); }
@Override protected IStatus run(IProgressMonitor monitor) { try { if (astManager != null) { String pythonpath = pythonPathNature.getOnlyProjectPythonPathStr(true); PythonPathHelper pythonPathHelper = (PythonPathHelper) astManager.getModulesManager().getPythonPathHelper(); // If it doesn't match, rebuid the pythonpath! if (!new HashSet<String>(PythonPathHelper.parsePythonPathFromStr(pythonpath, null)) .equals(new HashSet<String>(pythonPathHelper.getPythonpath()))) { rebuildPath(); } } } catch (CoreException e) { Log.log(e); } return Status.OK_STATUS; }
/** * @param topLevel * @return */ private boolean createSourceFolderSelect(Composite topLevel) { Label label; label = new Label(topLevel, SWT.NONE); label.setText("Source Folder"); textSourceFolder = new Text(topLevel, SWT.BORDER); textSourceFolder.addKeyListener(this); btBrowseSourceFolder = new Button(topLevel, SWT.NONE); setLayout(label, textSourceFolder, btBrowseSourceFolder); btBrowseSourceFolder.addSelectionListener( new SelectionListener() { public void widgetSelected(SelectionEvent e) { try { PythonPackageSelectionDialog dialog = new PythonPackageSelectionDialog(getShell(), true); dialog.open(); Object firstResult = dialog.getFirstResult(); if (firstResult instanceof SourceFolder) { SourceFolder f = (SourceFolder) firstResult; textSourceFolder.setText(f.folder.getFullPath().toString()); } } catch (Exception e1) { Log.log(e1); } } public void widgetDefaultSelected(SelectionEvent e) {} }); Object element = selection.getFirstElement(); try { if (element instanceof IAdaptable) { IAdaptable adaptable = (IAdaptable) element; element = adaptable.getAdapter(IFile.class); if (element == null) { element = adaptable.getAdapter(IProject.class); } if (element == null) { element = adaptable.getAdapter(IFolder.class); } } if (element instanceof IFile) { IFile f = (IFile) element; element = f.getParent(); } if (element instanceof IProject) { IPythonPathNature nature = PythonNature.getPythonPathNature((IProject) element); if (nature != null) { String[] srcPaths = PythonNature.getStrAsStrItems(nature.getProjectSourcePath(true)); if (srcPaths.length > 0) { textSourceFolder.setText(srcPaths[0]); return true; } } } if (element instanceof IFolder) { IFolder f = (IFolder) element; String srcPath = getSrcFolderFromFolder(f); if (srcPath == null) { return true; } textSourceFolder.setText(srcPath); return true; } } catch (Exception e) { Log.log(e); } return false; }