-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresult.py
More file actions
387 lines (295 loc) · 12.6 KB
/
result.py
File metadata and controls
387 lines (295 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
from abc import ABC
from abc import ABCMeta
from abc import abstractmethod
from enum import Enum
from typing import Optional
from ..constants import SRUResultCountPrecision
from ..diagnostic import SRUDiagnostic
from ..diagnostic import SRUDiagnosticList
from ..xml.writer import SRUXMLStreamWriter
# ---------------------------------------------------------------------------
class SRUAbstractResult(metaclass=ABCMeta):
"""Base class for SRU responses."""
def __init__(self, diagnostics: SRUDiagnosticList) -> None:
if diagnostics is None:
raise TypeError("Implementation error: diagnostics must not be None!")
self.diagnostics = diagnostics
def add_diagnostic(
self, uri: str, details: Optional[str] = None, message: Optional[str] = None
) -> None:
"""Add a non surrogate diagnostic to the response.
Args:
uri: the diagnostic's identifying URI
details: supplementary information available, often
in a format specified by the diagnostic or ``None``
message: human readable message to display to the
end user or ``None``
"""
self.diagnostics.add_diagnostic(uri, details, message)
@property
def has_extra_response_data(self) -> bool:
"""Check, if extra response data should be serialized for
this request. Default implementation is provided for convince
and always returns ``False``.
Returns:
bool: ``True`` if extra response data should be serialized
"""
return False
def write_extra_response_data(self, writer: SRUXMLStreamWriter) -> None:
"""Serialize extra response data for this request. A no-op
default implementation is provided for convince.
Args:
writer: Writer to serialize extra response data
"""
def close(self) -> None:
"""Release this result and free any associated resources.
This method **must not** throw any exceptions.
Calling the method `close` on a result object that
is already closed is a no-op.
"""
# ---------------------------------------------------------------------------
class SRUExplainResult(ABC, SRUAbstractResult):
"""A result set of an ``explain`` operation. A database
implementation may use it implement extensions to the SRU
protocol, i.e. providing extraResponseData.
This class needs to be implemented for the target data source.
See also:
SRU Explain Operation: http://www.loc.gov/standards/sru/explain/
"""
# ---------------------------------------------------------------------------
class SRUScanResultSet(ABC, SRUAbstractResult):
"""A result set of a ``scan`` operation. It is used to iterate
over the term set and provides a method to serialize the terms.
A `SRUScanResultSet` object maintains a cursor pointing to its
current term. Initially the cursor is positioned before the first
term. The `next` method moves the cursor to the next term, and
because it returns ``False`` when there are no more terms in the
`SRUScanResultSet` object, it can be used in a `while` loop to
iterate through the term set.
This class needs to be implemented for the target search engine.
See also:
SRU Scan Operation: http://www.loc.gov/standards/sru/companionSpecs/scan.html
"""
class WhereInList(str, Enum):
"""A flag to indicate the position of the term within the
complete term list."""
FIRST = "first"
"""The first term (**first**)"""
LAST = "last"
"""The last term (**last**)"""
ONLY = "only"
"""The only term (**only**)"""
INNER = "inner"
"""Any other term (**inner**)"""
@abstractmethod
def next_term(self) -> bool:
"""Moves the cursor forward one term from its current
position. A result set cursor is initially positioned before
the first record; the first call to the method `next` makes
the first term the current term; the second call makes the
second term the current term, and so on.
When a call to the `next` method returns ``False``, the
cursor is positioned after the last term.
Returns:
bool: ``True`` if the new current term is valid;
``False`` if there are no more terms
Raises:
`SRUException`: if an error occurred while fetching the
next term
"""
@abstractmethod
def get_value(self) -> str:
"""Get the current term exactly as it appears in the index.
Returns:
str: current term
"""
@abstractmethod
def get_number_of_records(self) -> int:
"""Get the number of records for the current term which would
be matched if the index in the request's `scanClause` was
searched with the term in the `value` field.
Returns:
int: a non-negative number of records or ``-1``, if the
number is unknown.
"""
@abstractmethod
def get_display_term(self) -> Optional[str]:
"""Get the string for the current term to display to the end
user in place of the term itself.
Returns:
str: display string or ``None``
"""
@abstractmethod
def get_WhereInList(self) -> Optional[WhereInList]:
"""Get the flag to indicate the position of the term within
the complete term list.
Returns:
`WhereInList`: position within term list or ``None``
"""
def has_extra_term_data(self) -> bool:
"""Check, if extra term data should be serialized for the
current term. A default implementation is provided for
convince and always returns ``False``.
Returns:
bool: ``True`` if the term has extra term data
Raises:
`StopIteration`: term set is already advanced past all
past terms
See also:
`write_extra_term_data`
"""
return False
@abstractmethod
def write_extra_term_data(self, writer: SRUXMLStreamWriter):
"""Serialize extra term data for the current term. A no-op
default implementation is provided for convince.
Args:
writer: Writer to serialize extra term data for current
term
Raises:
`StopIteration`: term set already advanced past all terms
"""
# ---------------------------------------------------------------------------
class SRUSearchResultSet(ABC, SRUAbstractResult):
"""A result set of a ``searchRetrieve`` operation. It it used to
iterate over the result set and provides a method to serialize
the record in the requested format.
A `SRUSearchResultSet` object maintains a cursor pointing to its
current record. Initially the cursor is positioned before the
first record. The `next` method moves the cursor to the next
record, and because it returns ``False`` when there are no more
records in the `SRUSearchResultSet` object, it can be used in a
`while` loop to iterate through the result set.
This class needs to be implemented for the target search engine.
See also:
* SRU Search Retrieve Operation: http://www.loc.gov/standards/sru/
* SRU 1.1 SR: http://www.loc.gov/standards/sru/sru-1-1.html
* SRU 1.2 SR: http://www.loc.gov/standards/sru/sru-1-2.html
* SRU 2.0 SR: http://www.loc.gov/standards/sru/sru-2-0.html
* Differences SRU 2.0 to SRU 1.2: http://www.loc.gov/standards/sru/differences.html
"""
@abstractmethod
def get_total_record_count(self) -> int:
"""The number of records matched by the query. If the query
fails this must be ``0``. If the search engine cannot
determine the total number of matched by a query, it must
return ``-1``.
Returns:
int: the total number of results or ``0`` if the query
failed or ``-1`` if the search engine cannot
determine the total number of results
"""
@abstractmethod
def get_record_count(self) -> int:
"""The number of records matched by the query but at most as
the number of records requested to be returned
(``maximumRecords`` parameter). If the query fails this must
be ``0``.
Returns:
int: the number of results or ``0`` if the query failed
"""
def get_resultSet_id(self) -> Optional[str]:
"""The result set id of this result. The default
implementation returns ``None``.
Returns:
str: the result set id or ``None`` if not applicable for
this result
"""
return None
def get_resultSet_TTL(self) -> int:
"""The result set time to live. In SRU 2.0 it will be
serialized as ``<resultSetTTL>`` element; in SRU 1.2 as
``<resultSetIdleTime>`` element.The default implementation
returns ``-1``.
Returns:
int: the result set time to live or ``-1`` if not
applicable for this result
"""
return -1
def get_result_count_precision(self) -> Optional[SRUResultCountPrecision]:
"""(SRU 2.0) Indicate the accuracy of the result count
reported by total number of records that matched the query.
Default implementation returns ``None``.
Returns:
Optional[SRUResultCountPrecision]: the result count
precision or ``None`` if not applicable for this
result
See also:
`SRUResultCountPrecision`
"""
return None
@abstractmethod
def get_record_schema_identifier(self) -> str:
"""The record schema identifier in which the records are
returned (``recordSchema`` parameter).
Returns:
str: the record schema identifier
"""
@abstractmethod
def next_record(self) -> bool:
"""Moves the cursor forward one record from its current
position. A `SRUSearchResultSet` cursor is initially
positioned before the first record; the first call to the
method `next` makes the first record the current record; the
second call makes the second record the current record, and
so on.
When a call to the `next` method returns ``False``, the
cursor is positioned after the last record.
Returns:
bool: ``True`` if the new current record is valid;
``False`` if there are no more records
Raises:
`SRUException`: if an error occurred while fetching the
next record
"""
@abstractmethod
def get_record_identifier(self) -> Optional[str]:
"""An identifier for the current record by which it can
unambiguously be retrieved in a subsequent operation.
Returns:
str: identifier for the record or ``None`` of none is
available
Raises:
`StopIteration`: result set is past all records
"""
def get_surrogate_diagnostic(self) -> Optional[SRUDiagnostic]:
"""Get surrogate diagnostic for current record. If this
method returns a diagnostic, the `write_record method will
not be called. The default implementation returns ``None``.
Returns:
Optional[SRUDiagnostic]: a surrogate diagnostic or
``None``
"""
return None
@abstractmethod
def write_record(self, writer: SRUXMLStreamWriter) -> None:
"""Serialize the current record in the requested format.
Args:
writer: Writer to serialize current record
Raises:
`StopIteration`: result set is past all records
"""
@property
def has_extra_record_data(self) -> bool:
"""Check, if extra record data should be serialized for the
current record. The default implementation returns ``False``.
Returns:
bool: ``True`` if the record has extra record data
Raises:
`StopIteration`: result set is past all records
See also:
`write_extra_record_data`
"""
return False
def write_extra_record_data(self, writer: SRUXMLStreamWriter) -> None:
"""Serialize extra record data for the current record. A
no-op default implementation is provided for convince.
Args:
writer: Writer to serialize extra record data for current
record
Raises:
`StopIteration`: result set past already advanced past
all records
"""
pass
# ---------------------------------------------------------------------------