Skip to content

Commit

Permalink
Refactor structure to separate helper to compute what folder is part …
Browse files Browse the repository at this point in the history
…of the interpreter or project and fix test.
  • Loading branch information
fabioz committed Nov 10, 2024
1 parent 066ffff commit 01704ba
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public boolean forceDeselect(String data) {
return isRootPath(data, rootPaths);
}

public boolean exists(String data) {
public static boolean exists(String data) {
return new File(data).exists();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,45 @@ public int hashCode() {
return this.executableOrJar.hashCode();
}

public static class HelperToComputeIfPathIsInInterpreter {

Set<IPath> pythonHomePaths = new HashSet<>();
private DefaultPathsForInterpreterInfo defaultPaths;

public void setPythonExecutable(String infoExecutable) {
File pythonExecutableFile = new File(infoExecutable);
File pythonHome = pythonExecutableFile.getParentFile();
if (pythonHome != null) {
if (new File(pythonHome, "pyenv.cfg").exists()) {
pythonHome = pythonHome.getParentFile();
}
pythonHomePaths.add(Path.fromOSString(pythonHome.toString()));
}

}

public HelperToComputeIfPathIsInInterpreter() {
boolean resolvingInterpreter = true;
defaultPaths = new DefaultPathsForInterpreterInfo(
resolvingInterpreter);

}

public boolean isInterpreterPath(String data) {
if (!defaultPaths.forceDeselect(data)) { // It's directly a project source folder (don't add it).
// If it's site-package, in python home or not under a source folder add it.
if (data.contains("site-packages")
|| (DefaultPathsForInterpreterInfo
.isChildOfRootPath(data, pythonHomePaths))
|| defaultPaths.selectByDefault(data)) {
return true;
}
}
return false;
}

}

/**
*
* @param received
Expand Down Expand Up @@ -372,11 +411,7 @@ public static InterpreterInfo fromString(String received, boolean askUserInOutPa
List<String> predefinedPaths = new ArrayList<String>();
Properties stringSubstitutionVars = new Properties();

boolean resolvingInterpreter = true;
DefaultPathsForInterpreterInfo defaultPaths = new DefaultPathsForInterpreterInfo(
resolvingInterpreter);

Set<IPath> pythonHomePaths = new HashSet<>();
HelperToComputeIfPathIsInInterpreter helper = new HelperToComputeIfPathIsInInterpreter();

for (int j = 0; j < xmlNodes.getLength(); j++) {
Node xmlChild = xmlNodes.item(j);
Expand All @@ -390,14 +425,7 @@ public static InterpreterInfo fromString(String received, boolean askUserInOutPa

} else if ("executable".equals(name)) {
infoExecutable = data;

// Handling of python inside of project.
File pythonExecutableFile = new File(infoExecutable);
File pythonHome = pythonExecutableFile.getParentFile();
if (new File(pythonHome, "pyenv.cfg").exists()) {
pythonHome = pythonHome.getParentFile();
}
pythonHomePaths.add(Path.fromOSString(pythonHome.toString()));
helper.setPythonExecutable(infoExecutable);

} else if ("vmArgs".equals(name)) {
infoVmArgs = data;
Expand All @@ -413,7 +441,7 @@ public static InterpreterInfo fromString(String received, boolean askUserInOutPa
Node pathIncludeItem = attributes.getNamedItem("path");

if (pathIncludeItem != null) {
if (defaultPaths.exists(data)) {
if (DefaultPathsForInterpreterInfo.exists(data)) {
//The python backend is expected to put path='ins' or path='out'
//While our own toString() is not expected to do that.
//This is probably not a very good heuristic, but it maps the current state of affairs.
Expand All @@ -422,16 +450,9 @@ public static InterpreterInfo fromString(String received, boolean askUserInOutPa
toAsk.add(data);
}

if (!defaultPaths.forceDeselect(data)) { // It's directly a project source folder (don't add it).
// If it's site-package, in python home or not under a source folder add it.
if (data.contains("site-packages")
|| (DefaultPathsForInterpreterInfo
.isChildOfRootPath(data, pythonHomePaths))
|| defaultPaths.selectByDefault(data)) {
selection.add(data);
}
if (helper.isInterpreterPath(data)) {
selection.add(data);
}

}

} else {
Expand Down

0 comments on commit 01704ba

Please sign in to comment.