Coverage for pathbrowser.py: 82%

84 statements  

« prev     ^ index     » next       coverage.py v7.2.5, created at 2023-05-11 13:22 -0700

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" 1c

33 

34 def GetSubList(self): 

35 sublist = [] 1c

36 for dir in sys.path: 1c

37 item = DirBrowserTreeItem(dir) 1c

38 sublist.append(item) 1c

39 return sublist 1c

40 

41 

42class DirBrowserTreeItem(TreeItem): 

43 

44 def __init__(self, dir, packages=[]): 

45 self.dir = dir 1ac

46 self.packages = packages 1ac

47 

48 def GetText(self): 

49 if not self.packages: 49 ↛ 52line 49 didn't jump to line 52, because the condition on line 49 was never false1a

50 return self.dir 1a

51 else: 

52 return self.packages[-1] + ": package" 

53 

54 def GetSubList(self): 

55 try: 1a

56 names = os.listdir(self.dir or os.curdir) 1a

57 except OSError: 

58 return [] 

59 packages = [] 1a

60 for name in names: 1a

61 file = os.path.join(self.dir, name) 1a

62 if self.ispackagedir(file): 1a

63 nn = os.path.normcase(name) 1a

64 packages.append((nn, name, file)) 1a

65 packages.sort() 1a

66 sublist = [] 1a

67 for nn, name, file in packages: 1a

68 item = DirBrowserTreeItem(file, self.packages + [name]) 1a

69 sublist.append(item) 1a

70 for nn, name in self.listmodules(names): 1a

71 item = ModuleBrowserTreeItem(os.path.join(self.dir, name)) 1a

72 sublist.append(item) 1a

73 return sublist 1a

74 

75 def ispackagedir(self, file): 

76 " Return true for directories that are packages." 

77 if not os.path.isdir(file): 1a

78 return False 1a

79 init = os.path.join(file, "__init__.py") 1a

80 return os.path.exists(init) 1a

81 

82 def listmodules(self, allnames): 

83 modules = {} 1a

84 suffixes = importlib.machinery.EXTENSION_SUFFIXES[:] 1a

85 suffixes += importlib.machinery.SOURCE_SUFFIXES 1a

86 suffixes += importlib.machinery.BYTECODE_SUFFIXES 1a

87 sorted = [] 1a

88 for suff in suffixes: 1a

89 i = -len(suff) 1a

90 for name in allnames[:]: 1a

91 normed_name = os.path.normcase(name) 1a

92 if normed_name[i:] == suff: 1a

93 mod_name = name[:i] 1a

94 if mod_name not in modules: 94 ↛ 90line 94 didn't jump to line 90, because the condition on line 94 was never false1a

95 modules[mod_name] = None 1a

96 sorted.append((normed_name, name)) 1a

97 allnames.remove(name) 1a

98 sorted.sort() 1a

99 return sorted 1a

100 

101 

102def _path_browser(parent): # htest # 

103 PathBrowser(parent, _htest=True) 

104 parent.mainloop() 

105 

106if __name__ == "__main__": 106 ↛ 107line 106 didn't jump to line 107, because the condition on line 106 was never true

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)