<s> """<STR_LIT>""" <EOL> from pymongo import Connection <EOL> from pymongo . binary import Binary <EOL> from IPython . utils . traitlets import Dict , List , Unicode , Instance <EOL> from . dictdb import BaseDB <EOL> class MongoDB ( BaseDB ) : <EOL> """<STR_LIT>""" <EOL> connection_args = List ( config = True , <EOL> help = """<STR_LIT>""" ) <EOL> connection_kwargs = Dict ( config = True , <EOL> help = """<STR_LIT>""" <EOL> ) <EOL> database = Unicode ( config = True , <EOL> help = """<STR_LIT>""" ) <EOL> _connection = Instance ( Connection ) <EOL> def __init__ ( self , ** kwargs ) : <EOL> super ( MongoDB , self ) . __init__ ( ** kwargs ) <EOL> if self . _connection is None : <EOL> self . _connection = Connection ( * self . connection_args , ** self . connection_kwargs ) <EOL> if not self . database : <EOL> self . database = self . session <EOL> self . _db = self . _connection [ self . database ] <EOL> self . _records = self . _db [ '<STR_LIT>' ] <EOL> self . _records . ensure_index ( '<STR_LIT>' , unique = True ) <EOL> self . _records . ensure_index ( '<STR_LIT>' ) <EOL> def _binary_buffers ( self , rec ) : <EOL> for key in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if rec . get ( key , None ) : <EOL> rec [ key ] = list ( map ( Binary , rec [ key ] ) ) <EOL> return rec <EOL> def add_record ( self , msg_id , rec ) : <EOL> """<STR_LIT>""" <EOL> rec = self . _binary_buffers ( rec ) <EOL> self . _records . insert ( rec ) <EOL> def get_record ( self , msg_id ) : <EOL> """<STR_LIT>""" <EOL> r = self . _records . find_one ( { '<STR_LIT>' : msg_id } ) <EOL> if not r : <EOL> raise KeyError ( msg_id ) <EOL> return r <EOL> def update_record ( self , msg_id , rec ) : <EOL> """<STR_LIT>""" <EOL> rec = self . _binary_buffers ( rec ) <EOL> self . _records . update ( { '<STR_LIT>' : msg_id } , { '<STR_LIT>' : rec } ) <EOL> def drop_matching_records ( self , check ) : <EOL> """<STR_LIT>""" <EOL> self . _records . remove ( check ) <EOL> def drop_record ( self , msg_id ) : <EOL> """<STR_LIT>""" <EOL> self . _records . remove ( { '<STR_LIT>' : msg_id } ) <EOL> def find_records ( self , check , keys = None ) : <EOL> """<STR_LIT>""" <EOL> if keys and '<STR_LIT>' not in keys : <EOL> keys . append ( '<STR_LIT>' ) <EOL> matches = list ( self . _records . find ( check , keys ) ) <EOL> for rec in matches : <EOL> rec . pop ( '<STR_LIT>' ) <EOL> return matches <EOL> def get_history ( self ) : <EOL> """<STR_LIT>""" <EOL> cursor = self . _records . find ( { } , { '<STR_LIT>' : <NUM_LIT:1> } ) . sort ( '<STR_LIT>' ) <EOL> return [ rec [ '<STR_LIT>' ] for rec in cursor ] </s>
