Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
catalogue
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
labs-team
catalogue
Commits
0eda5924
Commit
0eda5924
authored
6 years ago
by
gabriele-h
Browse files
Options
Downloads
Patches
Plain Diff
More granular errors, start get_multiple
parent
a4e2341a
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
sru/almasru.py
+53
-17
53 additions, 17 deletions
sru/almasru.py
with
53 additions
and
17 deletions
sru/almasru.py
+
53
−
17
View file @
0eda5924
...
...
@@ -82,19 +82,52 @@ class RecordRetriever:
:raises SruDiagnostics: if SRU returns an error
"""
startrecord
=
'
0
'
startrecord
=
0
sru_url
=
self
.
_create_url
(
alma_query
,
startrecord
)
sru_xml
=
self
.
_response_to_xml
(
sru_url
)
self
.
_raise_errors_unique_record
(
sru_xml
)
num_recs
=
sru_xml
.
find
(
"
srw:numberOfRecords
"
,
self
.
namespaces
)
self
.
_raise_error_unique
(
num_recs
)
self
.
_raise_error_no_match
(
num_recs
)
self
.
_raise_error_sru
(
sru_xml
)
xml_list
=
self
.
_extract_marc_xml
(
sru_xml
)
return
xml_list
[
0
]
def
_create_url
(
self
,
alma_query
:
str
,
startrecord
:
str
)
->
str
:
def
get_multiple
(
self
,
alma_query
:
str
)
->
list
:
"""
Return one MARC XML record for a specific query.
:param alma_query: URI-parameter query for SRU request
:return: XML as list of etree-Elements
:rtype list
:raises NoRecord: if no record was returned by SRU
:raises SruDiagnostics: if SRU returns an error
"""
startrecord
=
0
sru_url
=
self
.
_create_url
(
alma_query
,
startrecord
)
sru_xml
=
self
.
_response_to_xml
(
sru_url
)
num_recs
=
sru_xml
.
find
(
"
srw:numberOfRecords
"
,
self
.
namespaces
)
self
.
_raise_error_no_match
(
num_recs
)
self
.
_raise_error_sru
(
sru_xml
)
num_pages
=
num_recs
//
10
xml_list
=
self
.
_extract_marc_xml
(
sru_xml
)
for
i
in
range
(
num_pages
):
startrecord
=
i
*
10
sru_url
=
self
.
_create_url
(
alma_query
,
startrecord
)
sru_xml
=
self
.
_response_to_xml
(
sru_url
)
xml_list
.
append
(
self
.
_extract_marc_xml
(
sru_xml
))
return
xml_list
def
_create_url
(
self
,
alma_query
:
str
,
startrecord
:
int
)
->
str
:
self
.
url_query
[
'
startRecord
'
]
=
startrecord
self
.
url_query
[
'
startRecord
'
]
=
str
(
startrecord
)
self
.
url_query
[
'
maximumRecords
'
]
=
'
1
'
self
.
url_query
[
'
query
'
]
=
alma_query
url_query_string
=
parse
.
urlencode
(
self
.
url_query
)
...
...
@@ -102,27 +135,30 @@ class RecordRetriever:
return
sru_url
def
_response_to_xml
(
self
,
sru_url
):
@staticmethod
def
_response_to_xml
(
url
):
response
=
request
.
urlopen
(
sru_
url
).
read
()
sru_
xml
=
fromstring
(
response
)
response
=
request
.
urlopen
(
url
).
read
()
xml
=
fromstring
(
response
)
return
sru_
xml
return
xml
def
_raise_errors_unique_record
(
self
,
sru_xml
:
Element
):
num_recs
=
sru_xml
.
find
(
"
srw:numberOfRecords
"
,
self
.
namespaces
)
@staticmethod
def
_raise_error_unique
(
num_recs
:
Element
):
if
(
num_recs
is
not
None
and
num_recs
.
text
==
'
0
'
):
raise
NoRecord
(
'
No matching records found!
'
)
elif
(
num_recs
is
not
None
and
num_recs
.
text
>
'
1
'
):
raise
MultiRecord
(
'
More than one record found
'
'
while exactly one was expected!
'
)
def
_raise_errors_sru
(
self
,
sru_xml
:
Element
):
@staticmethod
def
_raise_error_no_match
(
num_recs
:
Element
):
if
(
num_recs
is
not
None
and
num_recs
.
text
==
'
0
'
):
raise
NoRecord
(
'
No matching records found!
'
)
def
_raise_error_sru
(
self
,
sru_xml
:
Element
):
if
sru_xml
.
find
(
"
srw:diagnostics
"
,
self
.
namespaces
):
xpath
=
"
srw:diagnostics/diag:diagnostic/diag:message
"
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment