pathbrowser.py 3.1 KB

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