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