<s> import logging <EOL> from imgfac . Singleton import Singleton <EOL> from Builder import Builder <EOL> from imgfac . NotificationCenter import NotificationCenter <EOL> from threading import BoundedSemaphore <EOL> class BuildDispatcher ( Singleton ) : <EOL> def _singleton_init ( self ) : <EOL> self . log = logging . getLogger ( '<STR_LIT>' % ( __name__ , self . __class__ . __name__ ) ) <EOL> self . builders = dict ( ) <EOL> self . builders_lock = BoundedSemaphore ( ) <EOL> NotificationCenter ( ) . add_observer ( self , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def handle_state_change ( self , notification ) : <EOL> status = notification . user_info [ '<STR_LIT>' ] <EOL> if ( status in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> self . builders_lock . acquire ( ) <EOL> image_id = notification . sender . identifier <EOL> if ( image_id in self . builders ) : <EOL> del self . builders [ image_id ] <EOL> self . log . debug ( '<STR_LIT>' % ( image_id , status ) ) <EOL> self . builders_lock . release ( ) <EOL> def builder_for_base_image ( self , template , parameters = None ) : <EOL> builder = Builder ( ) <EOL> builder . build_image_from_template ( template , parameters = parameters ) <EOL> self . builders_lock . acquire ( ) <EOL> try : <EOL> self . builders [ builder . base_image . identifier ] = builder <EOL> finally : <EOL> self . builders_lock . release ( ) <EOL> return builder <EOL> def builder_for_target_image ( self , target , image_id = None , template = None , parameters = None ) : <EOL> builder = Builder ( ) <EOL> builder . customize_image_for_target ( target , image_id , template , parameters ) <EOL> self . builders_lock . acquire ( ) <EOL> try : <EOL> self . builders [ builder . target_image . identifier ] = builder <EOL> finally : <EOL> self . builders_lock . release ( ) <EOL> return builder <EOL> def builder_for_provider_image ( self , provider , credentials , target , image_id = None , template = None , parameters = None , my_image_id = None ) : <EOL> builder = Builder ( ) <EOL> builder . create_image_on_provider ( provider , credentials , target , image_id , template , parameters , my_image_id ) <EOL> self . builders_lock . acquire ( ) <EOL> try : <EOL> self . builders [ builder . provider_image . identifier ] = builder <EOL> finally : <EOL> self . builders_lock . release ( ) <EOL> return builder </s>
