Skip to content

Commit a52a678

Browse files
authored
Merge pull request #49 from maxmind/greg/fix-travis
Fix Travis, run yapf from it, and minor code cleanup
2 parents 04c8914 + d7e8a15 commit a52a678

File tree

8 files changed

+94
-59
lines changed

8 files changed

+94
-59
lines changed

.travis-pylint.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
python setup.py install
3+
pylint geoip2

.travis-yapf.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
3+
diff=$(yapf -rd geoip2 tests)
4+
5+
if [[ $? != 0 ]]; then
6+
echo "yapf failed to run."
7+
echo "$diff"
8+
exit $?
9+
elif [[ $diff ]]; then
10+
echo "$diff"
11+
exit 1
12+
else
13+
exit 0
14+
fi

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ install:
3131
- "if [[ $TRAVIS_PYTHON_VERSION == '2.7' ]]; then pip install ipaddr; fi"
3232
script:
3333
- coverage run --source=geoip2 setup.py test
34-
- "if [[ $TRAVIS_PYTHON_VERSION == '2.7' ]]; then pylint geoip2; fi"
34+
- "if [[ $TRAVIS_PYTHON_VERSION == '3.6' ]]; then ./.travis-pylint.sh; fi"
35+
- "if [[ $TRAVIS_PYTHON_VERSION == '3.6' ]]; then ./.travis-yapf.sh; fi"
3536
after_success:
3637
- coveralls
3738
notifications:

geoip2/mixins.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ class SimpleEquality(object):
99
__metaclass__ = ABCMeta
1010

1111
def __eq__(self, other):
12-
return (isinstance(other, self.__class__) and
13-
self.__dict__ == other.__dict__)
12+
return (isinstance(other, self.__class__)
13+
and self.__dict__ == other.__dict__)
1414

1515
def __ne__(self, other):
1616
return not self.__eq__(other)

geoip2/webservice.py

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,10 @@ def _response_for(self, path, model_class, ip_address):
149149
'User-Agent': self._user_agent()
150150
},
151151
timeout=self._timeout)
152-
if response.status_code == 200:
153-
body = self._handle_success(response, uri)
154-
return model_class(body, locales=self._locales)
155-
else:
156-
self._handle_error(response, uri)
152+
if response.status_code != 200:
153+
raise self._exception_for_error(response, uri)
154+
body = self._handle_success(response, uri)
155+
return model_class(body, locales=self._locales)
157156

158157
def _user_agent(self):
159158
return 'GeoIP2 Python Client v%s (%s)' % (geoip2.__version__,
@@ -168,57 +167,54 @@ def _handle_success(self, response, uri):
168167
'JSON: ' % locals() + ', '.join(ex.args), 200,
169168
uri)
170169

171-
def _handle_error(self, response, uri):
170+
def _exception_for_error(self, response, uri):
172171
status = response.status_code
173172

174173
if 400 <= status < 500:
175-
self._handle_4xx_status(response, status, uri)
174+
return self._exception_for_4xx_status(response, status, uri)
176175
elif 500 <= status < 600:
177-
self._handle_5xx_status(status, uri)
178-
else:
179-
self._handle_non_200_status(status, uri)
176+
return self._exception_for_5xx_status(status, uri)
177+
return self._exception_for_non_200_status(status, uri)
180178

181-
def _handle_4xx_status(self, response, status, uri):
179+
def _exception_for_4xx_status(self, response, status, uri):
182180
if not response.content:
183-
raise HTTPError('Received a %(status)i error for %(uri)s '
184-
'with no body.' % locals(), status, uri)
181+
return HTTPError('Received a %(status)i error for %(uri)s '
182+
'with no body.' % locals(), status, uri)
185183
elif response.headers['Content-Type'].find('json') == -1:
186-
raise HTTPError('Received a %i for %s with the following '
187-
'body: %s' % (status, uri, response.content),
188-
status, uri)
184+
return HTTPError('Received a %i for %s with the following '
185+
'body: %s' % (status, uri, response.content),
186+
status, uri)
189187
try:
190188
body = response.json()
191189
except ValueError as ex:
192-
raise HTTPError(
190+
return HTTPError(
193191
'Received a %(status)i error for %(uri)s but it did'
194192
' not include the expected JSON body: ' % locals() +
195193
', '.join(ex.args), status, uri)
196194
else:
197195
if 'code' in body and 'error' in body:
198-
self._handle_web_service_error(
196+
return self._exception_for_web_service_error(
199197
body.get('error'), body.get('code'), status, uri)
200-
else:
201-
raise HTTPError(
202-
'Response contains JSON but it does not specify '
203-
'code or error keys', status, uri)
198+
return HTTPError('Response contains JSON but it does not specify '
199+
'code or error keys', status, uri)
204200

205-
def _handle_web_service_error(self, message, code, status, uri):
201+
def _exception_for_web_service_error(self, message, code, status, uri):
206202
if code in ('IP_ADDRESS_NOT_FOUND', 'IP_ADDRESS_RESERVED'):
207-
raise AddressNotFoundError(message)
203+
return AddressNotFoundError(message)
208204
elif code in ('AUTHORIZATION_INVALID', 'LICENSE_KEY_REQUIRED',
209205
'USER_ID_REQUIRED', 'USER_ID_UNKNOWN'):
210-
raise AuthenticationError(message)
206+
return AuthenticationError(message)
211207
elif code in ('INSUFFICIENT_FUNDS', 'OUT_OF_QUERIES'):
212-
raise OutOfQueriesError(message)
208+
return OutOfQueriesError(message)
213209
elif code == 'PERMISSION_REQUIRED':
214-
raise PermissionRequiredError(message)
210+
return PermissionRequiredError(message)
215211

216-
raise InvalidRequestError(message, code, status, uri)
212+
return InvalidRequestError(message, code, status, uri)
217213

218-
def _handle_5xx_status(self, status, uri):
219-
raise HTTPError('Received a server error (%(status)i) for '
220-
'%(uri)s' % locals(), status, uri)
214+
def _exception_for_5xx_status(self, status, uri):
215+
return HTTPError('Received a server error (%(status)i) for '
216+
'%(uri)s' % locals(), status, uri)
221217

222-
def _handle_non_200_status(self, status, uri):
223-
raise HTTPError('Received a very surprising HTTP status '
224-
'(%(status)i) for %(uri)s' % locals(), status, uri)
218+
def _exception_for_non_200_status(self, status, uri):
219+
return HTTPError('Received a very surprising HTTP status '
220+
'(%(status)i) for %(uri)s' % locals(), status, uri)

tests/database_test.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ def test_asn(self):
8787
self.assertEqual(record.ip_address, ip_address)
8888

8989
self.assertRegex(
90-
str(record),
91-
r'geoip2.models.ASN\(.*1\.128\.0\.0.*\)',
90+
str(record), r'geoip2.models.ASN\(.*1\.128\.0\.0.*\)',
9291
'str representation is correct')
9392

9493
self.assertEqual(record, eval(repr(record)), "ASN repr can be eval'd")
@@ -120,8 +119,7 @@ def test_connection_type(self):
120119
str(record), r'ConnectionType\(\{.*Cable/DSL.*\}\)',
121120
'ConnectionType str representation is reasonable')
122121

123-
self.assertEqual(record,
124-
eval(repr(record)),
122+
self.assertEqual(record, eval(repr(record)),
125123
"ConnectionType repr can be eval'd")
126124

127125
reader.close()
@@ -147,8 +145,8 @@ def test_domain(self):
147145
str(record), r'Domain\(\{.*maxmind.com.*\}\)',
148146
'Domain str representation is reasonable')
149147

150-
self.assertEqual(record,
151-
eval(repr(record)), "Domain repr can be eval'd")
148+
self.assertEqual(record, eval(repr(record)),
149+
"Domain repr can be eval'd")
152150

153151
reader.close()
154152

tests/models_test.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,14 @@ def test_insights_full(self):
163163
r'^geoip2.models.Insights\(\{.*geoname_id.*\}, \[.*en.*\]\)',
164164
'Insights str representation looks reasonable')
165165

166-
self.assertEqual(model,
167-
eval(repr(model)), "Insights repr can be eval'd")
166+
self.assertEqual(model, eval(repr(model)),
167+
"Insights repr can be eval'd")
168168

169169
self.assertRegex(
170170
str(model.location), r'^geoip2.records.Location\(.*longitude=.*\)',
171171
'Location str representation is reasonable')
172172

173-
self.assertEqual(model.location,
174-
eval(repr(model.location)),
173+
self.assertEqual(model.location, eval(repr(model.location)),
175174
"Location repr can be eval'd")
176175

177176
self.assertTrue(model.traits.is_anonymous)

tests/webservice_test.py

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ def test_country_ok(self, mock):
5959
self.base_uri + 'country/1.2.3.4',
6060
json=self.country,
6161
status_code=200,
62-
headers={'Content-Type': self._content_type('country')})
62+
headers={
63+
'Content-Type': self._content_type('country')
64+
})
6365
country = self.client.country('1.2.3.4')
6466
self.assertEqual(
6567
type(country), geoip2.models.Country,
@@ -87,7 +89,9 @@ def test_me(self, mock):
8789
self.base_uri + 'country/me',
8890
json=self.country,
8991
status_code=200,
90-
headers={'Content-Type': self._content_type('country')})
92+
headers={
93+
'Content-Type': self._content_type('country')
94+
})
9195
implicit_me = self.client.country()
9296
self.assertEqual(
9397
type(implicit_me), geoip2.models.Country,
@@ -102,7 +106,9 @@ def test_200_error(self, mock):
102106
mock.get(
103107
self.base_uri + 'country/1.1.1.1',
104108
status_code=200,
105-
headers={'Content-Type': self._content_type('country')})
109+
headers={
110+
'Content-Type': self._content_type('country')
111+
})
106112
with self.assertRaisesRegex(GeoIP2Error,
107113
'could not decode the response as JSON'):
108114
self.client.country('1.1.1.1')
@@ -119,7 +125,9 @@ def test_no_body_error(self, mock):
119125
self.base_uri + 'country/' + '1.2.3.7',
120126
text='',
121127
status_code=400,
122-
headers={'Content-Type': self._content_type('country')})
128+
headers={
129+
'Content-Type': self._content_type('country')
130+
})
123131
with self.assertRaisesRegex(
124132
HTTPError, 'Received a 400 error for .* with no body'):
125133
self.client.country('1.2.3.7')
@@ -130,7 +138,9 @@ def test_weird_body_error(self, mock):
130138
self.base_uri + 'country/' + '1.2.3.8',
131139
text='{"wierd": 42}',
132140
status_code=400,
133-
headers={'Content-Type': self._content_type('country')})
141+
headers={
142+
'Content-Type': self._content_type('country')
143+
})
134144
with self.assertRaisesRegex(HTTPError,
135145
'Response contains JSON but it does not '
136146
'specify code or error keys'):
@@ -142,7 +152,9 @@ def test_bad_body_error(self, mock):
142152
self.base_uri + 'country/' + '1.2.3.9',
143153
text='bad body',
144154
status_code=400,
145-
headers={'Content-Type': self._content_type('country')})
155+
headers={
156+
'Content-Type': self._content_type('country')
157+
})
146158
with self.assertRaisesRegex(
147159
HTTPError, 'it did not include the expected JSON body'):
148160
self.client.country('1.2.3.9')
@@ -159,7 +171,9 @@ def test_300_error(self, mock):
159171
mock.get(
160172
self.base_uri + 'country/' + '1.2.3.11',
161173
status_code=300,
162-
headers={'Content-Type': self._content_type('country')})
174+
headers={
175+
'Content-Type': self._content_type('country')
176+
})
163177
with self.assertRaisesRegex(HTTPError,
164178
'Received a very surprising HTTP status '
165179
'\(300\) for'):
@@ -214,7 +228,9 @@ def _test_error(self, mock, status, error_code, error_class):
214228
self.base_uri + 'country/1.2.3.18',
215229
json=body,
216230
status_code=status,
217-
headers={'Content-Type': self._content_type('country')})
231+
headers={
232+
'Content-Type': self._content_type('country')
233+
})
218234
with self.assertRaisesRegex(error_class, msg):
219235
self.client.country('1.2.3.18')
220236

@@ -227,7 +243,9 @@ def test_unknown_error(self, mock):
227243
self.base_uri + 'country/' + ip,
228244
json=body,
229245
status_code=400,
230-
headers={'Content-Type': self._content_type('country')})
246+
headers={
247+
'Content-Type': self._content_type('country')
248+
})
231249
with self.assertRaisesRegex(InvalidRequestError, msg):
232250
self.client.country(ip)
233251

@@ -237,7 +255,9 @@ def test_request(self, mock):
237255
self.base_uri + 'country/' + '1.2.3.4',
238256
json=self.country,
239257
status_code=200,
240-
headers={'Content-Type': self._content_type('country')})
258+
headers={
259+
'Content-Type': self._content_type('country')
260+
})
241261
self.client.country('1.2.3.4')
242262
request = mock.request_history[-1]
243263

@@ -256,7 +276,9 @@ def test_city_ok(self, mock):
256276
self.base_uri + 'city/' + '1.2.3.4',
257277
json=self.country,
258278
status_code=200,
259-
headers={'Content-Type': self._content_type('city')})
279+
headers={
280+
'Content-Type': self._content_type('city')
281+
})
260282
city = self.client.city('1.2.3.4')
261283
self.assertEqual(
262284
type(city), geoip2.models.City, 'return value of client.city')
@@ -267,7 +289,9 @@ def test_insights_ok(self, mock):
267289
self.base_uri + 'insights/1.2.3.4',
268290
json=self.country,
269291
status_code=200,
270-
headers={'Content-Type': self._content_type('country')})
292+
headers={
293+
'Content-Type': self._content_type('country')
294+
})
271295
insights = self.client.insights('1.2.3.4')
272296
self.assertEqual(
273297
type(insights), geoip2.models.Insights,

0 commit comments

Comments
 (0)