0001 import os 0002 import sys 0003 import imp 0004 0005 from TreeWidget import TreeItem 0006 from ClassBrowser import ClassBrowser, ModuleBrowserTreeItem 0007 0008 class PathBrowser(ClassBrowser): 0009 0010 def __init__(self, flist): 0011 self.init(flist) 0012 0013 def settitle(self): 0014 self.top.wm_title("Path Browser") 0015 self.top.wm_iconname("Path Browser") 0016 0017 def rootnode(self): 0018 return PathBrowserTreeItem() 0019 0020 class PathBrowserTreeItem(TreeItem): 0021 0022 def GetText(self): 0023 return "sys.path" 0024 0025 def GetSubList(self): 0026 sublist = [] 0027 for dir in sys.path: 0028 item = DirBrowserTreeItem(dir) 0029 sublist.append(item) 0030 return sublist 0031 0032 class DirBrowserTreeItem(TreeItem): 0033 0034 def __init__(self, dir, packages=[]): 0035 self.dir = dir 0036 self.packages = packages 0037 0038 def GetText(self): 0039 if not self.packages: 0040 return self.dir 0041 else: 0042 return self.packages[-1] + ": package" 0043 0044 def GetSubList(self): 0045 try: 0046 names = os.listdir(self.dir or os.curdir) 0047 except os.error: 0048 return [] 0049 packages = [] 0050 for name in names: 0051 file = os.path.join(self.dir, name) 0052 if self.ispackagedir(file): 0053 nn = os.path.normcase(name) 0054 packages.append((nn, name, file)) 0055 packages.sort() 0056 sublist = [] 0057 for nn, name, file in packages: 0058 item = DirBrowserTreeItem(file, self.packages + [name]) 0059 sublist.append(item) 0060 for nn, name in self.listmodules(names): 0061 item = ModuleBrowserTreeItem(os.path.join(self.dir, name)) 0062 sublist.append(item) 0063 return sublist 0064 0065 def ispackagedir(self, file): 0066 if not os.path.isdir(file): 0067 return 0 0068 init = os.path.join(file, "__init__.py") 0069 return os.path.exists(init) 0070 0071 def listmodules(self, allnames): 0072 modules = {} 0073 suffixes = imp.get_suffixes() 0074 sorted = [] 0075 for suff, mode, flag in suffixes: 0076 i = -len(suff) 0077 for name in allnames[:]: 0078 normed_name = os.path.normcase(name) 0079 if normed_name[i:] == suff: 0080 mod_name = name[:i] 0081 if not modules.has_key(mod_name): 0082 modules[mod_name] = None 0083 sorted.append((normed_name, name)) 0084 allnames.remove(name) 0085 sorted.sort() 0086 return sorted 0087 0088 def main(): 0089 import PyShell 0090 PathBrowser(PyShell.flist) 0091 if sys.stdin is sys.__stdin__: 0092 mainloop() 0093 0094 if __name__ == "__main__": 0095 main() 0096
Generated by PyXR 0.9.4