public Tuple<IModule, String> findOnImportedMods(IToken importedModule, String tok, ICompletionState state,
String activationToken, String currentModuleName, IModule current) throws CompletionRecursionException,
MisconfigurationException {
Tuple<IModule, String> modTok = null;
IModule mod = null;
//ok, check if it is a token for the new import
IPythonNature nature = state.getNature();
if (importedModule instanceof SourceToken) {
SourceToken token = (SourceToken) importedModule;
if (token.isImportFrom()) {
ImportFrom importFrom = (ImportFrom) token.getAst();
int level = importFrom.level;
if (level > 0) {
//ok, it must be treated as a relative import
//ok, it is the import added on python 2.5 (from .. import xxx)
String parentPackage = token.getParentPackage();
List<String> moduleParts = StringUtils.dotSplit(parentPackage);
String relative = null;
if (moduleParts.size() > level) {
relative = FullRepIterable.joinParts(moduleParts, moduleParts.size() - level);
}
String modName = ((NameTok) importFrom.module).id;
if (modName.length() > 0) {
//ok, we have to add the other part too, as we have more than the leading dots
//from ..bar import
relative += "." + modName;
}
if (!AbstractVisitor.isWildImport(importFrom)) {
tok = FullRepIterable.getLastPart(token.originalRep);
relative += "." + tok;
}
modTok = findModuleFromPath(relative, nature, false, null);
mod = modTok.o1;
if (checkValidity(currentModuleName, mod)) {
Tuple<IModule, String> ret = fixTok(modTok, tok, activationToken);
return ret;
}
//ok, it is 'forced' as relative import because it has a level, so, it MUST return here
return null;
}
}
}
boolean isAbsoluteImportEnabledx = this.isAbsoluteImportEnabled(current, nature);
String asRelativeImport = "";
if (!isAbsoluteImportEnabledx) {
//check as relative with complete rep
asRelativeImport = importedModule.getAsRelativeImport(currentModuleName);
if (!asRelativeImport.startsWith(".")) {
modTok = findModuleFromPath(asRelativeImport, nature, true, currentModuleName);
mod = modTok.o1;
if (checkValidity(currentModuleName, mod)) {
Tuple<IModule, String> ret = fixTok(modTok, tok, activationToken);
return ret;
}
}
}
//check if the import actually represents some token in an __init__ file
String originalWithoutRep = importedModule.getOriginalWithoutRep();
if (originalWithoutRep.length() > 0) {
if (!originalWithoutRep.endsWith("__init__")) {
originalWithoutRep = originalWithoutRep + ".__init__";
}
modTok = findModuleFromPath(originalWithoutRep, nature, true, null);
mod = modTok.o1;
if (modTok.o2.endsWith("__init__") == false && checkValidity(currentModuleName, mod)) {
if (mod.isInGlobalTokens(importedModule.getRepresentation(), nature, false, state)) {
//then this is the token we're looking for (otherwise, it might be a module).
Tuple<IModule, String> ret = fixTok(modTok, tok, activationToken);
if (ret.o2.length() == 0) {
ret.o2 = importedModule.getRepresentation();
} else {
ret.o2 = importedModule.getRepresentation() + "." + ret.o2;
}
return ret;
}
}
}
//the most 'simple' case: check as absolute with original rep
modTok = findModuleFromPath(importedModule.getOriginalRep(), nature, false, null);
mod = modTok.o1;
if (checkValidity(currentModuleName, mod)) {
Tuple<IModule, String> ret = fixTok(modTok, tok, activationToken);
return ret;
}
if (!isAbsoluteImportEnabledx) {
//ok, one last shot, to see a relative looking in folders __init__
modTok = findModuleFromPath(asRelativeImport, nature, false, null);
mod = modTok.o1;
if (checkValidity(currentModuleName, mod, true)) {
Tuple<IModule, String> ret = fixTok(modTok, tok, activationToken);
//now let's see if what we did when we found it as a relative import is correct:
//if we didn't find it in an __init__ module, all should be ok
if (!mod.getName().endsWith("__init__")) {
return ret;
}
//otherwise, we have to be more cautious...
//if the activation token is empty, then it is the module we were looking for
//if it is not the initial token we were looking for, it is correct
//if it is in the global tokens of the found module it is correct
//if none of this situations was found, we probably just found the same token we had when we started (unless I'm mistaken...)
else if (activationToken.length() == 0 || ret.o2.equals(activationToken) == false
|| mod.isInGlobalTokens(activationToken, nature, false, state)) {
return ret;
}
}
}