Skip to content

Commit

Permalink
Fix linting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Oderbolz committed May 20, 2020
1 parent 8d551c3 commit 07bc852
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint_python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ jobs:
pip install -r requirements.txt
pip install -r test-requirements.txt
- run: flake8 sruthi --count --show-source --statistics
- run: flake8 . --count --show-source --statistics
- run: pytest
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[flake8]
max-complexity = 10
4 changes: 2 additions & 2 deletions sruthi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '0.0.1'

from .errors import (SruthiError, ServerIncompatibleError, SruError, NoMoreRecordsError)
from .sru import searchretrieve, explain
from .errors import (SruthiError, ServerIncompatibleError, SruError, NoMoreRecordsError) # noqa
from .sru import searchretrieve, explain # noqa
15 changes: 10 additions & 5 deletions sruthi/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from . import xmlparse
from . import metadata


class Client(object):
def __init__(self, url=None, page_size=100):
self.session = requests.Session()
Expand Down Expand Up @@ -34,7 +35,6 @@ def explain(self):
params = {
'operation': 'explain',
'version': '1.2',
'query': query,
}
content = self._get_content(self.url, params)
return content
Expand Down Expand Up @@ -101,9 +101,14 @@ def _get_content(self, url, params):
def _check_errors(self, xml):
config = self.OPERATIONS[self.operation]
if not xml.tag == config['response']:
raise errors.ServerIncompatibleError('Server response did not contain a searchRetrieveResponse tag')
raise errors.ServerIncompatibleError(
'Server response did not contain a searchRetrieveResponse tag'
)

diagnostics = xmlparse.find(xml, '%sdiagnostics/%sdiagnostic' % (self.sru, self.sru))
diagnostics = xmlparse.find(
xml,
f'{self.sru}diagnostics/{self.sru}diagnostic'
)
if diagnostics:
error_msg = " ".join([d.find(detail).text for d in diagnostics])
raise errors.SruError(error_msg)
error_msg = " ".join([d.find('detail').text for d in diagnostics])
raise errors.SruError(error_msg)
9 changes: 7 additions & 2 deletions sruthi/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@ class SruthiError(Exception):
General sruthi error class to provide a superclass for all other errors
"""


class ServerIncompatibleError(SruthiError):
"""
The error raised from sru.search/sru.explain when the server doesn't behave
like a SRU endpoint.
"""


class SruError(SruthiError):
"""
The error raised from sru.search/sru.explain when the SRU response contains an error
The error raised from sru.search/sru.explain when the SRU response contains
an error
"""


class NoMoreRecordsError(SruthiError):
"""
This error is raised if all records have been loaded (or no records are present)
This error is raised if all records have been loaded (or no records are
present)
"""
53 changes: 24 additions & 29 deletions sruthi/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
from . import xmlparse
from . import errors


class SruData(object):
def __init__(self, records=[], sru_version=None, count=0, data_loader=None):
def __init__(self, records=[], sru_version=None, count=0, data_loader=None): # noqa
self.records = records
self.sru_version = sru_version
self.count = count
self._data_loader = data_loader

def __repr__(self):
return (
'SruData('
Expand All @@ -29,7 +30,7 @@ def __length_hint__(self):

def __iter__(self):
# use while loop since self.records could grow while iterating
i = 0
i = 0
while True:
# load new data when near end
if i == len(self.records):
Expand All @@ -38,38 +39,32 @@ def __iter__(self):
except errors.NoMoreRecordsError:
break
yield self.records[i]
i += 1
i += 1

def __getitem__(self, key):
if isinstance(key, slice):
limit = max(key.start or 0, key.stop or self.count)
while limit > len(self.records):
try:
self._load_new_data()
except errors.NoMoreRecordsError:
pass
return [self.records[k] for k in range(*key.indices(len(self.records)))]
self._load_new_data_until(limit)
count = len(self.records)
return [self.records[k] for k in range(*key.indices(count))]

if not isinstance(key, int):
raise IndexError("Index must be an integer")
if key >= self.count:
raise IndexError("Index %s is out of range" % key)
if key < 0:
# load all data
while True:
try:
self._load_new_data()
except errors.NoMoreRecordsError:
break
else:
while key >= len(self.records):
try:
self._load_new_data()
except errors.NoMoreRecordsError:
if key >= len(self.records):
raise IndexError("Index %s not found" % key)
raise TypeError("Index must be an integer or slice")

limit = key
if limit < 0:
# if we get a negative index, load all data
limit = self.count
self._load_new_data_until(limit)
return self.records[key]

def _load_new_data_until(self, limit):
while limit >= len(self.records):
try:
self._load_new_data()
except errors.NoMoreRecordsError:
break

def _load_new_data(self):
result = self._data_loader()
self.records.extend(result['records'])
Expand All @@ -84,7 +79,7 @@ def extract_records(xml):
record['schema'] = xmlparse.find(xml_rec, './sru:recordSchema').text
record_data = xmlparse.find(xml_rec, './sru:recordData')
extra_data = xmlparse.find(xml_rec, './sru:extraRecordData')

for elem in record_data.iter():
record = tag_data(record, elem)

Expand All @@ -106,6 +101,6 @@ def tag_data(record, elem):
tag_name = ns_pattern.sub('', elem.tag)
if elem.text and elem.text.strip():
record[tag_name] = elem.text.strip()
elif len(list(elem)) == 0: # leaf element
elif len(list(elem)) == 0: # leaf element
record[tag_name] = None
return record
2 changes: 2 additions & 0 deletions sruthi/sru.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

from . import client


def searchretrieve(url, query):
c = client.Client(url)
return c.searchretrieve(query)


def explain(url):
c = client.Client(url)
return c.explain()
1 change: 1 addition & 0 deletions sruthi/xmlparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
'ap': 'http://www.archivportal.ch/srw/extension/',
}


class XMLNone(object):
def __nonzero__(self):
return False
Expand Down
12 changes: 10 additions & 2 deletions tests/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,16 @@ def test_searchretrieve(self, session_mock):
for rec in r:
self.assertIsInstance(rec, dict)
self.assertEqual(rec['schema'], 'isad')

session_mock.return_value.get.assert_any_call('http://test.com/sru', params={'startRecord': 1, 'query': 'Test-Query', 'operation': 'searchretrieve', 'version': '1.2'})

session_mock.return_value.get.assert_any_call(
'http://test.com/sru',
params={
'startRecord': 1,
'query': 'Test-Query',
'operation': 'searchretrieve',
'version': '1.2'
}
)

# add test for getitem with slices etc.
# print("-3")
Expand Down
6 changes: 3 additions & 3 deletions tests/sru_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ def test_searchretrieve(self, session_mock):
'reference': 'VII.335.:2.34.8.',
'extra': {
'score': '0.38',
'link': 'https://amsquery.stadt-zuerich.ch/detail.aspx?Id=410130',
'link': 'https://amsquery.stadt-zuerich.ch/detail.aspx?Id=410130', # noqa
'hasDigitizedItems': '0',
'endDateISO': '1998-12-31',
'beginDateISO': '1998-01-01',
'beginDateISO': '1998-01-01',
'beginApprox': '0',
'endApprox': '0'
},
'descriptionlevel': 'Dossier',
'title': u'Podium "Frauen und Politik" beim Jubil\xe4umsanlass "Frauenrechte-Menschenrechte" des Bundes Schweizerischer Frauenorganisationen BSF zu 150 Jahre Bundesstaat, 50 Jahre UNO-Menschenrechtserkl\xe4rung und 27 Jahre politische Gleichberechtigung im Nationalratssaal in Bern vom 4. April 1998',
'title': u'Podium "Frauen und Politik" beim Jubil\xe4umsanlass "Frauenrechte-Menschenrechte" des Bundes Schweizerischer Frauenorganisationen BSF zu 150 Jahre Bundesstaat, 50 Jahre UNO-Menschenrechtserkl\xe4rung und 27 Jahre politische Gleichberechtigung im Nationalratssaal in Bern vom 4. April 1998', # noqa
'extent': None,
'date': '1998',
'creator': None,
Expand Down
3 changes: 1 addition & 2 deletions tests/sruthi_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,4 @@ def _session_mock(self, session_mock, filename=None):
filename
)
with open(path) as file:
session_mock.return_value.get.return_value = mock.MagicMock(content=file.read())

session_mock.return_value.get.return_value = mock.MagicMock(content=file.read()) # noqa

0 comments on commit 07bc852

Please sign in to comment.