<s> import weakref <EOL> class HasEvents ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , parent ) : <EOL> self . _events = [ ] <EOL> self . _parent = None if parent is None else weakref . ref ( parent ) <EOL> def __getstate__ ( self ) : <EOL> state = self . __dict__ . copy ( ) <EOL> state [ '<STR_LIT>' ] = self . parent <EOL> return state <EOL> def __setstate__ ( self , state ) : <EOL> self . __dict__ . update ( state ) <EOL> parent = state [ '<STR_LIT>' ] <EOL> self . _parent = None if parent is None else weakref . ref ( parent ) <EOL> @ property <EOL> def parent ( self ) : <EOL> """<STR_LIT>""" <EOL> return None if self . _parent is None else self . _parent ( ) <EOL> def _item_count ( self ) : <EOL> """<STR_LIT>""" <EOL> return len ( self . _events ) <EOL> def add_event ( self , name ) : <EOL> """<STR_LIT>""" <EOL> if name in self . _events : <EOL> self . parent . raise_exception ( "<STR_LIT>" <EOL> "<STR_LIT>" % name , <EOL> AttributeError ) <EOL> try : <EOL> typ = self . parent . parent . get_metadata ( name , '<STR_LIT:type>' ) <EOL> except AttributeError : <EOL> self . parent . raise_exception ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( name ) , AttributeError ) <EOL> if typ != '<STR_LIT>' : <EOL> self . parent . raise_exception ( "<STR_LIT>" % name , TypeError ) <EOL> self . _events . append ( name ) <EOL> def remove_event ( self , name ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . _events . remove ( name ) <EOL> except ValueError : <EOL> self . parent . raise_exception ( "<STR_LIT>" <EOL> "<STR_LIT>" % name , <EOL> AttributeError ) <EOL> def get_events ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _events <EOL> def list_available_events ( self ) : <EOL> """<STR_LIT>""" <EOL> events = [ ] <EOL> for comp in self . parent . workflow . __iter__ ( ) : <EOL> tset1 = set ( comp . _alltraits ( events = True ) ) <EOL> tset2 = set ( comp . _alltraits ( events = False ) ) <EOL> event_set = tset1 . difference ( tset2 ) <EOL> event_set . remove ( '<STR_LIT>' ) <EOL> event_set . remove ( '<STR_LIT>' ) <EOL> for item in event_set : <EOL> name = '<STR_LIT>' % ( comp . name , item ) <EOL> if name not in self . _events : <EOL> events . append ( name ) <EOL> return events <EOL> def clear_events ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _events = [ ] <EOL> def set_events ( self ) : <EOL> """<STR_LIT>""" <EOL> scope = self . parent . parent <EOL> for event in self . _events : <EOL> scope . set ( event , True ) <EOL> def mimic ( self , target ) : <EOL> """<STR_LIT>""" <EOL> self . clear_events ( ) <EOL> self . _events = target . get_events ( ) [ : ] </s>
