Search in sources :

Example 6 with IPythonPathNature

use of org.python.pydev.core.IPythonPathNature in project Pydev by fabioz.

the class PythonExistingSourceFolderWizard method doCreateNew.

@Override
protected IFile doCreateNew(IProgressMonitor monitor) throws CoreException {
    IProject project = filePage.getValidatedProject();
    String name = filePage.getValidatedName();
    IPath source = filePage.getSourceToLink();
    if (project == null || !project.exists()) {
        throw new RuntimeException("The project selected does not exist in the workspace.");
    }
    IPythonPathNature pathNature = PythonNature.getPythonPathNature(project);
    if (pathNature == null) {
        IPythonNature nature = PythonNature.addNature(project, monitor, null, null, null, null, null);
        pathNature = nature.getPythonPathNature();
        if (pathNature == null) {
            throw new RuntimeException("Unable to add the nature to the seleted project.");
        }
    }
    if (source == null || !source.toFile().exists()) {
        throw new RuntimeException("The source to link to, " + source.toString() + ", does not exist.");
    }
    IFolder folder = project.getFolder(name);
    if (!folder.exists()) {
        folder.createLink(source, IResource.BACKGROUND_REFRESH, monitor);
    }
    String newPath = folder.getFullPath().toString();
    String curr = pathNature.getProjectSourcePath(true);
    if (curr == null) {
        curr = "";
    }
    if (curr.endsWith("|")) {
        curr = curr.substring(0, curr.length() - 1);
    }
    String newPathRel = PyStructureConfigHelpers.convertToProjectRelativePath(project.getFullPath().toString(), newPath);
    if (curr.length() > 0) {
        // there is already some path
        Set<String> projectSourcePathSet = pathNature.getProjectSourcePathSet(true);
        if (!projectSourcePathSet.contains(newPath)) {
            // only add to the path if it doesn't already contain the new path
            curr += "|" + newPathRel;
        }
    } else {
        // there is still no other path
        curr = newPathRel;
    }
    pathNature.setProjectSourcePath(curr);
    PythonNature.getPythonNature(project).rebuildPath();
    return null;
}
Also used : IPath(org.eclipse.core.runtime.IPath) IPythonPathNature(org.python.pydev.core.IPythonPathNature) IPythonNature(org.python.pydev.core.IPythonNature) IProject(org.eclipse.core.resources.IProject) IFolder(org.eclipse.core.resources.IFolder)

Example 7 with IPythonPathNature

use of org.python.pydev.core.IPythonPathNature in project Pydev by fabioz.

the class PythonSourceFolderWizard method doCreateNew.

@Override
protected IFile doCreateNew(IProgressMonitor monitor) throws CoreException {
    IProject project = filePage.getValidatedProject();
    String name = filePage.getValidatedName();
    if (project == null || !project.exists()) {
        throw new RuntimeException("The project selected does not exist in the workspace.");
    }
    IPythonPathNature pathNature = PythonNature.getPythonPathNature(project);
    if (pathNature == null) {
        IPythonNature nature = PythonNature.addNature(project, monitor, null, null, null, null, null);
        pathNature = nature.getPythonPathNature();
        if (pathNature == null) {
            throw new RuntimeException("Unable to add the nature to the seleted project.");
        }
    }
    IFolder folder = project.getFolder(name);
    if (folder.exists()) {
        Log.log("Source folder already exists. Nothing new was created");
        return null;
    }
    folder.create(true, true, monitor);
    String newPath = folder.getFullPath().toString();
    String curr = pathNature.getProjectSourcePath(false);
    if (curr == null) {
        curr = "";
    }
    if (curr.endsWith("|")) {
        curr = curr.substring(0, curr.length() - 1);
    }
    String newPathRel = PyStructureConfigHelpers.convertToProjectRelativePath(project.getFullPath().toString(), newPath);
    if (curr.length() > 0) {
        // there is already some path
        Set<String> projectSourcePathSet = pathNature.getProjectSourcePathSet(true);
        if (!projectSourcePathSet.contains(newPath)) {
            // only add to the path if it doesn't already contain the new path
            curr += "|" + newPathRel;
        }
    } else {
        // there is still no other path
        curr = newPathRel;
    }
    pathNature.setProjectSourcePath(curr);
    PythonNature.getPythonNature(project).rebuildPath();
    return null;
}
Also used : IPythonPathNature(org.python.pydev.core.IPythonPathNature) IPythonNature(org.python.pydev.core.IPythonNature) IProject(org.eclipse.core.resources.IProject) IFolder(org.eclipse.core.resources.IFolder)

Example 8 with IPythonPathNature

use of org.python.pydev.core.IPythonPathNature in project Pydev by fabioz.

the class PyProjectProperties method doIt.

/**
 * Save the pythonpath - only updates model if asked to.
 * @return
 */
private boolean doIt(boolean force) {
    if (project != null) {
        try {
            boolean changed = false;
            IPythonPathNature pythonPathNature = PythonNature.getPythonPathNature(project);
            String sourcePath = pythonPathNature.getProjectSourcePath(false);
            String externalSourcePath = pythonPathNature.getProjectExternalSourcePath(false);
            Map<String, String> variableSubstitution = pythonPathNature.getVariableSubstitution(false);
            String newSourcePath = StringUtils.leftAndRightTrim(treeSourceFolders.getTreeItemsAsStr(), '|');
            String newExternalSourcePath = StringUtils.leftAndRightTrim(treeExternalLibs.getTreeItemsAsStr(), '|');
            Map<String, String> newVariableSubstitution = tabVariables.getTreeItemsAsMap();
            if (checkIfShouldBeSet(sourcePath, newSourcePath)) {
                pythonPathNature.setProjectSourcePath(newSourcePath);
                changed = true;
            }
            if (checkIfShouldBeSet(externalSourcePath, newExternalSourcePath)) {
                pythonPathNature.setProjectExternalSourcePath(newExternalSourcePath);
                changed = true;
            }
            if (checkIfShouldBeSet(variableSubstitution, newVariableSubstitution)) {
                pythonPathNature.setVariableSubstitution(newVariableSubstitution);
                changed = true;
            }
            PythonNature pythonNature = PythonNature.getPythonNature(project);
            if (pythonNature != null && (changed || force || pythonNature.getAstManager() == null)) {
                pythonNature.rebuildPath();
            }
        } catch (Exception e) {
            Log.log(IStatus.ERROR, "Unexpected error setting project properties", e);
        }
    }
    return true;
}
Also used : PythonNature(org.python.pydev.plugin.nature.PythonNature) IPythonPathNature(org.python.pydev.core.IPythonPathNature)

Example 9 with IPythonPathNature

use of org.python.pydev.core.IPythonPathNature in project Pydev by fabioz.

the class PyRemSrcFolder method doActionOnContainer.

@Override
protected int doActionOnContainer(IContainer container, IProgressMonitor monitor) {
    try {
        IProject project = container.getProject();
        IPythonPathNature pythonPathNature = PythonNature.getPythonPathNature(project);
        if (pythonPathNature == null) {
            Log.log("Unable to get PythonNature on project: " + project);
            return 0;
        }
        OrderedMap<String, String> projectSourcePathMap = pythonPathNature.getProjectSourcePathResolvedToUnresolvedMap();
        String pathToRemove = container.getFullPath().toString();
        if (projectSourcePathMap.remove(pathToRemove) == null) {
            return 0;
        }
        // Set back the map with the variables, not the one with resolved vars.
        pythonPathNature.setProjectSourcePath(StringUtils.join("|", projectSourcePathMap.values()));
        PythonNature.getPythonNature(project).rebuildPath();
        return 1;
    } catch (CoreException e) {
        Log.log(IStatus.ERROR, "Unexpected error setting project properties", e);
    }
    return 0;
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) IPythonPathNature(org.python.pydev.core.IPythonPathNature) IProject(org.eclipse.core.resources.IProject)

Example 10 with IPythonPathNature

use of org.python.pydev.core.IPythonPathNature in project Pydev by fabioz.

the class ProjectModulesManager method getCompletePythonPath.

/**
 * @see org.python.pydev.core.IProjectModulesManager#getCompletePythonPath()
 */
@Override
public List<String> getCompletePythonPath(IInterpreterInfo interpreter, IInterpreterManager manager) {
    List<String> l = new ArrayList<String>();
    IModulesManager[] managersInvolved = getManagersInvolved(true);
    for (IModulesManager m : managersInvolved) {
        if (m instanceof ISystemModulesManager) {
            ISystemModulesManager systemModulesManager = (ISystemModulesManager) m;
            l.addAll(systemModulesManager.getCompletePythonPath(interpreter, manager));
        } else {
            PythonPathHelper h = (PythonPathHelper) m.getPythonPathHelper();
            if (h != null) {
                List<String> pythonpath = h.getPythonpath();
                // Note: this was previously only l.addAll(pythonpath), and was changed to the code below as a place
                // to check for consistencies in the pythonpath stored in the pythonpath helper and the pythonpath
                // available in the PythonPathNature (in general, when requesting it the PythonPathHelper should be
                // used, as it's a cache for the resolved values of the PythonPathNature).
                boolean forceCheck = false;
                ProjectModulesManager m2 = null;
                String onlyProjectPythonPathStr = null;
                if (m instanceof ProjectModulesManager) {
                    long currentTimeMillis = System.currentTimeMillis();
                    m2 = (ProjectModulesManager) m;
                    // it should be fast to get it too if it's consistent).
                    if (pythonpath.size() == 0 || currentTimeMillis - m2.checkedPythonpathConsistency > 20 * 1000) {
                        try {
                            IPythonNature n = m.getNature();
                            if (n != null) {
                                IPythonPathNature pythonPathNature = n.getPythonPathNature();
                                if (pythonPathNature != null) {
                                    onlyProjectPythonPathStr = pythonPathNature.getOnlyProjectPythonPathStr(true);
                                    m2.checkedPythonpathConsistency = currentTimeMillis;
                                    forceCheck = true;
                                }
                            }
                        } catch (Exception e) {
                            Log.log(e);
                        }
                    }
                }
                if (forceCheck) {
                    // Check if it's actually correct and auto-fix if it's not.
                    List<String> parsed = PythonPathHelper.parsePythonPathFromStr(onlyProjectPythonPathStr, null);
                    if (m2.nature != null && !new HashSet<String>(parsed).equals(new HashSet<String>(pythonpath))) {
                        // Make it right at this moment (so any other place that calls it before the restore
                        // takes place has the proper version).
                        h.setPythonPath(parsed);
                        // Force a rebuild as the PythonPathHelper paths are not up to date.
                        m2.nature.rebuildPath();
                    }
                    // add the proper paths
                    l.addAll(parsed);
                } else {
                    l.addAll(pythonpath);
                }
            }
        }
    }
    return l;
}
Also used : ArrayList(java.util.ArrayList) IPythonNature(org.python.pydev.core.IPythonNature) IPythonPathNature(org.python.pydev.core.IPythonPathNature) IModulesManager(org.python.pydev.core.IModulesManager) IProjectModulesManager(org.python.pydev.core.IProjectModulesManager) CoreException(org.eclipse.core.runtime.CoreException) ISystemModulesManager(org.python.pydev.core.ISystemModulesManager)

Aggregations

IPythonPathNature (org.python.pydev.core.IPythonPathNature)26 IProject (org.eclipse.core.resources.IProject)15 CoreException (org.eclipse.core.runtime.CoreException)10 PythonNature (org.python.pydev.plugin.nature.PythonNature)9 IPythonNature (org.python.pydev.core.IPythonNature)7 ArrayList (java.util.ArrayList)5 IFile (org.eclipse.core.resources.IFile)5 IFolder (org.eclipse.core.resources.IFolder)5 IResource (org.eclipse.core.resources.IResource)5 Path (org.eclipse.core.runtime.Path)5 File (java.io.File)3 IContainer (org.eclipse.core.resources.IContainer)3 IPath (org.eclipse.core.runtime.IPath)3 Composite (org.eclipse.swt.widgets.Composite)3 Label (org.eclipse.swt.widgets.Label)3 HashMap (java.util.HashMap)2 IWorkspaceRoot (org.eclipse.core.resources.IWorkspaceRoot)2 GridData (org.eclipse.swt.layout.GridData)2 GridLayout (org.eclipse.swt.layout.GridLayout)2 Shell (org.eclipse.swt.widgets.Shell)2