<s> from . TreeWidget import TreeItem , TreeNode , ScrolledCanvas <EOL> from reprlib import Repr <EOL> myrepr = Repr ( ) <EOL> myrepr . maxstring = <NUM_LIT:100> <EOL> myrepr . maxother = <NUM_LIT:100> <EOL> class ObjectTreeItem ( TreeItem ) : <EOL> def __init__ ( self , labeltext , object , setfunction = None ) : <EOL> self . labeltext = labeltext <EOL> self . object = object <EOL> self . setfunction = setfunction <EOL> def GetLabelText ( self ) : <EOL> return self . labeltext <EOL> def GetText ( self ) : <EOL> return myrepr . repr ( self . object ) <EOL> def GetIconName ( self ) : <EOL> if not self . IsExpandable ( ) : <EOL> return "<STR_LIT>" <EOL> def IsEditable ( self ) : <EOL> return self . setfunction is not None <EOL> def SetText ( self , text ) : <EOL> try : <EOL> value = eval ( text ) <EOL> self . setfunction ( value ) <EOL> except : <EOL> pass <EOL> else : <EOL> self . object = value <EOL> def IsExpandable ( self ) : <EOL> return not not dir ( self . object ) <EOL> def GetSubList ( self ) : <EOL> keys = dir ( self . object ) <EOL> sublist = [ ] <EOL> for key in keys : <EOL> try : <EOL> value = getattr ( self . object , key ) <EOL> except AttributeError : <EOL> continue <EOL> item = make_objecttreeitem ( <EOL> str ( key ) + "<STR_LIT>" , <EOL> value , <EOL> lambda value , key = key , object = self . object : <EOL> setattr ( object , key , value ) ) <EOL> sublist . append ( item ) <EOL> return sublist <EOL> class ClassTreeItem ( ObjectTreeItem ) : <EOL> def IsExpandable ( self ) : <EOL> return True <EOL> def GetSubList ( self ) : <EOL> sublist = ObjectTreeItem . GetSubList ( self ) <EOL> if len ( self . object . __bases__ ) == <NUM_LIT:1> : <EOL> item = make_objecttreeitem ( "<STR_LIT>" , <EOL> self . object . __bases__ [ <NUM_LIT:0> ] ) <EOL> else : <EOL> item = make_objecttreeitem ( "<STR_LIT>" , self . object . __bases__ ) <EOL> sublist . insert ( <NUM_LIT:0> , item ) <EOL> return sublist <EOL> class AtomicObjectTreeItem ( ObjectTreeItem ) : <EOL> def IsExpandable ( self ) : <EOL> return <NUM_LIT:0> <EOL> class SequenceTreeItem ( ObjectTreeItem ) : <EOL> def IsExpandable ( self ) : <EOL> return len ( self . object ) > <NUM_LIT:0> <EOL> def keys ( self ) : <EOL> return range ( len ( self . object ) ) <EOL> def GetSubList ( self ) : <EOL> sublist = [ ] <EOL> for key in self . keys ( ) : <EOL> try : <EOL> value = self . object [ key ] <EOL> except KeyError : <EOL> continue <EOL> def setfunction ( value , key = key , object = self . object ) : <EOL> object [ key ] = value <EOL> item = make_objecttreeitem ( "<STR_LIT>" % ( key , ) , value , setfunction ) <EOL> sublist . append ( item ) <EOL> return sublist <EOL> class DictTreeItem ( SequenceTreeItem ) : <EOL> def keys ( self ) : <EOL> keys = list ( self . object . keys ( ) ) <EOL> try : <EOL> keys . sort ( ) <EOL> except : <EOL> pass <EOL> return keys <EOL> dispatch = { <EOL> int : AtomicObjectTreeItem , <EOL> float : AtomicObjectTreeItem , <EOL> str : AtomicObjectTreeItem , <EOL> tuple : SequenceTreeItem , <EOL> list : SequenceTreeItem , <EOL> dict : DictTreeItem , <EOL> type : ClassTreeItem , <EOL> } <EOL> def make_objecttreeitem ( labeltext , object , setfunction = None ) : <EOL> t = type ( object ) <EOL> if t in dispatch : <EOL> c = dispatch [ t ] <EOL> else : <EOL> c = ObjectTreeItem <EOL> return c ( labeltext , object , setfunction ) <EOL> def _test ( ) : <EOL> import sys <EOL> from tkinter import Tk <EOL> root = Tk ( ) <EOL> root . configure ( bd = <NUM_LIT:0> , bg = "<STR_LIT>" ) <EOL> root . focus_set ( ) <EOL> sc = ScrolledCanvas ( root , bg = "<STR_LIT>" , highlightthickness = <NUM_LIT:0> , takefocus = <NUM_LIT:1> ) <EOL> sc . frame . pack ( expand = <NUM_LIT:1> , fill = "<STR_LIT>" ) <EOL> item = make_objecttreeitem ( "<STR_LIT>" , sys ) <EOL> node = TreeNode ( sc . canvas , None , item ) <EOL> node . update ( ) <EOL> root . mainloop ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> _test ( ) </s>
