📄 pythonpathhelper.java
字号:
public String resolveModule(String fullPath, final boolean requireFileToExist){
fullPath = REF.getFileAbsolutePath(fullPath);
fullPath = getDefaultPathStr(fullPath);
String fullPathWithoutExtension;
if(isValidSourceFile(fullPath) || isValidDll(fullPath)){
fullPathWithoutExtension = FullRepIterable.headAndTail(fullPath)[0];
}else{
fullPathWithoutExtension = fullPath;
}
final File moduleFile = new File(fullPath);
if(requireFileToExist){
if(moduleFile.exists() == false){
return null;
}
}
boolean isFile = moduleFile.isFile();
synchronized (pythonpath) {
//go through our pythonpath and check the beggining
for (Iterator<String> iter = pythonpath.iterator(); iter.hasNext();) {
String element = getDefaultPathStr(iter.next());
if(fullPath.startsWith(element)){
int len = element.length();
String s = fullPath.substring(len);
String sWithoutExtension = fullPathWithoutExtension.substring(len);
if(s.startsWith("/")){
s = s.substring(1);
}
if(sWithoutExtension.startsWith("/")){
sWithoutExtension = sWithoutExtension.substring(1);
}
if(!isValidModule(sWithoutExtension)){
continue;
}
s = s.replaceAll("/",".");
if(s.indexOf(".") != -1){
File root = new File(element);
if(root.exists() == false){
continue;
}
//this means that more than 1 module is specified, so, in order to get it,
//we have to go and see if all the folders to that module have __init__.py in it...
String[] modulesParts = FullRepIterable.dotSplit(s);
if(modulesParts.length > 1 && isFile){
String[] t = new String[modulesParts.length -1];
for (int i = 0; i < modulesParts.length-1; i++) {
t[i] = modulesParts[i];
}
t[t.length -1] = t[t.length -1]+"."+modulesParts[modulesParts.length-1];
modulesParts = t;
}
//here, in modulesParts, we have something like
//["compiler", "ast.py"] - if file
//["pywin","debugger"] - if folder
//
//root starts with the pythonpath folder that starts with the same
//chars as the full path passed in.
boolean isValid = true;
for (int i = 0; i < modulesParts.length && root != null; i++) {
root = new File(REF.getFileAbsolutePath(root) + "/" + modulesParts[i]);
//check if file is in root...
if(isValidFileMod(modulesParts[i])){
if(root.exists() && root.isFile()){
break;
}
}else{
//this part is a folder part... check if it is a valid module (has init).
if(isFileOrFolderWithInit(root) == false){
isValid = false;
break;
}
//go on and check the next part.
}
}
if(isValid){
if(isFile){
s = stripExtension(s);
}else if(moduleFile.exists() == false){
//ok, it does not exist, so isFile will not work, let's just check if it is
//a valid module (ends with .py or .pyw) and if it is, strip the extension
if(isValidFileMod(s)){
s = stripExtension(s);
}
}
return s;
}
}else{
//simple part, we don't have to go into subfolders to check validity...
if(isFile){
throw new RuntimeException("This should never happen... if it is a file, it always has a dot, so, this should not happen...");
}else if (moduleFile.isDirectory() && isFileOrFolderWithInit(moduleFile) == false){
return null;
}
return s;
}
}
}
//ok, it was not found in any existing way, so, if we don't require the file to exist, let's just do some simpler search and get the
//first match (if any)... this is useful if the file we are looking for has just been deleted
if(requireFileToExist == false){
//we have to remove the last part (.py, .pyc, .pyw)
for (String element : pythonpath) {
element = getDefaultPathStr(element);
if(fullPathWithoutExtension.startsWith(element)){
String s = fullPathWithoutExtension.substring(element.length());
if(s.startsWith("/")){
s = s.substring(1);
}
if(!isValidModule(s)){
continue;
}
s = s.replaceAll("/",".");
return s;
}
}
}
return null;
}
}
/**
* Note that this function is not completely safe...beware when using it.
* @param s
* @return
*/
public static String stripExtension(String s) {
if(s != null){
String[] strings = s.split("\\.");
return s.substring(0, s.length() - strings[strings.length -1].length() -1);
}
return null;
}
/**
* @param root this is the folder we're checking
* @return true if it is a folder with an __init__ python file
*/
private boolean isFileOrFolderWithInit(File root) {
//check for an __init__ in a dir (we do not check if it is a file, becase if it is, it should return null)
String[] items = root.list(new FilenameFilter(){
public boolean accept(File dir, String name) {
if(isValidInitFile(name)){
return true;
}
return false;
}
});
if(items == null || items.length < 1){
return false;
}
return true;
}
/**
* @param item the file we want to check
* @return true if the file is a valid __init__ file
*/
public static boolean isValidInitFile(String item) {
return item.toLowerCase().indexOf("__init__.") != -1 && isValidSourceFile(item);
}
/**
* @param s
* @return
*/
private boolean isValidModule(String s) {
return s.indexOf("-") == -1 && s.indexOf(" ") == -1 && s.indexOf(".") == -1;
}
/**
* @param string with paths separated by |
* @return
*/
public List<String> setPythonPath(String string) {
synchronized (pythonpath) {
pythonpath.clear();
getPythonPathFromStr(string, pythonpath);
return new ArrayList<String>(pythonpath);
}
}
/**
* @param string this is the string that has the pythonpath (separated by |)
* @param lPath OUT: this list is filled with the pythonpath.
*/
public void getPythonPathFromStr(String string, List<String> lPath) {
String[] strings = string.split("\\|");
for (int i = 0; i < strings.length; i++) {
String defaultPathStr = getDefaultPathStr(strings[i]);
if(defaultPathStr != null && defaultPathStr.trim().length() > 0){
File file = new File(defaultPathStr);
if(file.exists()){
//we have to get it with the appropriate cases and in a canonical form
String path = REF.getFileAbsolutePath(file);
lPath.add(path);
}
}
}
}
public List<String> getPythonpath() {
return new ArrayList<String>(this.pythonpath);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -