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