#NEWLINE# def test_cached( self ) : #NEWLINE# #INDENT# ' cached ' #NEWLINE# class ISaveable ( Interface ) : #NEWLINE# #INDENT# ' Fake interface for saveable. ' #NEWLINE# dirty = Bool ( False ) #NEWLINE# def save( self , output_stream ) : #NEWLINE# #INDENT# ' Save the object to an output stream. ' #NEWLINE# #UNINDENT# #UNINDENT# class HasTraitsToISaveableAdapter ( Adapter ) : #NEWLINE# #INDENT# " An adapter from 'HasTraits' to 'ISaveable'. " #NEWLINE# adapts ( HasTraits , to = ISaveable , cached = True ) #NEWLINE# dirty = Bool ( False ) #NEWLINE# def save( self , output_stream ) : #NEWLINE# #INDENT# ' Save the object to an output stream. ' #NEWLINE# pickle . dump ( self . adaptee , output_stream ) #NEWLINE# self . dirty = False #NEWLINE# return #NEWLINE# #UNINDENT# def _adaptee_changed( self , old , new ) : #NEWLINE# #INDENT# ' Static trait change handler. ' #NEWLINE# if ( old is not None ) : #NEWLINE# #INDENT# old . on_trait_change ( self . _set_dirty , remove = True ) #NEWLINE# #UNINDENT# if ( new is not None ) : #NEWLINE# #INDENT# new . on_trait_change ( self . _set_dirty ) #NEWLINE# #UNINDENT# self . _set_dirty ( ) #NEWLINE# return #NEWLINE# #UNINDENT# def _set_dirty( self ) : #NEWLINE# #INDENT# ' Sets the dirty flag to True. ' #NEWLINE# self . dirty = True #NEWLINE# return #NEWLINE# #UNINDENT# #UNINDENT# fred = Person ( name = 'fred' , age = 42 ) #NEWLINE# wilma = Person ( name = 'wilma' , age = 35 ) #NEWLINE# fred_saveable = ISaveable ( fred ) #NEWLINE# self . assertEqual ( True , fred_saveable . dirty ) #NEWLINE# wilma_saveable = ISaveable ( wilma ) #NEWLINE# self . assertEqual ( True , wilma_saveable . dirty ) #NEWLINE# self . assertNotEqual ( id ( fred_saveable ) , id ( wilma_saveable ) ) #NEWLINE# self . assertEqual ( id ( ISaveable ( fred ) ) , id ( fred_saveable ) ) #NEWLINE# self . assertEqual ( id ( ISaveable ( wilma ) ) , id ( wilma_saveable ) ) #NEWLINE# fred_saveable . save ( file ( 'fred.pickle' , 'w' ) ) #NEWLINE# self . assertEqual ( False , ISaveable ( fred ) . dirty ) #NEWLINE# wilma_saveable . save ( file ( 'wilma.pickle' , 'w' ) ) #NEWLINE# self . assertEqual ( False , ISaveable ( wilma ) . dirty ) #NEWLINE# for path in [ 'fred.pickle' , 'wilma.pickle' ] : #NEWLINE# #INDENT# if os . access ( path , os . W_OK ) : #NEWLINE# #INDENT# os . remove ( path ) #NEWLINE# #UNINDENT# #UNINDENT# return