<s> from bson . son import SON <EOL> from pymongo . helpers import _check_command_response <EOL> from twisted . internet import defer <EOL> from twisted . python . compat import unicode <EOL> from txmongo . collection import Collection <EOL> from txmongo . utils import timeout <EOL> class Database ( object ) : <EOL> __factory = None <EOL> def __init__ ( self , factory , database_name , write_concern = None ) : <EOL> self . __factory = factory <EOL> self . __write_concern = write_concern <EOL> self . _database_name = unicode ( database_name ) <EOL> def __str__ ( self ) : <EOL> return self . _database_name <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . __factory , self . _database_name , ) <EOL> def __call__ ( self , database_name ) : <EOL> return Database ( self . __factory , database_name , self . __write_concern ) <EOL> def __getitem__ ( self , collection_name ) : <EOL> return Collection ( self , collection_name ) <EOL> def __getattr__ ( self , collection_name ) : <EOL> return self [ collection_name ] <EOL> @ property <EOL> def name ( self ) : <EOL> return self . _database_name <EOL> @ property <EOL> def connection ( self ) : <EOL> return self . __factory <EOL> @ property <EOL> def write_concern ( self ) : <EOL> return self . __write_concern or self . __factory . write_concern <EOL> @ timeout <EOL> @ defer . inlineCallbacks <EOL> def command ( self , command , value = <NUM_LIT:1> , check = True , allowable_errors = None , ** kwargs ) : <EOL> if isinstance ( command , ( bytes , unicode ) ) : <EOL> command = SON ( [ ( command , value ) ] ) <EOL> options = kwargs . copy ( ) <EOL> options . pop ( "<STR_LIT>" , None ) <EOL> command . update ( options ) <EOL> ns = self [ "<STR_LIT>" ] <EOL> response = yield ns . find_one ( command , ** kwargs ) <EOL> if check : <EOL> msg = "<STR_LIT>" . format ( repr ( command ) , ns ) <EOL> _check_command_response ( response , msg , allowable_errors ) <EOL> defer . returnValue ( response ) <EOL> @ timeout <EOL> @ defer . inlineCallbacks <EOL> def create_collection ( self , name , options = None , ** kwargs ) : <EOL> collection = Collection ( self , name ) <EOL> if options : <EOL> if "<STR_LIT:size>" in options : <EOL> options [ "<STR_LIT:size>" ] = float ( options [ "<STR_LIT:size>" ] ) <EOL> options . update ( kwargs ) <EOL> yield self . command ( "<STR_LIT>" , name , ** options ) <EOL> defer . returnValue ( collection ) <EOL> @ timeout <EOL> @ defer . inlineCallbacks <EOL> def drop_collection ( self , name_or_collection , ** kwargs ) : <EOL> if isinstance ( name_or_collection , Collection ) : <EOL> name = name_or_collection . _collection_name <EOL> elif isinstance ( name_or_collection , ( bytes , unicode ) ) : <EOL> name = name_or_collection <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> yield self . command ( "<STR_LIT>" , unicode ( name ) , allowable_errors = [ "<STR_LIT>" ] , ** kwargs ) <EOL> @ timeout <EOL> @ defer . inlineCallbacks <EOL> def collection_names ( self , ** kwargs ) : <EOL> results = yield self [ "<STR_LIT>" ] . find ( ** kwargs ) <EOL> names = [ r [ "<STR_LIT:name>" ] for r in results ] <EOL> names = [ n [ len ( str ( self ) ) + <NUM_LIT:1> : ] for n in names <EOL> if n . startswith ( str ( self ) + "<STR_LIT:.>" ) ] <EOL> names = [ n for n in names if "<STR_LIT:$>" not in n ] <EOL> defer . returnValue ( names ) <EOL> @ defer . inlineCallbacks <EOL> def authenticate ( self , name , password , mechanism = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( name , ( bytes , unicode ) ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if not isinstance ( password , ( bytes , unicode ) ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> """<STR_LIT>""" <EOL> yield self . connection . authenticate ( self , name , password , mechanism ) </s>
