Skip to content

Commit

Permalink
infrae#3 add None to oaipmh.common.Header and oaipmh.common.Metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
akorosov committed Apr 22, 2016
1 parent fb2d8e9 commit a44f82b
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions moai/oai.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ def get_writer(prefix, config, db):

class OAIServer(object):
"""An OAI-2.0 compliant oai server.
Underlying code is based on pyoai's oaipmh.server'
"""

def __init__(self, db, config):
self.db = db
self.config = config
Expand All @@ -43,7 +43,7 @@ def identify(self):
pkg_resources.Requirement.parse('pyoai'))
moai_egg = pkg_resources.working_set.find(
pkg_resources.Requirement.parse('MOAI'))

if moai_egg and pyoai_egg:
version = '<version>%s (using pyoai%s)</version>' % (
moai_egg.version,
Expand All @@ -58,7 +58,7 @@ def identify(self):
'%s'
'<URL>http://moai.infrae.com</URL>'
'</toolkit>' % version)

return result

def listMetadataFormats(self, identifier=None):
Expand All @@ -69,22 +69,22 @@ def listMetadataFormats(self, identifier=None):
schema = writer.get_schema_location()
result.append((prefix, schema, ns))
return result

def listSets(self, cursor=0, batch_size=20):
for set in self.db.oai_sets(cursor, batch_size):
yield [set['id'], set['name'], set['description']]

def listRecords(self, metadataPrefix, set=None, from_=None, until=None,
cursor=0, batch_size=10):

self._checkMetadataPrefix(metadataPrefix)
for record in self._listQuery(set, from_, until, cursor, batch_size):
header, metadata = self._createHeaderAndMetadata(record)
yield header, metadata, None

def listIdentifiers(self, metadataPrefix, set=None, from_=None, until=None,
cursor=0, batch_size=10):

self._checkMetadataPrefix(metadataPrefix)
for record in self._listQuery(set, from_, until, cursor, batch_size):
yield self._createHeader(record)
Expand All @@ -98,7 +98,7 @@ def getRecord(self, metadataPrefix, identifier):
if header is None:
raise oaipmh.error.IdDoesNotExistError(identifier)
return header, metadata, None

def _checkMetadataPrefix(self, metadataPrefix):
if metadataPrefix not in self.config.metadata_prefixes:
raise oaipmh.error.CannotDisseminateFormatError
Expand All @@ -109,39 +109,40 @@ def _createHeader(self, record):
if setspec in self.config.sets_deleted:
deleted = True
break
return oaipmh.common.Header(record['id'],
return oaipmh.common.Header(None,
record['id'],
record['modified'],
record['sets'],
deleted)

def _createHeaderAndMetadata(self, record):
header = self._createHeader(record)
metadata = oaipmh.common.Metadata(record)
metadata = oaipmh.common.Metadata(None, record)
metadata.record = record
return header, metadata
def _listQuery(self, set=None, from_=None, until=None,

def _listQuery(self, set=None, from_=None, until=None,
cursor=0, batch_size=10, identifier=None):

now = datetime.utcnow()
if until != None and until > now:
# until should never be in the future
until = now

if self.config.delay:
# subtract delay from until_ param, if present
if until is None:
until = datetime.utcnow()
until = until.timetuple()
ut = time.mktime(until)-self.filter_data.delay
until = datetime.fromtimestamp(ut)

needed_sets = self.config.sets_needed.copy()
if not set is None:
needed_sets.add(set)
allowed_sets = self.config.sets_allowed.copy()
disallowed_sets = self.config.sets_disallowed.copy()
disallowed_sets = self.config.sets_disallowed.copy()

return self.db.oai_query(offset=cursor,
batch_size=batch_size,
needed_sets=needed_sets,
Expand All @@ -155,12 +156,12 @@ def _listQuery(self, set=None, from_=None, until=None,
def OAIServerFactory(db, config):
"""Create a new OAI batching OAI Server given a config and
a database"""

metadata_registry = oaipmh.metadata.MetadataRegistry()
for prefix in config.metadata_prefixes:
metadata_registry.registerWriter(prefix,
get_writer(prefix, config, db))

return oaipmh.server.BatchingServer(
OAIServer(db, config),
metadata_registry=metadata_registry,
Expand Down

0 comments on commit a44f82b

Please sign in to comment.