1 | n/a | import importlib.machinery |
---|
2 | n/a | import os |
---|
3 | n/a | import sys |
---|
4 | n/a | |
---|
5 | n/a | from idlelib.browser import ClassBrowser, ModuleBrowserTreeItem |
---|
6 | n/a | from idlelib.pyshell import PyShellFileList |
---|
7 | n/a | from idlelib.tree import TreeItem |
---|
8 | n/a | |
---|
9 | n/a | |
---|
10 | n/a | class PathBrowser(ClassBrowser): |
---|
11 | n/a | |
---|
12 | n/a | def __init__(self, flist, _htest=False): |
---|
13 | n/a | """ |
---|
14 | n/a | _htest - bool, change box location when running htest |
---|
15 | n/a | """ |
---|
16 | n/a | self._htest = _htest |
---|
17 | n/a | self.init(flist) |
---|
18 | n/a | |
---|
19 | n/a | def settitle(self): |
---|
20 | n/a | "Set window titles." |
---|
21 | n/a | self.top.wm_title("Path Browser") |
---|
22 | n/a | self.top.wm_iconname("Path Browser") |
---|
23 | n/a | |
---|
24 | n/a | def rootnode(self): |
---|
25 | n/a | return PathBrowserTreeItem() |
---|
26 | n/a | |
---|
27 | n/a | |
---|
28 | n/a | class PathBrowserTreeItem(TreeItem): |
---|
29 | n/a | |
---|
30 | n/a | def GetText(self): |
---|
31 | n/a | return "sys.path" |
---|
32 | n/a | |
---|
33 | n/a | def GetSubList(self): |
---|
34 | n/a | sublist = [] |
---|
35 | n/a | for dir in sys.path: |
---|
36 | n/a | item = DirBrowserTreeItem(dir) |
---|
37 | n/a | sublist.append(item) |
---|
38 | n/a | return sublist |
---|
39 | n/a | |
---|
40 | n/a | |
---|
41 | n/a | class DirBrowserTreeItem(TreeItem): |
---|
42 | n/a | |
---|
43 | n/a | def __init__(self, dir, packages=[]): |
---|
44 | n/a | self.dir = dir |
---|
45 | n/a | self.packages = packages |
---|
46 | n/a | |
---|
47 | n/a | def GetText(self): |
---|
48 | n/a | if not self.packages: |
---|
49 | n/a | return self.dir |
---|
50 | n/a | else: |
---|
51 | n/a | return self.packages[-1] + ": package" |
---|
52 | n/a | |
---|
53 | n/a | def GetSubList(self): |
---|
54 | n/a | try: |
---|
55 | n/a | names = os.listdir(self.dir or os.curdir) |
---|
56 | n/a | except OSError: |
---|
57 | n/a | return [] |
---|
58 | n/a | packages = [] |
---|
59 | n/a | for name in names: |
---|
60 | n/a | file = os.path.join(self.dir, name) |
---|
61 | n/a | if self.ispackagedir(file): |
---|
62 | n/a | nn = os.path.normcase(name) |
---|
63 | n/a | packages.append((nn, name, file)) |
---|
64 | n/a | packages.sort() |
---|
65 | n/a | sublist = [] |
---|
66 | n/a | for nn, name, file in packages: |
---|
67 | n/a | item = DirBrowserTreeItem(file, self.packages + [name]) |
---|
68 | n/a | sublist.append(item) |
---|
69 | n/a | for nn, name in self.listmodules(names): |
---|
70 | n/a | item = ModuleBrowserTreeItem(os.path.join(self.dir, name)) |
---|
71 | n/a | sublist.append(item) |
---|
72 | n/a | return sublist |
---|
73 | n/a | |
---|
74 | n/a | def ispackagedir(self, file): |
---|
75 | n/a | " Return true for directories that are packages." |
---|
76 | n/a | if not os.path.isdir(file): |
---|
77 | n/a | return False |
---|
78 | n/a | init = os.path.join(file, "__init__.py") |
---|
79 | n/a | return os.path.exists(init) |
---|
80 | n/a | |
---|
81 | n/a | def listmodules(self, allnames): |
---|
82 | n/a | modules = {} |
---|
83 | n/a | suffixes = importlib.machinery.EXTENSION_SUFFIXES[:] |
---|
84 | n/a | suffixes += importlib.machinery.SOURCE_SUFFIXES |
---|
85 | n/a | suffixes += importlib.machinery.BYTECODE_SUFFIXES |
---|
86 | n/a | sorted = [] |
---|
87 | n/a | for suff in suffixes: |
---|
88 | n/a | i = -len(suff) |
---|
89 | n/a | for name in allnames[:]: |
---|
90 | n/a | normed_name = os.path.normcase(name) |
---|
91 | n/a | if normed_name[i:] == suff: |
---|
92 | n/a | mod_name = name[:i] |
---|
93 | n/a | if mod_name not in modules: |
---|
94 | n/a | modules[mod_name] = None |
---|
95 | n/a | sorted.append((normed_name, name)) |
---|
96 | n/a | allnames.remove(name) |
---|
97 | n/a | sorted.sort() |
---|
98 | n/a | return sorted |
---|
99 | n/a | |
---|
100 | n/a | |
---|
101 | n/a | def _path_browser(parent): # htest # |
---|
102 | n/a | flist = PyShellFileList(parent) |
---|
103 | n/a | PathBrowser(flist, _htest=True) |
---|
104 | n/a | parent.mainloop() |
---|
105 | n/a | |
---|
106 | n/a | if __name__ == "__main__": |
---|
107 | n/a | from unittest import main |
---|
108 | n/a | main('idlelib.idle_test.test_pathbrowser', verbosity=2, exit=False) |
---|
109 | n/a | |
---|
110 | n/a | from idlelib.idle_test.htest import run |
---|
111 | n/a | run(_path_browser) |
---|