def GetRecord(params): try: from lr.mustache.oaipmh import GetRecord as must_GetRecord identifier = params["identifier"] if params["by_doc_ID"] == True: resolver = OAIPMHDocumentResolver() single_doc = o.get_record(params["identifier"]) if single_doc is not None: docList = [resolver.process({"doc": single_doc})] else: docList = [] else: docList = o.get_records_by_resource(params["identifier"]) doc_idx = 0 valid_docs = 0 mustache = must_GetRecord() for doc in docList: if doc is not None: doc_idx += 1 if "payload_schema" in doc and params[ "metadataPrefix"] in map( lambda x: o_mod.getMetadataPrefix(x), doc["payload_schema"] ) and OAIPMHDocumentResolver.PAYLOAD_ERROR not in doc: valid_docs += 1 if valid_docs == 1: part = mustache.prefix(**self._initMustache( args=params, req=t_req)) yield h.fixUtf8( self._returnResponse(part, res=t_res)) part = mustache.doc(doc) yield h.fixUtf8( self._returnResponse(part, res=t_res)) if doc_idx == 0: raise IdDoesNotExistError(params['verb'], req=t_req) elif valid_docs == 0: raise CannotDisseminateFormatError(params['verb'], req=t_req) else: yield h.fixUtf8( self._returnResponse(mustache.suffix(), res=t_res)) except oaipmherrors.Error as e: from lr.mustache.oaipmh import Error as err_stache err = err_stache() yield h.fixUtf8(self._returnResponse(err.xml(e), res=t_res))
def format_data(self, full_docs, data, currentResumptionToken, keys=[]): yield '{"documents":[' num_sent = 0 currentID = None byIDResponseChunks = None count = 0 lastStartKey = None lastId = None if data is not None: firstID = True for doc in data: if hasattr(doc, 'id'): lastStartKey = doc.key lastId = doc.id count += 1 if full_docs: if doc.key != currentID: currentID = doc.key if not firstID: yield ']' + byIDResponseChunks[1] + ',\n' byIDResponseChunks = json.dumps({ 'doc_ID': doc.key, 'document': [] }).split(']') yield byIDResponseChunks[0] + json.dumps(doc.doc) firstID = False else: yield ',\n' + json.dumps(doc.doc) else: if doc.key != currentID: currentID = doc.key if not firstID: yield ',\n' firstID = False yield json.dumps({'doc_ID': doc.key}) if full_docs and byIDResponseChunks is not None: yield ']' + byIDResponseChunks[1] if not self.enable_flow_control: yield "]}" elif count < self.limit: yield '], "resumption_token":%s}' % 'null' else: token_params = { "startkey": lastStartKey, "endkey": None, "startkey_docid": lastId } if len(keys) > 0: token_params["keys"] = keys elif currentResumptionToken and "keys" in currentResumptionToken and len( currentResumptionToken["keys"]) > 0: token_params["keys"] = currentResumptionToken["keys"] token = h.fixUtf8(rt.get_token(self.service_id, **token_params)) yield '], "resumption_token":"%s"}' % token
def GetRecord(params): try: from lr.mustache.oaipmh import GetRecord as must_GetRecord identifier = params["identifier"] if params["by_doc_ID"] == True: resolver = OAIPMHDocumentResolver() single_doc = o.get_record(params["identifier"]) if single_doc is not None: docList = [resolver.process({ "doc": single_doc })] else: docList = [] else: docList = o.get_records_by_resource(params["identifier"]) doc_idx = 0 valid_docs = 0 mustache = must_GetRecord() for doc in docList: if doc is not None: doc_idx += 1 if "payload_schema" in doc and params["metadataPrefix"] in map(lambda x: o_mod.getMetadataPrefix(x), doc["payload_schema"]) and OAIPMHDocumentResolver.PAYLOAD_ERROR not in doc: valid_docs += 1 if valid_docs == 1: part = mustache.prefix(**self._initMustache(args=params, req=t_req)) yield h.fixUtf8(self._returnResponse(part, res=t_res)) part = mustache.doc(doc) yield h.fixUtf8(self._returnResponse(part, res=t_res)) if doc_idx == 0: raise IdDoesNotExistError(params['verb'], req=t_req) elif valid_docs == 0: raise CannotDisseminateFormatError(params['verb'], req=t_req) else: yield h.fixUtf8(self._returnResponse(mustache.suffix(), res=t_res)) except oaipmherrors.Error as e: from lr.mustache.oaipmh import Error as err_stache err = err_stache() yield h.fixUtf8(self._returnResponse(err.xml(e), res=t_res))
def format_data(self, full_docs, data, currentResumptionToken, keys=[]): yield '{"documents":[' num_sent = 0 currentID = None byIDResponseChunks = None count = 0 lastStartKey = None lastId = None if data is not None: firstID = True for doc in data: if hasattr(doc,'id'): lastStartKey = doc.key lastId = doc.id count += 1 if full_docs: if doc.key != currentID: currentID = doc.key if not firstID: yield ']' + byIDResponseChunks[1] + ',\n' byIDResponseChunks = json.dumps({'doc_ID':doc.key,'document':[]}).split(']') yield byIDResponseChunks[0] + json.dumps(doc.doc) firstID = False else: yield ',\n' + json.dumps(doc.doc) else: if doc.key != currentID: currentID = doc.key if not firstID: yield ',\n' firstID = False yield json.dumps({'doc_ID': doc.key}) if full_docs and byIDResponseChunks is not None: yield ']' + byIDResponseChunks[1] if not self.enable_flow_control: yield "]}" elif count < self.limit: yield '], "resumption_token":%s}' % 'null' else: token_params = { "startkey" : lastStartKey, "endkey" : None, "startkey_docid" : lastId } if len(keys) > 0: token_params["keys"] = keys elif currentResumptionToken and "keys" in currentResumptionToken and len(currentResumptionToken["keys"]) > 0: token_params["keys"] = currentResumptionToken["keys"] token = h.fixUtf8(rt.get_token(self.service_id,**token_params)) yield '], "resumption_token":"%s"}' % token
def ListGeneric(params, showDocs=False, record_limit=None): if not showDocs: from lr.mustache.oaipmh import ListIdentifiers as must_ListID mustache = must_ListID() else: from lr.mustache.oaipmh import ListRecords as must_ListRec mustache = must_ListRec() try: doc_index = 0 err_count = 0 metadataPrefix = params["metadataPrefix"] from_date = params["from"] until_date = params["until"] doc_err = None rendered_init = False resumptionToken = None if "resumptionToken" not in params else params[ 'resumptionToken'] records = o.list_identifiers_or_records(metadataPrefix, from_date=from_date, until_date=until_date, rt=resumptionToken, fc_limit=record_limit, include_docs=showDocs) for ident in records: doc_index += 1 doc_err = False if OAIPMHDocumentResolver.PAYLOAD_ERROR in ident: err_count += 1 doc_err = True log.debug( "Payload Error detected, doc_index: {0}, err_count: {1}" .format(doc_index, err_count)) if doc_index - err_count == 1: rendered_init = True part = mustache.prefix( **self._initMustache(args=params, req=t_req)) yield h.fixUtf8(self._returnResponse(part, res=t_res)) if doc_err is False and (record_limit is None or doc_index <= record_limit): part = mustache.doc(ident) yield h.fixUtf8(part) elif enable_flow_control: from lr.lib import resumption_token if doc_index - err_count > 0 and doc_index > record_limit: opts = o.list_opts( metadataPrefix, h.convertToISO8601UTC(ident["node_timestamp"]), until_date) opts["startkey_docid"] = ident["doc_ID"] token = resumption_token.get_token( serviceid=service_id, from_date=from_date, until_date=until_date, **opts) part = mustache.resumptionToken(token) yield h.fixUtf8(part) break elif doc_index - err_count == 0 and doc_index > record_limit: opts = o.list_opts( metadataPrefix, h.convertToISO8601UTC(ident["node_timestamp"]), until_date) opts["startkey_docid"] = ident["doc_ID"] payload = resumption_token.get_payload( from_date=from_date, until_date=until_date, **opts) records = o.list_identifiers_or_records( metadataPrefix, from_date=from_date, until_date=until_date, rt=payload, fc_limit=record_limit, include_docs=showDocs) doc_index = 0 err_count = 0 if doc_index == 0 and err_count == 0: raise NoRecordsMatchError(params['verb'], req=t_req) elif (doc_index - err_count) == 0: raise CannotDisseminateFormatError(params['verb'], req=t_req) else: if enable_flow_control and doc_index <= record_limit: yield h.fixUtf8(mustache.resumptionToken()) yield h.fixUtf8(mustache.suffix()) except oaipmherrors.Error as e: if not rendered_init: from lr.mustache.oaipmh import Error as err_stache err = err_stache() yield h.fixUtf8(self._returnResponse(err.xml(e), res=t_res)) else: from lr.mustache.oaipmh import ErrorOnly as err_stache err = err_stache() yield h.fixUtf8( self._returnResponse(err.xml(e) + mustache.suffix(), res=t_res)) except: log.exception("Unknown Error Occurred")
def ListGeneric(params, showDocs=False, record_limit=None): if not showDocs: from lr.mustache.oaipmh import ListIdentifiers as must_ListID mustache = must_ListID() else: from lr.mustache.oaipmh import ListRecords as must_ListRec mustache = must_ListRec() try: doc_index = 0 err_count = 0 metadataPrefix=params["metadataPrefix"] from_date=params["from"] until_date=params["until"] doc_err = None rendered_init = False resumptionToken = None if "resumptionToken" not in params else params['resumptionToken'] records = o.list_identifiers_or_records(metadataPrefix, from_date=from_date, until_date=until_date, rt=resumptionToken, fc_limit=record_limit, include_docs=showDocs ) for ident in records: doc_index += 1 doc_err = False if OAIPMHDocumentResolver.PAYLOAD_ERROR in ident: err_count += 1 doc_err = True log.debug("Payload Error detected, doc_index: {0}, err_count: {1}".format(doc_index, err_count)) if doc_index - err_count == 1: rendered_init = True part = mustache.prefix(**self._initMustache(args=params, req=t_req)) yield h.fixUtf8(self._returnResponse(part, res=t_res)) if doc_err is False and (record_limit is None or doc_index <= record_limit): part = mustache.doc(ident) yield h.fixUtf8(part) elif enable_flow_control: from lr.lib import resumption_token if doc_index - err_count > 0 and doc_index > record_limit: opts = o.list_opts(metadataPrefix, h.convertToISO8601UTC(ident["node_timestamp"]), until_date) opts["startkey_docid"] = ident["doc_ID"] token = resumption_token.get_token(serviceid=service_id, from_date=from_date, until_date=until_date, **opts) part = mustache.resumptionToken(token) yield h.fixUtf8(part) break elif doc_index - err_count == 0 and doc_index > record_limit: opts = o.list_opts(metadataPrefix, h.convertToISO8601UTC(ident["node_timestamp"]), until_date) opts["startkey_docid"] = ident["doc_ID"] payload = resumption_token.get_payload(from_date=from_date, until_date=until_date, **opts) records = o.list_identifiers_or_records(metadataPrefix, from_date=from_date, until_date=until_date, rt=payload, fc_limit=record_limit, include_docs=showDocs ) doc_index = 0 err_count = 0 if doc_index == 0 and err_count == 0: raise NoRecordsMatchError(params['verb'], req=t_req) elif (doc_index - err_count) == 0: raise CannotDisseminateFormatError(params['verb'], req=t_req) else: if enable_flow_control and doc_index <= record_limit: yield h.fixUtf8(mustache.resumptionToken()) yield h.fixUtf8(mustache.suffix()) except oaipmherrors.Error as e: if not rendered_init: from lr.mustache.oaipmh import Error as err_stache err = err_stache() yield h.fixUtf8(self._returnResponse(err.xml(e), res=t_res)) else: from lr.mustache.oaipmh import ErrorOnly as err_stache err = err_stache() yield h.fixUtf8(self._returnResponse(err.xml(e)+mustache.suffix(), res=t_res)) except: log.exception("Unknown Error Occurred")