ยปCore Development>Code coverage>Lib/idlelib/pathbrowser.py

Python code coverage for Lib/idlelib/pathbrowser.py

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