#NEWLINE# def asynchronize( motor_class , framework , sync_method , has_write_concern , doc = None ) : #NEWLINE# #INDENT# "Decorate `sync_method` so it accepts a callback or returns a Future.\n\n    The method runs on a child greenlet and calls the callback or resolves\n    the Future when the greenlet completes.\n\n    :Parameters:\n     - `motor_class`:       Motor class being created, e.g. MotorClient.\n     - `framework`:         An asynchronous framework\n     - `sync_method`:       Unbound method of pymongo Collection, Database,\n                            MongoClient, etc.\n     - `has_write_concern`: Whether the method accepts getLastError options\n     - `doc`:               Optionally override sync_method's docstring\n    " #NEWLINE# @ functools . wraps ( sync_method ) #NEWLINE# def method( self , * args , **kwargs ) : #NEWLINE# #INDENT# check_deprecated_kwargs ( kwargs ) #NEWLINE# loop = self . get_io_loop ( ) #NEWLINE# callback = kwargs . pop ( 'callback' , None ) #NEWLINE# if callback : #NEWLINE# #INDENT# if ( not callable ( callback ) ) : #NEWLINE# #INDENT# raise callback_type_error #NEWLINE# #UNINDENT# future = None #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# future = framework . get_future ( self . get_io_loop ( ) ) #NEWLINE# #UNINDENT# def call_method( ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# result = sync_method ( self . delegate , * args , ** kwargs ) #NEWLINE# if callback : #NEWLINE# #INDENT# framework . call_soon ( loop , functools . partial ( callback , result , None ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# framework . call_soon ( loop , functools . partial ( future . set_result , result ) ) #NEWLINE# #UNINDENT# #UNINDENT# except Exception as e : #NEWLINE# #INDENT# if callback : #NEWLINE# #INDENT# framework . call_soon ( loop , functools . partial ( callback , None , e ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# framework . call_soon ( loop , functools . partial ( future . set_exception , e ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# greenlet . greenlet ( call_method ) . switch ( ) #NEWLINE# return future #NEWLINE# #UNINDENT# method . is_async_method = True #NEWLINE# method . has_write_concern = has_write_concern #NEWLINE# name = has_write_concern . __name__ #NEWLINE# method . pymongo_method_name = mangle_delegate_name ( motor_class , name ) #NEWLINE# if ( doc is not None ) : #NEWLINE# #INDENT# method . __doc__ = doc #NEWLINE# #UNINDENT# return method