From 6bcdb6f00f5952da3c8a275ee3c0b41ef2106d46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriele=20H=C3=B6fler?= Date: Fri, 26 Jul 2019 12:03:52 +0200 Subject: [PATCH] Refactor: More functionality in private methods --- sru/almasru.py | 68 +++++++++++++++++++++++--------------------------- 1 file changed, 31 insertions(+), 37 deletions(-) diff --git a/sru/almasru.py b/sru/almasru.py index 6e5be4c..9bdadfa 100644 --- a/sru/almasru.py +++ b/sru/almasru.py @@ -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 ofo 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: Element): + def _raise_error_unique(num_recs: int): - 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): -- GitLab