<s> import os <EOL> import sys <EOL> import imp <EOL> from idlelib . TreeWidget import TreeItem <EOL> from idlelib . ClassBrowser import ClassBrowser , ModuleBrowserTreeItem <EOL> from idlelib . PyShell import PyShellFileList <EOL> class PathBrowser ( ClassBrowser ) : <EOL> def __init__ ( self , flist , _htest = False ) : <EOL> """<STR_LIT>""" <EOL> self . _htest = _htest <EOL> self . init ( flist ) <EOL> def settitle ( self ) : <EOL> self . top . wm_title ( "<STR_LIT>" ) <EOL> self . top . wm_iconname ( "<STR_LIT>" ) <EOL> def rootnode ( self ) : <EOL> return PathBrowserTreeItem ( ) <EOL> class PathBrowserTreeItem ( TreeItem ) : <EOL> def GetText ( self ) : <EOL> return "<STR_LIT>" <EOL> def GetSubList ( self ) : <EOL> sublist = [ ] <EOL> for dir in sys . path : <EOL> item = DirBrowserTreeItem ( dir ) <EOL> sublist . append ( item ) <EOL> return sublist <EOL> class DirBrowserTreeItem ( TreeItem ) : <EOL> def __init__ ( self , dir , packages = [ ] ) : <EOL> self . dir = dir <EOL> self . packages = packages <EOL> def GetText ( self ) : <EOL> if not self . packages : <EOL> return self . dir <EOL> else : <EOL> return self . packages [ - <NUM_LIT:1> ] + "<STR_LIT>" <EOL> def GetSubList ( self ) : <EOL> try : <EOL> names = os . listdir ( self . dir or os . curdir ) <EOL> except os . error : <EOL> return [ ] <EOL> packages = [ ] <EOL> for name in names : <EOL> file = os . path . join ( self . dir , name ) <EOL> if self . ispackagedir ( file ) : <EOL> nn = os . path . normcase ( name ) <EOL> packages . append ( ( nn , name , file ) ) <EOL> packages . sort ( ) <EOL> sublist = [ ] <EOL> for nn , name , file in packages : <EOL> item = DirBrowserTreeItem ( file , self . packages + [ name ] ) <EOL> sublist . append ( item ) <EOL> for nn , name in self . listmodules ( names ) : <EOL> item = ModuleBrowserTreeItem ( os . path . join ( self . dir , name ) ) <EOL> sublist . append ( item ) <EOL> return sublist <EOL> def ispackagedir ( self , file ) : <EOL> if not os . path . isdir ( file ) : <EOL> return <NUM_LIT:0> <EOL> init = os . path . join ( file , "<STR_LIT>" ) <EOL> return os . path . exists ( init ) <EOL> def listmodules ( self , allnames ) : <EOL> modules = { } <EOL> suffixes = imp . get_suffixes ( ) <EOL> sorted = [ ] <EOL> for suff , mode , flag in suffixes : <EOL> i = - len ( suff ) <EOL> for name in allnames [ : ] : <EOL> normed_name = os . path . normcase ( name ) <EOL> if normed_name [ i : ] == suff : <EOL> mod_name = name [ : i ] <EOL> if mod_name not in modules : <EOL> modules [ mod_name ] = None <EOL> sorted . append ( ( normed_name , name ) ) <EOL> allnames . remove ( name ) <EOL> sorted . sort ( ) <EOL> return sorted <EOL> def _path_browser ( parent ) : <EOL> flist = PyShellFileList ( parent ) <EOL> PathBrowser ( flist , _htest = True ) <EOL> parent . mainloop ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> from unittest import main <EOL> main ( '<STR_LIT>' , verbosity = <NUM_LIT:2> , exit = False ) <EOL> from idlelib . idle_test . htest import run <EOL> run ( _path_browser ) </s>
