<s> from atom . api import ContainerList , Bool <EOL> from . declarative import Declarative , d_ <EOL> from . object import Object <EOL> class Include ( Declarative ) : <EOL> """<STR_LIT>""" <EOL> objects = d_ ( ContainerList ( Object ) ) <EOL> destroy_old = d_ ( Bool ( True ) ) <EOL> def initialize ( self ) : <EOL> """<STR_LIT>""" <EOL> super ( Include , self ) . initialize ( ) <EOL> self . parent . insert_children ( self , self . objects ) <EOL> for obj in self . objects : <EOL> obj . initialize ( ) <EOL> def destroy ( self ) : <EOL> """<STR_LIT>""" <EOL> destroy_items = self . destroy_old <EOL> if destroy_items : <EOL> parent = self . parent <EOL> destroy_items = parent is None or not parent . is_destroyed <EOL> super ( Include , self ) . destroy ( ) <EOL> if destroy_items : <EOL> for item in self . objects : <EOL> if not item . is_destroyed : <EOL> item . destroy ( ) <EOL> del self . objects <EOL> def _observe_objects ( self , change ) : <EOL> """<STR_LIT>""" <EOL> if self . is_initialized : <EOL> if change [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> oldvalue = change [ '<STR_LIT>' ] <EOL> newvalue = change [ '<STR_LIT:value>' ] <EOL> newset = set ( newvalue ) <EOL> if self . destroy_old : <EOL> for obj in oldvalue : <EOL> if obj not in newset : <EOL> obj . destroy ( ) <EOL> else : <EOL> for obj in oldvalue : <EOL> if obj not in newset : <EOL> obj . set_parent ( None ) <EOL> if newvalue : <EOL> self . parent . insert_children ( self , newvalue ) <EOL> elif change [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> added = [ ] <EOL> removed = [ ] <EOL> op = change [ '<STR_LIT>' ] <EOL> if op == '<STR_LIT>' : <EOL> item = change [ '<STR_LIT>' ] <EOL> if isinstance ( item , list ) : <EOL> removed . extend ( item ) <EOL> else : <EOL> removed . append ( item ) <EOL> elif op == '<STR_LIT>' : <EOL> added . extend ( change [ '<STR_LIT>' ] ) <EOL> elif op == '<STR_LIT>' : <EOL> olditem = change [ '<STR_LIT>' ] <EOL> newitem = change [ '<STR_LIT>' ] <EOL> if isinstance ( olditem , list ) : <EOL> removed . extend ( olditem ) <EOL> else : <EOL> removed . append ( olditem ) <EOL> if isinstance ( newitem , list ) : <EOL> added . extend ( newitem ) <EOL> else : <EOL> added . append ( newitem ) <EOL> elif op == '<STR_LIT>' : <EOL> added . append ( change [ '<STR_LIT>' ] ) <EOL> elif op == '<STR_LIT>' : <EOL> added . extend ( change [ '<STR_LIT>' ] ) <EOL> elif op == '<STR_LIT>' : <EOL> added . append ( change [ '<STR_LIT>' ] ) <EOL> elif op == '<STR_LIT>' : <EOL> removed . append ( change [ '<STR_LIT>' ] ) <EOL> elif op == '<STR_LIT>' : <EOL> removed . append ( change [ '<STR_LIT>' ] ) <EOL> addset = set ( added ) <EOL> if self . destroy_old : <EOL> for obj in removed : <EOL> if obj not in addset : <EOL> obj . destroy ( ) <EOL> else : <EOL> for obj in removed : <EOL> if obj not in addset : <EOL> obj . set_parent ( None ) <EOL> self . parent . insert_children ( self , change [ '<STR_LIT:value>' ] ) </s>
