annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/idlelib/pathbrowser.py @ 69:33d812a61356

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