本文整理汇总了Python中meresco.components.sru.SruHandler.addObserver方法的典型用法代码示例。如果您正苦于以下问题:Python SruHandler.addObserver方法的具体用法?Python SruHandler.addObserver怎么用?Python SruHandler.addObserver使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类meresco.components.sru.SruHandler
的用法示例。
在下文中一共展示了SruHandler.addObserver方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testValidXml
# 需要导入模块: from meresco.components.sru import SruHandler [as 别名]
# 或者: from meresco.components.sru.SruHandler import addObserver [as 别名]
def testValidXml(self):
component = SruParser()
sruHandler = SruHandler()
component.addObserver(sruHandler)
observer = CallTrace('observer')
sruHandler.addObserver(observer)
response = Response(total=2, hits=[Hit('id0'), Hit('id1')])
def executeQuery(**kwargs):
raise StopIteration(response)
yield
def retrieveData(**kwargs):
raise StopIteration('<bike/>')
yield
observer.methods['executeQuery'] = executeQuery
observer.returnValues['echoedExtraRequestData'] = (f for f in [])
observer.returnValues['extraResponseData'] = (f for f in [])
observer.methods['extraRecordData'] = lambda hit: (f for f in [])
observer.methods['retrieveData'] = retrieveData
result = ''.join(compose(component.handleRequest(arguments={'version':['1.1'], 'query': ['aQuery'], 'operation':['searchRetrieve']})))
header, body = result.split('\r\n'*2)
assertValid(body, join(schemasPath, 'srw-types1.2.xsd'))
self.assertTrue('<bike/>' in body, body)
result = ''.join(compose(component.handleRequest(arguments={'version':['1.1'], 'operation':['searchRetrieve']})))
header, body = result.split('\r\n'*2)
assertValid(body, join(schemasPath, 'srw-types1.2.xsd'))
self.assertTrue('diagnostic' in body, body)
示例2: testNextRecordPosition
# 需要导入模块: from meresco.components.sru import SruHandler [as 别名]
# 或者: from meresco.components.sru.SruHandler import addObserver [as 别名]
def testNextRecordPosition(self):
observer = CallTrace(emptyGeneratorMethods=['additionalDiagnosticDetails'])
response = Response(total=100, hits=hitsRange(11, 26))
def executeQuery(**kwargs):
raise StopIteration(response)
yield
def retrieveData(**kwargs):
raise StopIteration('record')
yield
observer.methods['executeQuery'] = executeQuery
observer.methods['retrieveData'] = retrieveData
observer.methods['extraResponseData'] = lambda *a, **kw: (x for x in 'extraResponseData')
observer.methods['echoedExtraRequestData'] = lambda *a, **kw: (x for x in 'echoedExtraRequestData')
observer.methods['extraRecordData'] = lambda hit: (f for f in [])
component = SruHandler()
component.addObserver(observer)
arguments = dict(startRecord=11, maximumRecords=15, query='query', recordPacking='string', recordSchema='schema')
result = "".join(compose(component.searchRetrieve(sruArguments=arguments, **arguments)))
self.assertTrue("<srw:nextRecordPosition>26</srw:nextRecordPosition>" in result, result)
executeCqlCallKwargs = observer.calledMethods[0].kwargs
self.assertEquals(10, executeCqlCallKwargs['start']) # SRU is 1 based
self.assertEquals(25, executeCqlCallKwargs['stop'])
示例3: testQueryTimeInExtraResponse
# 需要导入模块: from meresco.components.sru import SruHandler [as 别名]
# 或者: from meresco.components.sru.SruHandler import addObserver [as 别名]
def testQueryTimeInExtraResponse(self):
handler = SruHandler(includeQueryTimes=True)
observer = CallTrace('observer', emptyGeneratorMethods=['echoedExtraRequestData', 'extraResponseData'])
times = [1, 2.5, 3.5]
def timeNow():
return times.pop(0)
handler._timeNow = timeNow
def executeQuery(**kwargs):
response = Response(total=0, hits=[])
response.queryTime=5
raise StopIteration(response)
yield
observer.methods['executeQuery'] = executeQuery
handler.addObserver(observer)
arguments = dict(startRecord=11, maximumRecords=15, query='query', recordPacking='string', recordSchema='schema')
result = "".join(compose(handler.searchRetrieve(sruArguments=arguments, **arguments)))
sruResponse = parse(StringIO(result))
extraResponseData = sruResponse.xpath('/srw:searchRetrieveResponse/srw:extraResponseData', namespaces={'srw':"http://www.loc.gov/zing/srw/"})[0]
self.assertEqualsWS("""<srw:extraResponseData %(xmlns_srw)s %(xmlns_diag)s %(xmlns_xcql)s %(xmlns_dc)s %(xmlns_meresco_srw)s>
<querytimes xmlns="http://meresco.org/namespace/timing">
<sruHandling>PT2.500S</sruHandling>
<sruQueryTime>PT1.500S</sruQueryTime>
<index>PT0.005S</index>
</querytimes>
</srw:extraResponseData>""" % namespaces, lxmltostring(extraResponseData))
queryTimes = lxmltostring(extraResponseData.xpath('//ti:querytimes', namespaces={'ti':"http://meresco.org/namespace/timing"})[0])
assertValid(queryTimes, join(schemasPath, 'timing-20120827.xsd'))
self.assertEquals(['executeQuery', 'echoedExtraRequestData', 'extraResponseData', 'handleQueryTimes'], observer.calledMethodNames())
self.assertEquals({'sru': Decimal("2.500"), 'queryTime': Decimal("1.500"), 'index': Decimal("0.005")}, observer.calledMethods[3].kwargs)
示例4: testCollectLog
# 需要导入模块: from meresco.components.sru import SruHandler [as 别名]
# 或者: from meresco.components.sru.SruHandler import addObserver [as 别名]
def testCollectLog(self):
handler = SruHandler(enableCollectLog=True)
observer = CallTrace('observer', emptyGeneratorMethods=['echoedExtraRequestData', 'extraResponseData'])
__callstack_var_logCollector__ = dict()
times = [1, 2.5, 3.5]
def timeNow():
return times.pop(0)
handler._timeNow = timeNow
def executeQuery(**kwargs):
response = Response(total=0, hits=[])
response.queryTime=5
raise StopIteration(response)
yield
observer.methods['executeQuery'] = executeQuery
handler.addObserver(observer)
arguments = dict(startRecord=11, maximumRecords=15, query='query', recordPacking='string', recordSchema='schema')
consume(handler.searchRetrieve(sruArguments=arguments, **arguments))
self.assertEquals({
'sru': {
'handlingTime': [Decimal('2.500')],
'queryTime': [Decimal('1.500')],
'indexTime': [Decimal('0.005')],
'numberOfRecords': [0],
'arguments': [{
'startRecord': 11,
'query': 'query',
'recordPacking': 'string',
'maximumRecords': 15,
'recordSchema': 'schema',
}],
}
}, __callstack_var_logCollector__)
示例5: testCollectLogWhenIndexRaisesError
# 需要导入模块: from meresco.components.sru import SruHandler [as 别名]
# 或者: from meresco.components.sru.SruHandler import addObserver [as 别名]
def testCollectLogWhenIndexRaisesError(self):
handler = SruHandler(enableCollectLog=True)
observer = CallTrace('observer', emptyGeneratorMethods=['echoedExtraRequestData', 'extraResponseData', 'additionalDiagnosticDetails'])
__callstack_var_logCollector__ = dict()
times = [1]
def timeNow():
return times.pop(0)
handler._timeNow = timeNow
def executeQuery(**kwargs):
raise Exception('Sorry')
yield
observer.methods['executeQuery'] = executeQuery
handler.addObserver(observer)
arguments = dict(startRecord=11, maximumRecords=15, query='query', recordPacking='string', recordSchema='schema')
consume(handler.searchRetrieve(sruArguments=arguments, **arguments))
self.assertEquals({
'sru': {
'arguments': [{
'startRecord': 11,
'query': 'query',
'recordPacking': 'string',
'maximumRecords': 15,
'recordSchema': 'schema',
}],
}
}, __callstack_var_logCollector__)
示例6: testExtraResponseDataHandlerNoData
# 需要导入模块: from meresco.components.sru import SruHandler [as 别名]
# 或者: from meresco.components.sru.SruHandler import addObserver [as 别名]
def testExtraResponseDataHandlerNoData(self):
class TestHandler:
def extraResponseData(self, *args, **kwargs):
return (f for f in [])
component = SruHandler()
component.addObserver(TestHandler())
result = "".join(list(component._writeExtraResponseData(cqlAbstractSyntaxTree=None, **MOCKDATA)))
self.assertEquals('' , result)
示例7: testExceptionInWriteExtraRecordData
# 需要导入模块: from meresco.components.sru import SruHandler [as 别名]
# 或者: from meresco.components.sru.SruHandler import addObserver [as 别名]
def testExceptionInWriteExtraRecordData(self):
class RaisesException(object):
def extraResponseData(self, *args, **kwargs):
raise Exception("Test Exception")
component = SruHandler()
component.addObserver(RaisesException())
result = "".join(compose(component._writeExtraResponseData(cqlAbstractSyntaxTree=None, **MOCKDATA)))
self.assertTrue("<uri>info://srw/diagnostics/1/1</uri>" in result)
self.assertTrue("<message>General System Error</message>" in result)
self.assertTrue("<details>Test Exception</details>" in result)
示例8: testExtraResponseDataWithTermDrilldown
# 需要导入模块: from meresco.components.sru import SruHandler [as 别名]
# 或者: from meresco.components.sru.SruHandler import addObserver [as 别名]
def testExtraResponseDataWithTermDrilldown(self):
sruHandler = SruHandler()
sruTermDrilldown = SRUTermDrilldown()
drilldownData = [
{'fieldname': 'field0', 'terms': [{'term': 'value0_0', 'count': 14}]},
{'fieldname': 'field1', 'terms': [{'term': 'value1_0', 'count': 13}, {'term': 'value1_1', 'count': 11}]},
{'fieldname': 'field2', 'terms': [{'term': 'value2_0', 'count': 3}, {'term': 'value2_1', 'count': 2}, {'term': 'value2_2', 'count': 1}]}
]
sruHandler.addObserver(sruTermDrilldown)
result = "".join(sruHandler._writeExtraResponseData(drilldownData=drilldownData, sruArguments={}, **MOCKDATA))
self.assertEqualsWS("""<srw:extraResponseData><dd:drilldown\n xmlns:dd="http://meresco.org/namespace/drilldown"\n xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\n xsi:schemaLocation="http://meresco.org/namespace/drilldown http://meresco.org/files/xsd/drilldown-20070730.xsd"><dd:term-drilldown><dd:navigator name="field0"><dd:item count="14">value0_0</dd:item></dd:navigator><dd:navigator name="field1"><dd:item count="13">value1_0</dd:item><dd:item count="11">value1_1</dd:item></dd:navigator><dd:navigator name="field2"><dd:item count="3">value2_0</dd:item><dd:item count="2">value2_1</dd:item><dd:item count="1">value2_2</dd:item></dd:navigator></dd:term-drilldown></dd:drilldown></srw:extraResponseData>""" , result)
示例9: testExceptionInWriteRecordData
# 需要导入模块: from meresco.components.sru import SruHandler [as 别名]
# 或者: from meresco.components.sru.SruHandler import addObserver [as 别名]
def testExceptionInWriteRecordData(self):
observer = CallTrace(emptyGeneratorMethods=['additionalDiagnosticDetails'])
def retrieveData(**kwargs):
raise Exception("Test Exception")
yield
observer.methods["retrieveData"] = retrieveData
component = SruHandler()
component.addObserver(observer)
result = "".join(list(compose(component._writeRecordData(recordPacking="string", recordSchema="schema", recordId="ID"))))
self.assertTrue("<uri>info://srw/diagnostics/1/1</uri>" in result)
self.assertTrue("<message>General System Error</message>" in result)
self.assertTrue("<details>Test Exception</details>" in result)
示例10: testDiagnosticOnExecuteCql
# 需要导入模块: from meresco.components.sru import SruHandler [as 别名]
# 或者: from meresco.components.sru.SruHandler import addObserver [as 别名]
def testDiagnosticOnExecuteCql(self):
with stderr_replaced():
class RaisesException(object):
def executeQuery(self, *args, **kwargs):
raise Exception("Test Exception")
component = SruHandler()
component.addObserver(RaisesException())
arguments = dict(startRecord=11, maximumRecords=15, query='query', recordPacking='string', recordSchema='schema')
result = parse(StringIO("".join(compose(component.searchRetrieve(sruArguments=arguments, **arguments)))))
diagnostic = result.xpath("/srw:searchRetrieveResponse/srw:diagnostics/diag:diagnostic", namespaces=namespaces)
self.assertEquals(1, len(diagnostic))
self.assertEquals(["info://srw/diagnostics/1/48"], diagnostic[0].xpath("diag:uri/text()", namespaces=namespaces))
self.assertEquals(["Query Feature Unsupported"], diagnostic[0].xpath("diag:message/text()", namespaces=namespaces))
self.assertEquals(["Test Exception"], diagnostic[0].xpath("diag:details/text()", namespaces=namespaces))
示例11: testDiagnosticWarning
# 需要导入模块: from meresco.components.sru import SruHandler [as 别名]
# 或者: from meresco.components.sru.SruHandler import addObserver [as 别名]
def testDiagnosticWarning(self):
sruArguments = {'version':'1.2', 'operation':'searchRetrieve', 'recordSchema':'schema', 'recordPacking':'xml', 'query':'field=value', 'startRecord':1, 'maximumRecords':2, }
queryArguments = {'version':'1.2', 'operation':'searchRetrieve', 'recordSchema':'schema', 'recordPacking':'xml', 'query':'field=value', 'startRecord':1, 'maximumRecords':2}
observer = CallTrace(emptyGeneratorMethods=['additionalDiagnosticDetails'])
response = Response(total=100, hits=[Hit('<aap&noot>'), Hit('vuur')])
def executeQuery(**kwargs):
raise StopIteration(response)
yield
observer.methods['executeQuery'] = executeQuery
retrieveDataCalls = []
def retrieveData(identifier, name):
retrieveDataCalls.append(1)
raise StopIteration("<MOCKED_WRITTEN_DATA>%s-%s</MOCKED_WRITTEN_DATA>" % (xmlEscape(identifier), name))
yield
observer.retrieveData = retrieveData
observer.methods['extraResponseData'] = lambda *a, **kw: (x for x in 'extraResponseData')
observer.methods['echoedExtraRequestData'] = lambda *a, **kw: (x for x in 'echoedExtraRequestData')
observer.methods['extraRecordData'] = lambda hit: (f for f in [])
component = SruHandler()
component.addObserver(observer)
result = "".join(compose(component.searchRetrieve(sruArguments=sruArguments, diagnostics=[(998, 'Diagnostic 998', 'The <tag> message'), (999, 'Diagnostic 999', 'Some message')], **queryArguments)))
response = parse(StringIO(result))
self.assertEquals([t % namespaces for t in [
'{%(srw)s}version',
'{%(srw)s}numberOfRecords',
'{%(srw)s}records',
'{%(srw)s}nextRecordPosition',
'{%(srw)s}echoedSearchRetrieveRequest',
'{%(srw)s}diagnostics',
'{%(srw)s}extraResponseData',
]], [t.tag for t in xpath(response, '//srw:searchRetrieveResponse/*')])
diagnostics = [{'uri': xpath(d, 'diag:uri/text()')[0],
'details': xpath(d, 'diag:details/text()')[0],
'message': xpath(d, 'diag:message/text()')[0]} for d in
xpath(response, '/srw:searchRetrieveResponse/srw:diagnostics/diag:diagnostic')]
self.assertEquals([
{'uri': 'info://srw/diagnostics/1/998', 'message': 'Diagnostic 998', 'details': 'The <tag> message'},
{'uri': 'info://srw/diagnostics/1/999', 'message': 'Diagnostic 999', 'details': 'Some message'},
], diagnostics)
示例12: testExtraResponseDataHandlerWithData
# 需要导入模块: from meresco.components.sru import SruHandler [as 别名]
# 或者: from meresco.components.sru.SruHandler import addObserver [as 别名]
def testExtraResponseDataHandlerWithData(self):
argsUsed = []
kwargsUsed = {}
class TestHandler:
def extraResponseData(self, *args, **kwargs):
argsUsed.append(args)
kwargsUsed.update(kwargs)
return (f for f in ["<someD", "ata/>"])
component = SruHandler()
component.addObserver(TestHandler())
result = "".join(list(component._writeExtraResponseData(cqlAbstractSyntaxTree=None, **MOCKDATA)))
self.assertEquals('<srw:extraResponseData><someData/></srw:extraResponseData>' , result)
self.assertEquals([()], argsUsed)
self.assertEquals(None, kwargsUsed['cqlAbstractSyntaxTree'])
self.assertEquals(MOCKDATA['queryTime'], kwargsUsed['queryTime'])
self.assertEquals(MOCKDATA['response'], kwargsUsed['response'])
示例13: testExtraRecordDataOldStyle
# 需要导入模块: from meresco.components.sru import SruHandler [as 别名]
# 或者: from meresco.components.sru.SruHandler import addObserver [as 别名]
def testExtraRecordDataOldStyle(self):
queryArguments = {'version':'1.2', 'operation':'searchRetrieve', 'recordSchema':'schema', 'recordPacking':'xml', 'query':'field=value', 'startRecord':1, 'maximumRecords':2}
sruArguments = {'version':'1.2', 'operation':'searchRetrieve', 'recordSchema':'schema', 'recordPacking':'xml', 'query':'field=value', 'startRecord':1, 'maximumRecords':2, 'x-recordSchema':['extra', 'evenmore']}
observer = CallTrace()
response = Response(total=100, hits=[Hit('11')])
def executeQuery(**kwargs):
raise StopIteration(response)
yield
observer.methods['executeQuery'] = executeQuery
retrieveDataCalls = []
def retrieveData(identifier, name):
retrieveDataCalls.append(1)
raise StopIteration("<MOCKED_WRITTEN_DATA>%s-%s</MOCKED_WRITTEN_DATA>" % (identifier, name))
yield
observer.retrieveData = retrieveData
observer.methods['extraResponseData'] = lambda *a, **kw: (x for x in 'extraResponseData')
observer.methods['echoedExtraRequestData'] = lambda *a, **kw: (x for x in 'echoedExtraRequestData')
observer.methods['extraRecordData'] = lambda hit: (f for f in [])
component = SruHandler(extraRecordDataNewStyle=False)
component.addObserver(observer)
result = "".join(compose(component.searchRetrieve(sruArguments=sruArguments, **queryArguments)))
strippedResult = result[result.index('<srw:record>'):result.index('</srw:records>')]
self.assertEqualsWS("""<srw:record>
<srw:recordSchema>schema</srw:recordSchema>
<srw:recordPacking>xml</srw:recordPacking>
<srw:recordIdentifier>11</srw:recordIdentifier>
<srw:recordData>
<MOCKED_WRITTEN_DATA>11-schema</MOCKED_WRITTEN_DATA>
</srw:recordData>
<srw:extraRecordData>
<recordData recordSchema="extra">
<MOCKED_WRITTEN_DATA>11-extra</MOCKED_WRITTEN_DATA>
</recordData>
<recordData recordSchema="evenmore">
<MOCKED_WRITTEN_DATA>11-evenmore</MOCKED_WRITTEN_DATA>
</recordData>
</srw:extraRecordData>
</srw:record>""", strippedResult)
示例14: testGetDataWithAdapter
# 需要导入模块: from meresco.components.sru import SruHandler [as 别名]
# 或者: from meresco.components.sru.SruHandler import addObserver [as 别名]
def testGetDataWithAdapter(self):
observer = CallTrace(returnValues=dict(getData='<record/>'))
adapter = RetrieveToGetDataAdapter()
handler = SruHandler()
handler.addObserver(adapter)
adapter.addObserver(observer)
response = Response(total=100, hits=hitsRange(1, 3))
def executeQuery(**kwargs):
raise StopIteration(response)
yield
observer.methods['executeQuery'] = executeQuery
observer.methods['extraResponseData'] = lambda *a, **kw: (x for x in 'extraResponseData')
observer.methods['echoedExtraRequestData'] = lambda *a, **kw: (x for x in 'echoedExtraRequestData')
observer.methods['extraRecordData'] = lambda hit: (f for f in [])
response = asString(handler.searchRetrieve(query="word", recordSchema='schema', recordPacking='string', maximumRecords=2, sruArguments={}))
self.assertEquals(['getData', 'getData'], observer.calledMethodNames()[1:4:2])
self.assertTrue('<srw:recordData><record/></srw:recordData>' in response, response)
self.assertEquals({'identifier': '1', 'name': 'schema'}, observer.calledMethods[1].kwargs)
self.assertEquals({'identifier': '2', 'name': 'schema'}, observer.calledMethods[3].kwargs)
示例15: testEchoedSearchRetrieveRequestWithExtraRequestData
# 需要导入模块: from meresco.components.sru import SruHandler [as 别名]
# 或者: from meresco.components.sru.SruHandler import addObserver [as 别名]
def testEchoedSearchRetrieveRequestWithExtraRequestData(self):
sruArguments = {'version':'1.1', 'operation':'searchRetrieve', 'query':'query >= 3', 'recordSchema':'schema', 'recordPacking':'string', 'x-term-drilldown':['field0,field1']}
observer = CallTrace('ExtraRequestData')
observer.methods['echoedExtraRequestData'] = lambda *a, **kw: (x for x in '<some>extra request data</some>')
component = SruHandler()
component.addObserver(SRUTermDrilldown())
component.addObserver(observer)
result = "".join(list(component._writeEchoedSearchRetrieveRequest(sruArguments=sruArguments)))
drilldownRequestData = DRILLDOWN_HEADER \
+ """<dd:term-drilldown>field0,field1</dd:term-drilldown>"""\
+ DRILLDOWN_FOOTER
self.assertEqualsWS("""<srw:echoedSearchRetrieveRequest>
<srw:version>1.1</srw:version>
<srw:query>query >= 3</srw:query>
<srw:recordPacking>string</srw:recordPacking>
<srw:recordSchema>schema</srw:recordSchema>
<srw:extraRequestData>%s<some>extra request data</some></srw:extraRequestData>
</srw:echoedSearchRetrieveRequest>""" % drilldownRequestData, result)