<s> import os , stat , time <EOL> import pygtk <EOL> pygtk . require ( '<STR_LIT>' ) <EOL> import gtk <EOL> folderxpm = [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ] <EOL> folderpb = gtk . gdk . pixbuf_new_from_xpm_data ( folderxpm ) <EOL> filexpm = [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ] <EOL> filepb = gtk . gdk . pixbuf_new_from_xpm_data ( filexpm ) <EOL> class FileListModel ( gtk . GenericTreeModel ) : <EOL> column_types = ( gtk . gdk . Pixbuf , str , long , str , str ) <EOL> column_names = [ '<STR_LIT:Name>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def __init__ ( self , dname = None ) : <EOL> gtk . GenericTreeModel . __init__ ( self ) <EOL> if not dname : <EOL> self . dirname = os . path . expanduser ( '<STR_LIT>' ) <EOL> else : <EOL> self . dirname = os . path . abspath ( dname ) <EOL> self . files = [ f for f in os . listdir ( self . dirname ) if f [ <NUM_LIT:0> ] < > '<STR_LIT:.>' ] <EOL> self . files . sort ( ) <EOL> self . files = [ '<STR_LIT:..>' ] + self . files <EOL> return <EOL> def get_pathname ( self , path ) : <EOL> filename = self . files [ path [ <NUM_LIT:0> ] ] <EOL> return os . path . join ( self . dirname , filename ) <EOL> def is_folder ( self , path ) : <EOL> filename = self . files [ path [ <NUM_LIT:0> ] ] <EOL> pathname = os . path . join ( self . dirname , filename ) <EOL> filestat = os . stat ( pathname ) <EOL> if stat . S_ISDIR ( filestat . st_mode ) : <EOL> return True <EOL> return False <EOL> def get_column_names ( self ) : <EOL> return self . column_names [ : ] <EOL> def on_get_flags ( self ) : <EOL> return gtk . TREE_MODEL_LIST_ONLY | gtk . TREE_MODEL_ITERS_PERSIST <EOL> def on_get_n_columns ( self ) : <EOL> return len ( self . column_types ) <EOL> def on_get_column_type ( self , n ) : <EOL> return self . column_types [ n ] <EOL> def on_get_iter ( self , path ) : <EOL> return self . files [ path [ <NUM_LIT:0> ] ] <EOL> def on_get_path ( self , rowref ) : <EOL> return self . files . index ( rowref ) <EOL> def on_get_value ( self , rowref , column ) : <EOL> fname = os . path . join ( self . dirname , rowref ) <EOL> try : <EOL> filestat = os . stat ( fname ) <EOL> except OSError : <EOL> return None <EOL> mode = filestat . st_mode <EOL> if column is <NUM_LIT:0> : <EOL> if stat . S_ISDIR ( mode ) : <EOL> return folderpb <EOL> else : <EOL> return filepb <EOL> elif column is <NUM_LIT:1> : <EOL> return rowref <EOL> elif column is <NUM_LIT:2> : <EOL> return filestat . st_size <EOL> elif column is <NUM_LIT:3> : <EOL> return oct ( stat . S_IMODE ( mode ) ) <EOL> return time . ctime ( filestat . st_mtime ) <EOL> def on_iter_next ( self , rowref ) : <EOL> try : <EOL> i = self . files . index ( rowref ) + <NUM_LIT:1> <EOL> return self . files [ i ] <EOL> except IndexError : <EOL> return None <EOL> def on_iter_children ( self , rowref ) : <EOL> if rowref : <EOL> return None <EOL> return self . files [ <NUM_LIT:0> ] <EOL> def on_iter_has_child ( self , rowref ) : <EOL> return False <EOL> def on_iter_n_children ( self , rowref ) : <EOL> if rowref : <EOL> return <NUM_LIT:0> <EOL> return len ( self . files ) <EOL> def on_iter_nth_child ( self , rowref , n ) : <EOL> if rowref : <EOL> return None <EOL> try : <EOL> return self . files [ n ] <EOL> except IndexError : <EOL> return None <EOL> def on_iter_parent ( child ) : <EOL> return None <EOL> class GenericTreeModelExample : <EOL> def delete_event ( self , widget , event , data = None ) : <EOL> gtk . main_quit ( ) <EOL> return False <EOL> def __init__ ( self ) : <EOL> self . window = gtk . Window ( gtk . WINDOW_TOPLEVEL ) <EOL> self . window . set_size_request ( <NUM_LIT> , <NUM_LIT:200> ) <EOL> self . window . connect ( "<STR_LIT>" , self . delete_event ) <EOL> self . listmodel = FileListModel ( ) <EOL> self . treeview = gtk . TreeView ( ) <EOL> column_names = self . listmodel . get_column_names ( ) <EOL> self . tvcolumn = [ None ] * len ( column_names ) <EOL> cellpb = gtk . CellRendererPixbuf ( ) <EOL> self . tvcolumn [ <NUM_LIT:0> ] = gtk . TreeViewColumn ( column_names [ <NUM_LIT:0> ] , <EOL> cellpb , pixbuf = <NUM_LIT:0> ) <EOL> cell = gtk . CellRendererText ( ) <EOL> self . tvcolumn [ <NUM_LIT:0> ] . pack_start ( cell , False ) <EOL> self . tvcolumn [ <NUM_LIT:0> ] . add_attribute ( cell , '<STR_LIT:text>' , <NUM_LIT:1> ) <EOL> self . treeview . append_column ( self . tvcolumn [ <NUM_LIT:0> ] ) <EOL> for n in range ( <NUM_LIT:1> , len ( column_names ) ) : <EOL> cell = gtk . CellRendererText ( ) <EOL> if n == <NUM_LIT:1> : <EOL> cell . set_property ( '<STR_LIT>' , <NUM_LIT:1.0> ) <EOL> self . tvcolumn [ n ] = gtk . TreeViewColumn ( column_names [ n ] , <EOL> cell , text = n + <NUM_LIT:1> ) <EOL> self . treeview . append_column ( self . tvcolumn [ n ] ) <EOL> self . treeview . connect ( '<STR_LIT>' , self . open_file ) <EOL> self . scrolledwindow = gtk . ScrolledWindow ( ) <EOL> self . scrolledwindow . add ( self . treeview ) <EOL> self . window . add ( self . scrolledwindow ) <EOL> self . treeview . set_model ( self . listmodel ) <EOL> self . window . set_title ( self . listmodel . dirname ) <EOL> self . window . show_all ( ) <EOL> def open_file ( self , treeview , path , column ) : <EOL> model = treeview . get_model ( ) <EOL> if model . is_folder ( path ) : <EOL> pathname = model . get_pathname ( path ) <EOL> new_model = FileListModel ( pathname ) <EOL> self . window . set_title ( new_model . dirname ) <EOL> treeview . set_model ( new_model ) <EOL> return <EOL> def main ( ) : <EOL> gtk . main ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> gtmexample = GenericTreeModelExample ( ) <EOL> main ( ) </s>
