From 87ecc9dacceb5b4a5ec6bb83e70d845b644f2295 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Guillot?= Date: Tue, 18 Apr 2017 13:38:14 -0400 Subject: [PATCH] Add PEP8 validation and missing copyright headers --- setup.py | 3 +- test-requirements.txt | 3 +- tests/http_utils_test.py | 17 +++++++++-- tests/ubersmith_request_form_encoding_test.py | 14 +++++++++ tests/ubersmith_request_get_test.py | 2 ++ tests/ubersmith_request_post_test.py | 2 ++ tests/ubersmith_request_test.py | 30 +++++++++++-------- tox.ini | 11 ++++++- ubersmith_client/_http_utils.py | 15 ++++++++++ ubersmith_client/ubersmith_api.py | 2 ++ ubersmith_client/ubersmith_request.py | 18 +++++------ ubersmith_client/ubersmith_request_get.py | 2 ++ ubersmith_client/ubersmith_request_post.py | 2 ++ 13 files changed, 93 insertions(+), 28 deletions(-) diff --git a/setup.py b/setup.py index 919bcf9..823b9f8 100755 --- a/setup.py +++ b/setup.py @@ -1,5 +1,4 @@ - -# Copyright 2016 Internap +# Copyright 2017 Internap. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/test-requirements.txt b/test-requirements.txt index daf105d..bb0dfc5 100755 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -1,3 +1,4 @@ nose==1.2.1 pyhamcrest==1.8.1 -mock==1.3.0 \ No newline at end of file +mock==1.3.0 +flake8 diff --git a/tests/http_utils_test.py b/tests/http_utils_test.py index 52472e2..2c01c50 100644 --- a/tests/http_utils_test.py +++ b/tests/http_utils_test.py @@ -1,4 +1,19 @@ +# Copyright 2017 Internap. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + import unittest + from ubersmith_client import _http_utils @@ -51,5 +66,3 @@ def test_with_bools(self): 'true': True, 'false': False }, result) - - diff --git a/tests/ubersmith_request_form_encoding_test.py b/tests/ubersmith_request_form_encoding_test.py index 4572f6f..1337ccc 100644 --- a/tests/ubersmith_request_form_encoding_test.py +++ b/tests/ubersmith_request_form_encoding_test.py @@ -1,3 +1,17 @@ +# Copyright 2017 Internap. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + import unittest from mock import sentinel, patch, MagicMock diff --git a/tests/ubersmith_request_get_test.py b/tests/ubersmith_request_get_test.py index e14d9db..3717e8c 100644 --- a/tests/ubersmith_request_get_test.py +++ b/tests/ubersmith_request_get_test.py @@ -11,11 +11,13 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. + import unittest from hamcrest import assert_that, equal_to from mock import patch, MagicMock import ubersmith_client + from tests.ubersmith_json.response_data_structure import a_response_data diff --git a/tests/ubersmith_request_post_test.py b/tests/ubersmith_request_post_test.py index 44d407b..1149c0d 100644 --- a/tests/ubersmith_request_post_test.py +++ b/tests/ubersmith_request_post_test.py @@ -11,11 +11,13 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. + import unittest from hamcrest import assert_that, equal_to, calling, raises from mock import patch, MagicMock import ubersmith_client + from tests.ubersmith_json.response_data_structure import a_response_data diff --git a/tests/ubersmith_request_test.py b/tests/ubersmith_request_test.py index 7220aff..6d026af 100644 --- a/tests/ubersmith_request_test.py +++ b/tests/ubersmith_request_test.py @@ -11,18 +11,19 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. + import unittest import ubersmith_client from mock import Mock, patch from hamcrest import assert_that, raises, calling, equal_to from requests.exceptions import ConnectionError, Timeout -from ubersmith_client.exceptions import UbersmithException, BadRequest, UnknownError, Forbidden, NotFound, Unauthorized, \ - UbersmithConnectionError, \ - UbersmithTimeout -from tests.ubersmith_json.response_data_structure import a_response_data + +from ubersmith_client import exceptions from ubersmith_client.ubersmith_request import UbersmithRequest +from tests.ubersmith_json.response_data_structure import a_response_data + class UbersmithRequestTest(unittest.TestCase): def setUp(self): @@ -50,35 +51,40 @@ def test_process_ubersmith_response_not_application_json(self): def test_process_ubersmith_response_raise_exception(self): response = Mock(status_code=400, headers={'content-type': 'application/json'}) - assert_that(calling(UbersmithRequest.process_ubersmith_response).with_args(response), raises(BadRequest)) + assert_that(calling(UbersmithRequest.process_ubersmith_response).with_args(response), + raises(exceptions.BadRequest)) response.status_code = 401 - assert_that(calling(UbersmithRequest.process_ubersmith_response).with_args(response), raises(Unauthorized)) + assert_that(calling(UbersmithRequest.process_ubersmith_response).with_args(response), + raises(exceptions.Unauthorized)) response.status_code = 403 - assert_that(calling(UbersmithRequest.process_ubersmith_response).with_args(response), raises(Forbidden)) + assert_that(calling(UbersmithRequest.process_ubersmith_response).with_args(response), + raises(exceptions.Forbidden)) response.status_code = 404 - assert_that(calling(UbersmithRequest.process_ubersmith_response).with_args(response), raises(NotFound)) + assert_that(calling(UbersmithRequest.process_ubersmith_response).with_args(response), + raises(exceptions.NotFound)) response.status_code = 500 - assert_that(calling(UbersmithRequest.process_ubersmith_response).with_args(response), raises(UnknownError)) + assert_that(calling(UbersmithRequest.process_ubersmith_response).with_args(response), + raises(exceptions.UnknownError)) response.status_code = 200 response.json = Mock(return_value={'status': False, 'error_code': 42, 'error_message': 'come and watch tv'}) assert_that(calling(UbersmithRequest.process_ubersmith_response).with_args(response), - raises(UbersmithException, 'Error code 42 - message: come and watch tv')) + raises(exceptions.UbersmithException, 'Error code 42 - message: come and watch tv')) @patch('ubersmith_client.ubersmith_request_post.requests') def test_api_method_returns_handle_connection_error_exception(self, requests_mock): ubersmith_api = ubersmith_client.api.init(self.url, self.username, self.password) requests_mock.post = Mock(side_effect=ConnectionError()) - assert_that(calling(ubersmith_api.client.list), raises(UbersmithConnectionError)) + assert_that(calling(ubersmith_api.client.list), raises(exceptions.UbersmithConnectionError)) @patch('ubersmith_client.ubersmith_request_post.requests') def test_api_method_returns_handle_timeout_exception(self, requests_mock): ubersmith_api = ubersmith_client.api.init(self.url, self.username, self.password) requests_mock.post = Mock(side_effect=Timeout()) - assert_that(calling(ubersmith_api.client.list), raises(UbersmithTimeout)) + assert_that(calling(ubersmith_api.client.list), raises(exceptions.UbersmithTimeout)) diff --git a/tox.ini b/tox.ini index 6f9d47d..ef011f1 100755 --- a/tox.ini +++ b/tox.ini @@ -1,7 +1,16 @@ [tox] -envlist = py34,py27 +envlist = py34,py27,pep8 [testenv] deps = -r{toxinidir}/test-requirements.txt commands = nosetests + +[testenv:pep8] +deps = -r{toxinidir}/test-requirements.txt +commands = flake8 {posargs} + +[flake8] +show-source = True +max-line-length = 120 +exclude = .venv,.git,.tox,dist,doc,*egg,build,ubersmith_client/__init__.py diff --git a/ubersmith_client/_http_utils.py b/ubersmith_client/_http_utils.py index a851b56..df15971 100644 --- a/ubersmith_client/_http_utils.py +++ b/ubersmith_client/_http_utils.py @@ -1,3 +1,18 @@ +# Copyright 2017 Internap. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + def form_encode(data): exploded_data = {} for k, v in data.items(): diff --git a/ubersmith_client/ubersmith_api.py b/ubersmith_client/ubersmith_api.py index 8595b31..2431281 100644 --- a/ubersmith_client/ubersmith_api.py +++ b/ubersmith_client/ubersmith_api.py @@ -1,3 +1,4 @@ +# Copyright 2017 Internap. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -10,6 +11,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. + from ubersmith_client.ubersmith_request_get import UbersmithRequestGet from ubersmith_client.ubersmith_request_post import UbersmithRequestPost diff --git a/ubersmith_client/ubersmith_request.py b/ubersmith_client/ubersmith_request.py index d77966e..4a807fc 100644 --- a/ubersmith_client/ubersmith_request.py +++ b/ubersmith_client/ubersmith_request.py @@ -1,3 +1,4 @@ +# Copyright 2017 Internap. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -10,11 +11,11 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. + from abc import abstractmethod from requests import Timeout, ConnectionError -from ubersmith_client.exceptions import get_exception_for, UbersmithException, UbersmithConnectionError, \ - UbersmithTimeout +from ubersmith_client import exceptions class UbersmithRequest(object): @@ -39,9 +40,9 @@ def _process_request(self, method, **kwargs): return method(**kwargs) except ConnectionError: - raise UbersmithConnectionError(self.url) + raise exceptions.UbersmithConnectionError(self.url) except Timeout: - raise UbersmithTimeout(self.url, self.timeout) + raise exceptions.UbersmithTimeout(self.url, self.timeout) def _build_request_params(self, kwargs): _methods = '.'.join(self.methods) @@ -50,16 +51,13 @@ def _build_request_params(self, kwargs): @staticmethod def process_ubersmith_response(response): if response.status_code < 200 or response.status_code >= 400: - raise get_exception_for(status_code=response.status_code) + raise exceptions.get_exception_for(status_code=response.status_code) if response.headers['content-type'] == 'application/json': response_json = response.json() if not response_json['status']: - raise UbersmithException( - response_json['error_code'], - response_json['error_message'] - ) - + raise exceptions.UbersmithException(response_json['error_code'], + response_json['error_message']) return response_json['data'] return response.content diff --git a/ubersmith_client/ubersmith_request_get.py b/ubersmith_client/ubersmith_request_get.py index 7435139..37d4cce 100644 --- a/ubersmith_client/ubersmith_request_get.py +++ b/ubersmith_client/ubersmith_request_get.py @@ -1,3 +1,4 @@ +# Copyright 2017 Internap. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -10,6 +11,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. + import requests from ubersmith_client import _http_utils diff --git a/ubersmith_client/ubersmith_request_post.py b/ubersmith_client/ubersmith_request_post.py index b57abe4..9cc7742 100644 --- a/ubersmith_client/ubersmith_request_post.py +++ b/ubersmith_client/ubersmith_request_post.py @@ -1,3 +1,4 @@ +# Copyright 2017 Internap. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -10,6 +11,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. + import requests from ubersmith_client import _http_utils