From 02a41f71e303e9fa90fae856b52bb26e8c0139fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriele=20H=C3=B6fler?= Date: Tue, 18 Jun 2019 11:41:51 +0200 Subject: [PATCH] Move namespaces dict to __init__ --- sru/almasru.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/sru/almasru.py b/sru/almasru.py index f756b38..ab59684 100644 --- a/sru/almasru.py +++ b/sru/almasru.py @@ -65,6 +65,11 @@ class RecordRetriever: self.url_query = {'version': '1.2', 'operation': 'searchRetrieve', 'recordSchema': xml_format} + self.namespaces = { + 'srw': 'http://www.loc.gov/zing/srw/', + 'diag': 'http://www.loc.gov/zing/srw/diagnostic/', + 'marc': 'http://www.loc.gov/MARC21/slim' + } def get_single(self, alma_query: str) -> Element: """Return one MARC XML record for a specific query. @@ -84,7 +89,7 @@ class RecordRetriever: self._raise_errors_single_records(sru_xml) - xml_list = self._extract_marc_xml(sru_xml, namespaces) + xml_list = self._extract_marc_xml(sru_xml) return xml_list[0] def _create_url(self, alma_query: str) -> str: @@ -99,13 +104,7 @@ class RecordRetriever: def _raise_errors_single_records(self, sru_xml: Element): - namespaces = { - 'srw': 'http://www.loc.gov/zing/srw/', - 'diag': 'http://www.loc.gov/zing/srw/diagnostic/', - 'marc': 'http://www.loc.gov/MARC21/slim' - } - - num_recs = sru_xml.find("srw:numberOfRecords", namespaces) + num_recs = sru_xml.find("srw:numberOfRecords", self.namespaces) if ( num_recs is not None and @@ -120,14 +119,16 @@ class RecordRetriever: raise MultiRecord('More than one record found' 'while exactly one was expected!') - elif sru_xml.find("srw:diagnostics", namespaces): + def _raise_errors_sru(self, sru_xml: Element): + + if sru_xml.find("srw:diagnostics", self.namespaces): xpath = "srw:diagnostics/diag:diagnostic/diag:message" - sru_message = sru_xml.find(xpath, namespaces).text + sru_message = sru_xml.find(xpath, self.namespaces).text raise SruDiagnostics(sru_message) - def _extract_marc_xml(self, sru_xml, namespaces): + def _extract_marc_xml(self, sru_xml): record_xpath = 'srw:records/srw:record//marc:record' - marc_xml = sru_xml.findall(record_xpath, namespaces) + marc_xml = sru_xml.findall(record_xpath, self.namespaces) return marc_xml def by_marc_009(self, marc_009: str) -> Element: -- GitLab