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