<s> import time <EOL> from pymongo import MongoClient <EOL> from pyspider . database . base . projectdb import ProjectDB as BaseProjectDB <EOL> class ProjectDB ( BaseProjectDB ) : <EOL> __collection_name__ = '<STR_LIT>' <EOL> def __init__ ( self , url , database = '<STR_LIT>' ) : <EOL> self . conn = MongoClient ( url ) <EOL> self . conn . admin . command ( "<STR_LIT>" ) <EOL> self . database = self . conn [ database ] <EOL> self . collection = self . database [ self . __collection_name__ ] <EOL> self . collection . ensure_index ( '<STR_LIT:name>' , unique = True ) <EOL> def _default_fields ( self , each ) : <EOL> if each is None : <EOL> return each <EOL> each . setdefault ( '<STR_LIT>' , None ) <EOL> each . setdefault ( '<STR_LIT:status>' , '<STR_LIT>' ) <EOL> each . setdefault ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> each . setdefault ( '<STR_LIT>' , None ) <EOL> each . setdefault ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> each . setdefault ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> each . setdefault ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> return each <EOL> def insert ( self , name , obj = { } ) : <EOL> obj = dict ( obj ) <EOL> obj [ '<STR_LIT:name>' ] = name <EOL> obj [ '<STR_LIT>' ] = time . time ( ) <EOL> return self . collection . update ( { '<STR_LIT:name>' : name } , { '<STR_LIT>' : obj } , upsert = True ) <EOL> def update ( self , name , obj = { } , ** kwargs ) : <EOL> obj = dict ( obj ) <EOL> obj . update ( kwargs ) <EOL> obj [ '<STR_LIT>' ] = time . time ( ) <EOL> return self . collection . update ( { '<STR_LIT:name>' : name } , { '<STR_LIT>' : obj } ) <EOL> def get_all ( self , fields = None ) : <EOL> for each in self . collection . find ( { } , fields ) : <EOL> if each and '<STR_LIT>' in each : <EOL> del each [ '<STR_LIT>' ] <EOL> yield self . _default_fields ( each ) <EOL> def get ( self , name , fields = None ) : <EOL> each = self . collection . find_one ( { '<STR_LIT:name>' : name } , fields ) <EOL> if each and '<STR_LIT>' in each : <EOL> del each [ '<STR_LIT>' ] <EOL> return self . _default_fields ( each ) <EOL> def check_update ( self , timestamp , fields = None ) : <EOL> for project in self . get_all ( fields = ( '<STR_LIT>' , '<STR_LIT:name>' ) ) : <EOL> if project [ '<STR_LIT>' ] > timestamp : <EOL> project = self . get ( project [ '<STR_LIT:name>' ] , fields ) <EOL> yield self . _default_fields ( project ) <EOL> def drop ( self , name ) : <EOL> return self . collection . remove ( { '<STR_LIT:name>' : name } ) </s>
