Skip to content

Commit f4c774c

Browse files
authored
Merge pull request #6 from metaodi/fix-linting
Fix linting errors
2 parents 8d551c3 + 8530819 commit f4c774c

File tree

11 files changed

+64
-46
lines changed

11 files changed

+64
-46
lines changed

.github/workflows/lint_python.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ jobs:
2424
python -m pip install --upgrade pip
2525
pip install -r requirements.txt
2626
pip install -r test-requirements.txt
27+
pip install -e .
2728
28-
- run: flake8 sruthi --count --show-source --statistics
29+
- run: flake8 . --count --show-source --statistics
2930
- run: pytest

setup.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[flake8]
2+
max-complexity = 10

sruthi/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
__version__ = '0.0.1'
22

3-
from .errors import (SruthiError, ServerIncompatibleError, SruError, NoMoreRecordsError)
4-
from .sru import searchretrieve, explain
3+
from .errors import (SruthiError, ServerIncompatibleError, SruError, NoMoreRecordsError) # noqa
4+
from .sru import searchretrieve, explain # noqa

sruthi/client.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from . import xmlparse
66
from . import metadata
77

8+
89
class Client(object):
910
def __init__(self, url=None, page_size=100):
1011
self.session = requests.Session()
@@ -34,7 +35,6 @@ def explain(self):
3435
params = {
3536
'operation': 'explain',
3637
'version': '1.2',
37-
'query': query,
3838
}
3939
content = self._get_content(self.url, params)
4040
return content
@@ -101,9 +101,14 @@ def _get_content(self, url, params):
101101
def _check_errors(self, xml):
102102
config = self.OPERATIONS[self.operation]
103103
if not xml.tag == config['response']:
104-
raise errors.ServerIncompatibleError('Server response did not contain a searchRetrieveResponse tag')
104+
raise errors.ServerIncompatibleError(
105+
'Server response did not contain a searchRetrieveResponse tag'
106+
)
105107

106-
diagnostics = xmlparse.find(xml, '%sdiagnostics/%sdiagnostic' % (self.sru, self.sru))
108+
diagnostics = xmlparse.find(
109+
xml,
110+
f'{self.sru}diagnostics/{self.sru}diagnostic'
111+
)
107112
if diagnostics:
108-
error_msg = " ".join([d.find(detail).text for d in diagnostics])
109-
raise errors.SruError(error_msg)
113+
error_msg = " ".join([d.find('detail').text for d in diagnostics])
114+
raise errors.SruError(error_msg)

sruthi/errors.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,23 @@ class SruthiError(Exception):
33
General sruthi error class to provide a superclass for all other errors
44
"""
55

6+
67
class ServerIncompatibleError(SruthiError):
78
"""
89
The error raised from sru.search/sru.explain when the server doesn't behave
910
like a SRU endpoint.
1011
"""
1112

13+
1214
class SruError(SruthiError):
1315
"""
14-
The error raised from sru.search/sru.explain when the SRU response contains an error
16+
The error raised from sru.search/sru.explain when the SRU response contains
17+
an error
1518
"""
1619

20+
1721
class NoMoreRecordsError(SruthiError):
1822
"""
19-
This error is raised if all records have been loaded (or no records are present)
23+
This error is raised if all records have been loaded (or no records are
24+
present)
2025
"""

sruthi/metadata.py

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
from . import xmlparse
66
from . import errors
77

8+
89
class SruData(object):
9-
def __init__(self, records=[], sru_version=None, count=0, data_loader=None):
10+
def __init__(self, records=[], sru_version=None, count=0, data_loader=None): # noqa
1011
self.records = records
1112
self.sru_version = sru_version
1213
self.count = count
1314
self._data_loader = data_loader
14-
15+
1516
def __repr__(self):
1617
return (
1718
'SruData('
@@ -29,7 +30,7 @@ def __length_hint__(self):
2930

3031
def __iter__(self):
3132
# use while loop since self.records could grow while iterating
32-
i = 0
33+
i = 0
3334
while True:
3435
# load new data when near end
3536
if i == len(self.records):
@@ -38,38 +39,32 @@ def __iter__(self):
3839
except errors.NoMoreRecordsError:
3940
break
4041
yield self.records[i]
41-
i += 1
42+
i += 1
4243

4344
def __getitem__(self, key):
4445
if isinstance(key, slice):
4546
limit = max(key.start or 0, key.stop or self.count)
46-
while limit > len(self.records):
47-
try:
48-
self._load_new_data()
49-
except errors.NoMoreRecordsError:
50-
pass
51-
return [self.records[k] for k in range(*key.indices(len(self.records)))]
47+
self._load_new_data_until(limit)
48+
count = len(self.records)
49+
return [self.records[k] for k in range(*key.indices(count))]
50+
5251
if not isinstance(key, int):
53-
raise IndexError("Index must be an integer")
54-
if key >= self.count:
55-
raise IndexError("Index %s is out of range" % key)
56-
if key < 0:
57-
# load all data
58-
while True:
59-
try:
60-
self._load_new_data()
61-
except errors.NoMoreRecordsError:
62-
break
63-
else:
64-
while key >= len(self.records):
65-
try:
66-
self._load_new_data()
67-
except errors.NoMoreRecordsError:
68-
if key >= len(self.records):
69-
raise IndexError("Index %s not found" % key)
52+
raise TypeError("Index must be an integer or slice")
7053

54+
limit = key
55+
if limit < 0:
56+
# if we get a negative index, load all data
57+
limit = self.count
58+
self._load_new_data_until(limit)
7159
return self.records[key]
7260

61+
def _load_new_data_until(self, limit):
62+
while limit >= len(self.records):
63+
try:
64+
self._load_new_data()
65+
except errors.NoMoreRecordsError:
66+
break
67+
7368
def _load_new_data(self):
7469
result = self._data_loader()
7570
self.records.extend(result['records'])
@@ -84,7 +79,7 @@ def extract_records(xml):
8479
record['schema'] = xmlparse.find(xml_rec, './sru:recordSchema').text
8580
record_data = xmlparse.find(xml_rec, './sru:recordData')
8681
extra_data = xmlparse.find(xml_rec, './sru:extraRecordData')
87-
82+
8883
for elem in record_data.iter():
8984
record = tag_data(record, elem)
9085

@@ -106,6 +101,6 @@ def tag_data(record, elem):
106101
tag_name = ns_pattern.sub('', elem.tag)
107102
if elem.text and elem.text.strip():
108103
record[tag_name] = elem.text.strip()
109-
elif len(list(elem)) == 0: # leaf element
104+
elif len(list(elem)) == 0: # leaf element
110105
record[tag_name] = None
111106
return record

sruthi/sru.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
from . import client
44

5+
56
def searchretrieve(url, query):
67
c = client.Client(url)
78
return c.searchretrieve(query)
89

10+
911
def explain(url):
1012
c = client.Client(url)
1113
return c.explain()

sruthi/xmlparse.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
'ap': 'http://www.archivportal.ch/srw/extension/',
99
}
1010

11+
1112
class XMLNone(object):
1213
def __nonzero__(self):
1314
return False

tests/client_test.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,16 @@ def test_searchretrieve(self, session_mock):
1818
for rec in r:
1919
self.assertIsInstance(rec, dict)
2020
self.assertEqual(rec['schema'], 'isad')
21-
22-
session_mock.return_value.get.assert_any_call('http://test.com/sru', params={'startRecord': 1, 'query': 'Test-Query', 'operation': 'searchretrieve', 'version': '1.2'})
21+
22+
session_mock.return_value.get.assert_any_call(
23+
'http://test.com/sru',
24+
params={
25+
'startRecord': 1,
26+
'query': 'Test-Query',
27+
'operation': 'searchretrieve',
28+
'version': '1.2'
29+
}
30+
)
2331

2432
# add test for getitem with slices etc.
2533
# print("-3")

tests/sru_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ def test_searchretrieve(self, session_mock):
2121
'reference': 'VII.335.:2.34.8.',
2222
'extra': {
2323
'score': '0.38',
24-
'link': 'https://amsquery.stadt-zuerich.ch/detail.aspx?Id=410130',
24+
'link': 'https://amsquery.stadt-zuerich.ch/detail.aspx?Id=410130', # noqa
2525
'hasDigitizedItems': '0',
2626
'endDateISO': '1998-12-31',
27-
'beginDateISO': '1998-01-01',
27+
'beginDateISO': '1998-01-01',
2828
'beginApprox': '0',
2929
'endApprox': '0'
3030
},
3131
'descriptionlevel': 'Dossier',
32-
'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',
32+
'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
3333
'extent': None,
3434
'date': '1998',
3535
'creator': None,

0 commit comments

Comments
 (0)