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
6bcdb6f0
Commit
6bcdb6f0
authored
6 years ago
by
gabriele-h
Browse files
Options
Downloads
Patches
Plain Diff
Refactor: More functionality in private methods
parent
2543a5b2
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
+31
-37
31 additions, 37 deletions
sru/almasru.py
with
31 additions
and
37 deletions
sru/almasru.py
+
31
−
37
View file @
6bcdb6f0
...
...
@@ -88,20 +88,15 @@ class RecordRetriever:
:raises SruDiagnostics: if SRU returns an error
"""
startrecord
=
1
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
)
first_page
=
self
.
_get_first_page
(
alma_query
)
num_recs
=
self
.
_get_num_records
(
first_page
)
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
]
xml
=
self
.
_extract_marc_xml
(
first_page
)
return
xml
[
0
]
def
get_multiple
(
self
,
alma_query
:
str
)
->
list
:
"""
Return list of
o
MARC XML records for a specific query.
"""
Return list of MARC XML records for a specific query.
:param alma_query: URI-parameter query for SRU request
:return: XML as list of etree-Elements
...
...
@@ -110,30 +105,32 @@ class RecordRetriever:
:raises SruDiagnostics: if SRU returns an error
"""
startrecord
=
1
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
)
self
.
_raise_error_too_many_recs
(
num_recs
)
num_pages
=
int
(
num_recs
.
text
)
//
10
first_page
=
self
.
_get_first_page
(
alma_query
)
num_recs
=
self
.
_get_num_records
(
first_page
)
xml_list
=
self
.
_extract_marc_xml
(
sru_xml
)
xml_list
=
[]
for
i
in
range
(
num_pages
):
startrecord
=
(
i
+
1
)
*
10
+
1
for
startrecord
in
range
(
1
,
int
(
num_recs
),
10
):
sru_url
=
self
.
_create_url
(
alma_query
,
startrecord
)
sru_xml
=
self
.
_response_to_xml
(
sru_url
)
self
.
_raise_error_sru
(
sru_xml
)
xml_list
.
extend
(
self
.
_extract_marc_xml
(
sru_xml
))
return
xml_list
def
_get_first_page
(
self
,
alma_query
):
sru_url
=
self
.
_create_url
(
alma_query
,
1
)
sru_xml
=
self
.
_response_to_xml
(
sru_url
)
return
sru_xml
def
_get_num_records
(
self
,
first_page
):
num_recs_element
=
first_page
.
find
(
"
srw:numberOfRecords
"
,
self
.
namespaces
)
self
.
_raise_error_sru
(
first_page
)
self
.
_raise_error_no_match
(
num_recs_element
)
self
.
_raise_error_too_many_recs
(
num_recs_element
)
num_recs
=
int
(
num_recs_element
.
text
)
return
num_recs
def
_create_url
(
self
,
alma_query
:
str
,
startrecord
:
int
)
->
str
:
self
.
url_query
[
'
startRecord
'
]
=
str
(
startrecord
)
...
...
@@ -153,26 +150,23 @@ class RecordRetriever:
return
xml
@staticmethod
def
_raise_error_unique
(
num_recs
:
Eleme
nt
):
def
_raise_error_unique
(
num_recs
:
i
nt
):
if
(
num_recs
is
not
None
and
int
(
num_recs
.
text
)
>
1
):
raise
NotUnique
(
'
More than one record found
'
'
while exactly one was expected.
'
)
if
num_recs
>
1
:
raise
NotUnique
(
'
More than one record found while exactly one was expected.
'
)
@staticmethod
def
_raise_error_no_match
(
num_recs
:
Element
):
def
_raise_error_no_match
(
num_recs
_element
:
Element
):
if
(
num_recs
is
not
None
and
num_recs
.
text
==
'
0
'
):
if
(
num_recs
_element
is
not
None
and
num_recs
_element
.
text
==
'
0
'
):
raise
NoRecord
(
'
No matching records found.
'
)
@staticmethod
def
_raise_error_too_many_recs
(
num_recs
:
Element
):
def
_raise_error_too_many_recs
(
num_recs
_element
:
Element
):
if
int
(
num_recs
.
text
)
>
10000
:
raise
TooManyRecords
(
'
SRU searches are limited to 10.000
'
'
records.
'
)
if
int
(
num_recs_element
.
text
)
>
10000
:
raise
TooManyRecords
(
'
SRU searches are limited to 10.000 records.
'
)
def
_raise_error_sru
(
self
,
sru_xml
:
Element
):
...
...
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