<s> from pkg_resources import iter_entry_points <EOL> from datetime import datetime <EOL> import pkg_resources <EOL> import time <EOL> import oaipmh <EOL> import oaipmh . metadata <EOL> import oaipmh . server <EOL> import oaipmh . error <EOL> def get_writer ( prefix , config , db ) : <EOL> for writer in iter_entry_points ( group = '<STR_LIT>' , name = prefix ) : <EOL> return writer . load ( ) ( prefix , config , db ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' % prefix ) <EOL> class OAIServer ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , db , config ) : <EOL> self . db = db <EOL> self . config = config <EOL> def identify ( self ) : <EOL> result = oaipmh . common . Identify ( <EOL> repositoryName = self . config . name , <EOL> baseURL = self . config . url , <EOL> protocolVersion = '<STR_LIT>' , <EOL> adminEmails = self . config . admins , <EOL> earliestDatestamp = self . db . oai_earliest_datestamp ( ) , <EOL> deletedRecord = '<STR_LIT>' , <EOL> granularity = '<STR_LIT>' , <EOL> compression = [ '<STR_LIT>' ] , <EOL> toolkit_description = False ) <EOL> version = '<STR_LIT>' <EOL> pyoai_egg = pkg_resources . working_set . find ( <EOL> pkg_resources . Requirement . parse ( '<STR_LIT>' ) ) <EOL> moai_egg = pkg_resources . working_set . find ( <EOL> pkg_resources . Requirement . parse ( '<STR_LIT>' ) ) <EOL> if moai_egg and pyoai_egg : <EOL> version = '<STR_LIT>' % ( <EOL> moai_egg . version , <EOL> pyoai_egg . version ) <EOL> result . add_description ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT:%s>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % version ) <EOL> return result <EOL> def listMetadataFormats ( self , identifier = None ) : <EOL> result = [ ] <EOL> for prefix in self . config . metadata_prefixes : <EOL> writer = get_writer ( prefix , self . config , self . db ) <EOL> ns = writer . get_namespace ( ) <EOL> schema = writer . get_schema_location ( ) <EOL> result . append ( ( prefix , schema , ns ) ) <EOL> return result <EOL> def listSets ( self , cursor = <NUM_LIT:0> , batch_size = <NUM_LIT:20> ) : <EOL> for set in self . db . oai_sets ( cursor , batch_size ) : <EOL> yield [ set [ '<STR_LIT:id>' ] , set [ '<STR_LIT:name>' ] , set [ '<STR_LIT:description>' ] ] <EOL> def listRecords ( self , metadataPrefix , set = None , from_ = None , until = None , <EOL> cursor = <NUM_LIT:0> , batch_size = <NUM_LIT:10> ) : <EOL> self . _checkMetadataPrefix ( metadataPrefix ) <EOL> for record in self . _listQuery ( set , from_ , until , cursor , batch_size ) : <EOL> header , metadata = self . _createHeaderAndMetadata ( record ) <EOL> yield header , metadata , None <EOL> def listIdentifiers ( self , metadataPrefix , set = None , from_ = None , until = None , <EOL> cursor = <NUM_LIT:0> , batch_size = <NUM_LIT:10> ) : <EOL> self . _checkMetadataPrefix ( metadataPrefix ) <EOL> for record in self . _listQuery ( set , from_ , until , cursor , batch_size ) : <EOL> yield self . _createHeader ( record ) <EOL> def getRecord ( self , metadataPrefix , identifier ) : <EOL> self . _checkMetadataPrefix ( metadataPrefix ) <EOL> header = None <EOL> metadata = None <EOL> for record in self . _listQuery ( identifier = identifier ) : <EOL> header , metadata = self . _createHeaderAndMetadata ( record ) <EOL> if header is None : <EOL> raise oaipmh . error . IdDoesNotExistError ( identifier ) <EOL> return header , metadata , None <EOL> def _checkMetadataPrefix ( self , metadataPrefix ) : <EOL> if metadataPrefix not in self . config . metadata_prefixes : <EOL> raise oaipmh . error . CannotDisseminateFormatError <EOL> def _createHeader ( self , record ) : <EOL> deleted = record [ '<STR_LIT>' ] <EOL> for setspec in record [ '<STR_LIT>' ] : <EOL> if setspec in self . config . sets_deleted : <EOL> deleted = True <EOL> break <EOL> return oaipmh . common . Header ( record [ '<STR_LIT:id>' ] , <EOL> record [ '<STR_LIT>' ] , <EOL> record [ '<STR_LIT>' ] , <EOL> deleted ) <EOL> def _createHeaderAndMetadata ( self , record ) : <EOL> header = self . _createHeader ( record ) <EOL> metadata = oaipmh . common . Metadata ( record ) <EOL> metadata . record = record <EOL> return header , metadata <EOL> def _listQuery ( self , set = None , from_ = None , until = None , <EOL> cursor = <NUM_LIT:0> , batch_size = <NUM_LIT:10> , identifier = None ) : <EOL> now = datetime . utcnow ( ) <EOL> if until != None and until > now : <EOL> until = now <EOL> if self . config . delay : <EOL> if until is None : <EOL> until = datetime . utcnow ( ) <EOL> until = until . timetuple ( ) <EOL> ut = time . mktime ( until ) - self . filter_data . delay <EOL> until = datetime . fromtimestamp ( ut ) <EOL> needed_sets = self . config . sets_needed . copy ( ) <EOL> if not set is None : <EOL> needed_sets . add ( set ) <EOL> allowed_sets = self . config . sets_allowed . copy ( ) <EOL> disallowed_sets = self . config . sets_disallowed . copy ( ) <EOL> return self . db . oai_query ( offset = cursor , <EOL> batch_size = batch_size , <EOL> needed_sets = needed_sets , <EOL> disallowed_sets = disallowed_sets , <EOL> allowed_sets = allowed_sets , <EOL> from_date = from_ , <EOL> until_date = until , <EOL> identifier = identifier <EOL> ) <EOL> def OAIServerFactory ( db , config ) : <EOL> """<STR_LIT>""" <EOL> metadata_registry = oaipmh . metadata . MetadataRegistry ( ) <EOL> for prefix in config . metadata_prefixes : <EOL> metadata_registry . registerWriter ( prefix , <EOL> get_writer ( prefix , config , db ) ) <EOL> return oaipmh . server . BatchingServer ( <EOL> OAIServer ( db , config ) , <EOL> metadata_registry = metadata_registry , <EOL> resumption_batch_size = config . batch_size <EOL> ) </s>
