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