/** @return if the paths maps to a valid python module (depending on its extension). */ public static boolean isValidFileMod(String path) { boolean ret = false; if (isValidSourceFile(path)) { ret = true; } else if (FileTypesPreferencesPage.isValidDll(path)) { ret = true; } return ret; }
/** * This method returns the module that corresponds to the path passed as a parameter. * * @param name the name of the module we're looking for (e.g.: mod1.mod2) * @param dontSearchInit is used in a negative form because initially it was isLookingForRelative, * but it actually defines if we should look in __init__ modules too, so, the name matches the * old signature. * <p>NOTE: isLookingForRelative description was: when looking for relative imports, we don't * check for __init__ * @return the module represented by this name */ protected IModule getModule( boolean acceptCompiledModule, String name, IPythonNature nature, boolean dontSearchInit) { synchronized (lockTemporaryModules) { SortedMap<Integer, IModule> map = temporaryModules.get(name); if (map != null && map.size() > 0) { if (DEBUG_TEMPORARY_MODULES) { System.out.println("Returning temporary module: " + name); } return map.get(map.lastKey()); } } AbstractModule n = null; ModulesKey keyForCacheAccess = new ModulesKey(null, null); if (!dontSearchInit) { if (n == null) { keyForCacheAccess.name = (String) StringUtils.join(".", new String[] {name, "__init__"}, null); n = cache.getObj(keyForCacheAccess, this); if (n != null) { name = keyForCacheAccess.name; } } } if (n == null) { keyForCacheAccess.name = name; n = cache.getObj(keyForCacheAccess, this); } if (n instanceof SourceModule) { // ok, module exists, let's check if it is synched with the filesystem version... SourceModule s = (SourceModule) n; if (!s.isSynched()) { // change it for an empty and proceed as usual. n = (AbstractModule) addModule(createModulesKey(s.getName(), s.getFile())); } } if (n instanceof EmptyModule) { EmptyModule e = (EmptyModule) n; if (e.f != null) { if (!e.f.exists()) { // if the file does not exist anymore, just remove it. keyForCacheAccess.name = name; keyForCacheAccess.file = e.f; doRemoveSingleModule(keyForCacheAccess); n = null; } else { // file exists n = checkOverride(name, nature, n); if (n instanceof EmptyModule) { // ok, handle case where the file is actually from a zip file... if (e instanceof EmptyModuleForZip) { EmptyModuleForZip emptyModuleForZip = (EmptyModuleForZip) e; if (emptyModuleForZip.pathInZip.endsWith(".class") || !emptyModuleForZip.isFile) { // handle java class... (if it's a class or a folder in a jar) n = JythonModulesManagerUtils.createModuleFromJar(emptyModuleForZip); n = decorateModule(n, nature); } else if (FileTypesPreferencesPage.isValidDll(emptyModuleForZip.pathInZip)) { // .pyd n = new CompiledModule(name, this); n = decorateModule(n, nature); } else if (PythonPathHelper.isValidSourceFile(emptyModuleForZip.pathInZip)) { // handle python file from zip... we have to create it getting the contents from the // zip file try { IDocument doc = FileUtilsFileBuffer.getDocFromZip( emptyModuleForZip.f, emptyModuleForZip.pathInZip); // NOTE: The nature (and so the grammar to be used) must be defined by this // modules // manager (and not by the initial caller)!! n = AbstractModule.createModuleFromDoc( name, emptyModuleForZip.f, doc, this.getNature(), false); SourceModule zipModule = (SourceModule) n; zipModule.zipFilePath = emptyModuleForZip.pathInZip; n = decorateModule(n, nature); } catch (Exception exc1) { Log.log(exc1); n = null; } } } else { // regular case... just go on and create it. try { // NOTE: The nature (and so the grammar to be used) must be defined by this modules // manager (and not by the initial caller)!! n = AbstractModule.createModule(name, e.f, this.getNature(), true); n = decorateModule(n, nature); } catch (IOException exc) { keyForCacheAccess.name = name; keyForCacheAccess.file = e.f; doRemoveSingleModule(keyForCacheAccess); n = null; } catch (MisconfigurationException exc) { Log.log(exc); n = null; } } } } } else { // ok, it does not have a file associated, so, we treat it as a builtin (this can // happen in java jars) n = checkOverride(name, nature, n); if (n instanceof EmptyModule) { if (acceptCompiledModule) { n = new CompiledModule(name, this); n = decorateModule(n, nature); } else { return null; } } } if (n != null) { doAddSingleModule(createModulesKey(name, e.f), n); } else { Log.log(("The module " + name + " could not be found nor created!")); } } if (n instanceof EmptyModule) { throw new RuntimeException("Should not be an empty module anymore: " + n); } if (n instanceof SourceModule) { SourceModule sourceModule = (SourceModule) n; // now, here's a catch... it may be a bootstrap module... if (sourceModule.isBootstrapModule()) { // if it's a bootstrap module, we must replace it for the related compiled module. n = new CompiledModule(name, this); n = decorateModule(n, nature); } } return n; }
/** * DAMN... when I started thinking this up, it seemed much better... (and easier) * * @param module - this is the full path of the module. Only for directories or py,pyd,dll,pyo * files. * @return a String with the module that the file or folder should represent. E.g.: compiler.ast */ public String resolveModule( String fullPath, final boolean requireFileToExist, List<String> pythonPathCopy) { fullPath = REF.getFileAbsolutePath(fullPath); fullPath = getDefaultPathStr(fullPath); String fullPathWithoutExtension; if (isValidSourceFile(fullPath) || FileTypesPreferencesPage.isValidDll(fullPath)) { fullPathWithoutExtension = FullRepIterable.headAndTail(fullPath)[0]; } else { fullPathWithoutExtension = fullPath; } final File moduleFile = new File(fullPath); if (requireFileToExist && !moduleFile.exists()) { return null; } boolean isFile = moduleFile.isFile(); // go through our pythonpath and check the beginning for (String pathEntry : pythonPathCopy) { String element = getDefaultPathStr(pathEntry); 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 (!isValidModuleLastPart(sWithoutExtension)) { continue; } s = s.replaceAll("/", "."); if (s.indexOf(".") != -1) { File root = new File(element); if (root.exists() == false) { continue; } final List<String> temp = StringUtils.dotSplit(s); String[] modulesParts = temp.toArray(new String[temp.size()]); // 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... 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 && 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) { // we have to remove the last part (.py, .pyc, .pyw) for (String element : pythonPathCopy) { element = getDefaultPathStr(element); if (fullPathWithoutExtension.startsWith(element)) { String s = fullPathWithoutExtension.substring(element.length()); if (s.startsWith("/")) { s = s.substring(1); } if (!isValidModuleLastPart(s)) { continue; } s = s.replaceAll("/", "."); return s; } } } return null; }