jpayne@68: import importlib.machinery jpayne@68: import os jpayne@68: import sys jpayne@68: jpayne@68: from idlelib.browser import ModuleBrowser, ModuleBrowserTreeItem jpayne@68: from idlelib.tree import TreeItem jpayne@68: jpayne@68: jpayne@68: class PathBrowser(ModuleBrowser): jpayne@68: jpayne@68: def __init__(self, master, *, _htest=False, _utest=False): jpayne@68: """ jpayne@68: _htest - bool, change box location when running htest jpayne@68: """ jpayne@68: self.master = master jpayne@68: self._htest = _htest jpayne@68: self._utest = _utest jpayne@68: self.init() jpayne@68: jpayne@68: def settitle(self): jpayne@68: "Set window titles." jpayne@68: self.top.wm_title("Path Browser") jpayne@68: self.top.wm_iconname("Path Browser") jpayne@68: jpayne@68: def rootnode(self): jpayne@68: return PathBrowserTreeItem() jpayne@68: jpayne@68: jpayne@68: class PathBrowserTreeItem(TreeItem): jpayne@68: jpayne@68: def GetText(self): jpayne@68: return "sys.path" jpayne@68: jpayne@68: def GetSubList(self): jpayne@68: sublist = [] jpayne@68: for dir in sys.path: jpayne@68: item = DirBrowserTreeItem(dir) jpayne@68: sublist.append(item) jpayne@68: return sublist jpayne@68: jpayne@68: jpayne@68: class DirBrowserTreeItem(TreeItem): jpayne@68: jpayne@68: def __init__(self, dir, packages=[]): jpayne@68: self.dir = dir jpayne@68: self.packages = packages jpayne@68: jpayne@68: def GetText(self): jpayne@68: if not self.packages: jpayne@68: return self.dir jpayne@68: else: jpayne@68: return self.packages[-1] + ": package" jpayne@68: jpayne@68: def GetSubList(self): jpayne@68: try: jpayne@68: names = os.listdir(self.dir or os.curdir) jpayne@68: except OSError: jpayne@68: return [] jpayne@68: packages = [] jpayne@68: for name in names: jpayne@68: file = os.path.join(self.dir, name) jpayne@68: if self.ispackagedir(file): jpayne@68: nn = os.path.normcase(name) jpayne@68: packages.append((nn, name, file)) jpayne@68: packages.sort() jpayne@68: sublist = [] jpayne@68: for nn, name, file in packages: jpayne@68: item = DirBrowserTreeItem(file, self.packages + [name]) jpayne@68: sublist.append(item) jpayne@68: for nn, name in self.listmodules(names): jpayne@68: item = ModuleBrowserTreeItem(os.path.join(self.dir, name)) jpayne@68: sublist.append(item) jpayne@68: return sublist jpayne@68: jpayne@68: def ispackagedir(self, file): jpayne@68: " Return true for directories that are packages." jpayne@68: if not os.path.isdir(file): jpayne@68: return False jpayne@68: init = os.path.join(file, "__init__.py") jpayne@68: return os.path.exists(init) jpayne@68: jpayne@68: def listmodules(self, allnames): jpayne@68: modules = {} jpayne@68: suffixes = importlib.machinery.EXTENSION_SUFFIXES[:] jpayne@68: suffixes += importlib.machinery.SOURCE_SUFFIXES jpayne@68: suffixes += importlib.machinery.BYTECODE_SUFFIXES jpayne@68: sorted = [] jpayne@68: for suff in suffixes: jpayne@68: i = -len(suff) jpayne@68: for name in allnames[:]: jpayne@68: normed_name = os.path.normcase(name) jpayne@68: if normed_name[i:] == suff: jpayne@68: mod_name = name[:i] jpayne@68: if mod_name not in modules: jpayne@68: modules[mod_name] = None jpayne@68: sorted.append((normed_name, name)) jpayne@68: allnames.remove(name) jpayne@68: sorted.sort() jpayne@68: return sorted jpayne@68: jpayne@68: jpayne@68: def _path_browser(parent): # htest # jpayne@68: PathBrowser(parent, _htest=True) jpayne@68: parent.mainloop() jpayne@68: jpayne@68: if __name__ == "__main__": jpayne@68: from unittest import main jpayne@68: main('idlelib.idle_test.test_pathbrowser', verbosity=2, exit=False) jpayne@68: jpayne@68: from idlelib.idle_test.htest import run jpayne@68: run(_path_browser)