#NEWLINE# def capture( self , webcam ) : #NEWLINE# #INDENT# " Capture an image from a webcam\n\n        Given a webcam, this attempts to capture an image using the subprocess\n        command. Also creates a thumbnail of the image\n\n        Args:\n            webcam (dict): Entry for the webcam. Example::\n                {\n                    'name': 'Pier West',\n                    'port': '/dev/video0',\n                    'params': {\n                        'rotate': 270\n                    },\n                }\n\n            The values for the `params` key will be passed directly to fswebcam\n        " #NEWLINE# assert isinstance ( webcam , dict ) #NEWLINE# self . logger . debug ( 'Capturing image for {}...' . format ( webcam . get ( 'name' ) ) ) #NEWLINE# camera_name = webcam . get ( 'port' ) . split ( '/' ) [ ( - 1 ) ] #NEWLINE# webcam_dir = self . config [ 'directories' ] . get ( 'webcam' ) #NEWLINE# timestamp = current_time ( ) . isot #NEWLINE# date_dir = timestamp . split ( 'T' ) [ 0 ] . replace ( '-' , '' ) #NEWLINE# try : #NEWLINE# #INDENT# os . makedirs ( '{}/{}' . format ( webcam_dir , date_dir ) , exist_ok = True ) #NEWLINE# #UNINDENT# except OSError as err : #NEWLINE# #INDENT# self . logger . warning ( 'Cannot create new dir: {} \t {}' . format ( date_dir , err ) ) #NEWLINE# #UNINDENT# out_file = '{}/{}/{}_{}.jpeg' . format ( webcam_dir , out_file , camera_name , timestamp ) #NEWLINE# thumbnail_file = '{}/tn_{}.jpeg' . format ( webcam_dir , camera_name ) #NEWLINE# options = self . base_params #NEWLINE# if ( 'params' in webcam ) : #NEWLINE# #INDENT# for ( opt , val ) in webcam . get ( 'params' ) . items ( ) : #NEWLINE# #INDENT# options += '--{}={}' . format ( opt , val ) #NEWLINE# #UNINDENT# #UNINDENT# params = ' -d {} --title "{}" {} --save {} --scale {} {}' . format ( webcam . get ( 'port' ) , webcam . get ( 'name' ) , options , out_file , self . _thumbnail_resolution , thumbnail_file ) #NEWLINE# try : #NEWLINE# #INDENT# self . logger . debug ( 'Webcam subproccess command: {} {}' . format ( self . cmd , params ) ) #NEWLINE# with open ( os . devnull , 'w' ) as devnull : #NEWLINE# #INDENT# retcode = subprocess . call ( ( self . cmd + params ) , shell = True , stdout = devnull , stderr = devnull ) #NEWLINE# #UNINDENT# if ( retcode < 0 ) : #NEWLINE# #INDENT# self . logger . warning ( 'Image captured terminated for {}. Return code: {} \t Error: {}' . format ( webcam . get ( 'name' ) , retcode , sys . stderr ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# self . logger . debug ( 'Image captured for {}' . format ( webcam . get ( 'name' ) ) ) #NEWLINE# static_out_file = '{}/{}.jpeg' . format ( webcam_dir , camera_name ) #NEWLINE# if os . path . lexists ( static_out_file ) : #NEWLINE# #INDENT# os . remove ( static_out_file ) #NEWLINE# #UNINDENT# os . symlink ( out_file , static_out_file ) #NEWLINE# return retcode #NEWLINE# #UNINDENT# #UNINDENT# except OSError as e : #NEWLINE# #INDENT# self . logger . warning ( 'Execution failed:' . format ( e , file = sys . stderr ) )