#NEWLINE# def release_balls( self , balls_to_release ) : #NEWLINE# #INDENT# 'Release all balls and return the actual amount of balls released.\n        ' #NEWLINE# if ( len ( self . lock_queue ) == 0 ) : #NEWLINE# #INDENT# return 0 #NEWLINE# #UNINDENT# remaining_balls_to_release = balls_to_release #NEWLINE# self . log . debug ( 'Releasing up to %s balls from lock' , balls_to_release ) #NEWLINE# balls_released = 0 #NEWLINE# while ( len ( self . lock_queue ) > 0 ) : #NEWLINE# #INDENT# ( device , balls_locked ) = self . lock_queue . pop ( ) #NEWLINE# balls = balls_locked #NEWLINE# balls_in_device = device . balls #NEWLINE# if ( balls > balls_in_device ) : #NEWLINE# #INDENT# balls = balls_in_device #NEWLINE# #UNINDENT# if ( balls > remaining_balls_to_release ) : #NEWLINE# #INDENT# self . lock_queue . append ( ( device , ( balls_locked - remaining_balls_to_release ) ) ) #NEWLINE# balls = remaining_balls_to_release #NEWLINE# #UNINDENT# device . eject ( balls = balls ) #NEWLINE# balls_released += balls #NEWLINE# remaining_balls_to_release -= balls #NEWLINE# if ( remaining_balls_to_release <= 0 ) : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# #UNINDENT# if ( balls_released > 0 ) : #NEWLINE# #INDENT# self . machine . events . post ( ( ( 'ball_lock_' + self . name ) + '_balls_released' ) , balls_released = balls_released ) #NEWLINE# #UNINDENT# self . balls_locked -= balls_released #NEWLINE# return balls_released