Skip to content

Commit daef052

Browse files
authored
Merge pull request #13 from heavenshell/feature/flake8
Add flake8
2 parents 69f64f6 + a070af9 commit daef052

File tree

10 files changed

+102
-46
lines changed

10 files changed

+102
-46
lines changed

.travis.yml

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
dist: xenial
12
language: python
3+
cache:
4+
pip: true
5+
directories:
6+
- "$TRAVIS_BUILD_DIR/.tox"
27

38
python:
49
- 2.7
@@ -9,6 +14,10 @@ env:
914
global:
1015
MACKEREL_APIKEY='xxxxxxxx'
1116

12-
before_install: pip install -r requirements.txt
17+
before_install:
18+
- pip install -U -r requirements.txt
19+
before_script:
20+
- flake8 --verbose --jobs=8
1321

14-
script: python setup.py test
22+
script:
23+
- python setup.py test

mackerel/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
# -*- coding: utf-8 -*-
12
__import__('pkg_resources').declare_namespace(__name__)

mackerel/client.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
:license: BSD, see LICENSE for more details.
1414
"""
1515
import logging
16+
1617
import requests
1718
import simplejson as json
1819
from mackerel.host import Host
@@ -25,7 +26,7 @@ class MackerelClientError(Exception):
2526
class Client(object):
2627

2728
#: Mackerel apikey error message.
28-
ERROR_MESSAGE_FOR_API_KEY_ABSENCE = 'API key is absent. Set your API key in a environment variable called MACKEREL_APIKEY.'
29+
ERROR_MESSAGE_FOR_API_KEY_ABSENCE = 'API key is absent. Set your API key in a environment variable called MACKEREL_APIKEY.' # noqa E501
2930
#: Log format.
3031
debug_log_format = (
3132
'[%(asctime)s %(levelname)s][%(pathname)s:%(lineno)d]: %(message)s'
@@ -44,8 +45,7 @@ def __init__(self, logger=None, **kwargs):
4445

4546
self.api_key = api_key
4647
if logger is None:
47-
logging.basicConfig(level=logging.INFO,
48-
format=self.debug_log_format)
48+
logging.basicConfig(level=logging.INFO, format=self.debug_log_format)
4949
self.logger = logging.getLogger('mackerel.client')
5050
else:
5151
self.logger = logger
@@ -176,8 +176,9 @@ def _request(self, uri, method='GET', headers=None, params=None):
176176
message = '{0} is not supported.'.format(method)
177177
raise NotImplementedError(message)
178178

179-
self.logger.debug('Response from {0} is {1}'.format(self.origin,
180-
res.status_code))
179+
self.logger.debug(
180+
'Response from {0} is {1}'.format(self.origin, res.status_code),
181+
)
181182
if res.status_code != 200:
182183
message = '{0} {1} failed: {2}'.format(method, uri, res.status_code)
183184
raise MackerelClientError(message)

mackerel/host.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,20 @@ def mac_addr(self):
5757
return i['macAddress']
5858

5959
def __repr__(self):
60+
"""Host instance."""
6061
repr = '<Host('
6162
repr += 'name={0}, meta={1}, type={2}, status={3}, memo={4},'
6263
repr += 'is_retired={5}, id={6}, created_at={7}, roles={8},'
6364
repr += 'interfaces={9})'
64-
return repr.format(self.name, self.meta, self.type, self.status,
65-
self.memo, self.is_retired, self.id,
66-
self.created_at, self.roles, self.interfaces)
65+
return repr.format(
66+
self.name,
67+
self.meta,
68+
self.type,
69+
self.status,
70+
self.memo,
71+
self.is_retired,
72+
self.id,
73+
self.created_at,
74+
self.roles,
75+
self.interfaces,
76+
)

mackerel/runner.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
:copyright: (c) 2015 Shinya Ohyanagi, All rights reserved.
1313
:license: BSD, see LICENSE for more details.
1414
"""
15-
import os
1615
import logging
16+
import os
17+
1718
import click
1819
from mackerel.client import Client
1920

requirements.txt

+9
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,12 @@ requests==2.22.0
22
click==7.0
33
simplejson==3.16.0
44
mock==3.0.5
5+
flake8==3.7.8
6+
flake8-coding==1.3.2
7+
flake8-commas==2.0.0
8+
flake8-comprehensions==1.4.1 # pyup: <2.0
9+
flake8-debugger==3.1.0
10+
flake8-docstrings==1.4.0
11+
flake8-import-order==0.18.1
12+
flake8-print==3.1.0
13+
flake8-string-format==0.2.3

setup.cfg

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[flake8]
2+
max-line-length = 120
3+
exclude =
4+
.cache,
5+
.git,
6+
.tox,
7+
build,
8+
migrations,
9+
venv,
10+
__pycache__
11+
ignore = D100,D101,D102,D103,D104,D106,D107,D205,D208,D400
12+
import-order-style = smarkets
13+
14+
[wheel]
15+
universal = 1

setup.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
:license: BSD, see LICENSE for more details.
1111
"""
1212
import os
13-
from setuptools import setup, find_packages
13+
14+
from setuptools import (
15+
find_packages,
16+
setup,
17+
)
1418

1519
requires = ['requests', 'simplejson', 'click']
1620

@@ -41,12 +45,12 @@
4145
'Intended Audience :: Developers',
4246
'License :: OSI Approved :: BSD License',
4347
'Operating System :: OS Independent',
44-
'Programming Language :: Python'
48+
'Programming Language :: Python',
4549
],
4650
entry_points="""
4751
[console_scripts]
4852
mkr.py = mackerel.runner:main
4953
""",
5054
tests_require=['requests', 'simplejson', 'mock'],
51-
test_suite='tests'
55+
test_suite='tests',
5256
)

tests/test_client.py

+34-28
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,15 @@
1010
:license: BSD, see LICENSE for more details.
1111
"""
1212
import os
13-
import requests
1413
from unittest import TestCase
15-
from mock import patch
16-
from mackerel.client import Client, MackerelClientError
14+
15+
import requests
16+
from mackerel.client import (
17+
Client,
18+
MackerelClientError,
19+
)
1720
from mackerel.host import Host
21+
from mock import patch
1822

1923

2024
def dummy_response(m, filename, status_code=200):
@@ -37,22 +41,22 @@ def setUpClass(cls):
3741

3842
@patch('mackerel.client.requests.get')
3943
def test_should_get_hosts(self, m):
40-
""" Client().get_hosts() should get host list. """
44+
"""Client().get_hosts() should get host list."""
4145
dummy_response(m, 'fixtures/get_hosts.json')
4246
hosts = self.client.get_hosts()
4347
for host in hosts:
4448
self.assertTrue(isinstance(host, Host))
4549

4650
@patch('mackerel.client.requests.get')
4751
def test_should_get_host(self, m):
48-
""" Client().get_hosts() should get host. """
52+
"""Client().get_hosts() should get host."""
4953
dummy_response(m, 'fixtures/get_host.json')
5054
host = self.client.get_host(self.id)
5155
self.assertTrue(isinstance(host, Host))
5256

5357
@patch('mackerel.client.requests.post')
5458
def test_should_update_host_poweroff(self, m):
55-
""" Client().update_host_status('poweroff') should return success. """
59+
"""Client().update_host_status('poweroff') should return success."""
5660
dummy_response(m, 'fixtures/success.json')
5761
ret = self.client.update_host_status(self.id, 'poweroff')
5862
self.assertEqual(ret['success'], True)
@@ -63,7 +67,7 @@ def test_should_update_host_poweroff(self, m):
6367

6468
@patch('mackerel.client.requests.post')
6569
def test_should_update_host_standby(self, m):
66-
""" Client().update_host_status('standby') should return success. """
70+
"""Client().update_host_status('standby') should return success."""
6771
dummy_response(m, 'fixtures/success.json')
6872
ret = self.client.update_host_status(self.id, 'standby')
6973
self.assertEqual(ret['success'], True)
@@ -75,7 +79,7 @@ def test_should_update_host_standby(self, m):
7579

7680
@patch('mackerel.client.requests.post')
7781
def test_should_update_host_working(self, m):
78-
""" Client().update_host_status('working') should return success. """
82+
"""Client().update_host_status('working') should return success."""
7983
dummy_response(m, 'fixtures/success.json')
8084
ret = self.client.update_host_status('2k48zsCx8ij', 'working')
8185
self.assertEqual(ret['success'], True)
@@ -86,7 +90,7 @@ def test_should_update_host_working(self, m):
8690

8791
@patch('mackerel.client.requests.post')
8892
def test_should_update_host_maintenance(self, m):
89-
""" Client().update_host_status('maintenance') should return success. """
93+
"""Client().update_host_status('maintenance') should return success."""
9094
dummy_response(m, 'fixtures/success.json')
9195
ret = self.client.update_host_status(self.id, 'maintenance')
9296
self.assertEqual(ret['success'], True)
@@ -96,75 +100,77 @@ def test_should_update_host_maintenance(self, m):
96100
self.assertEqual(host.status, 'maintenance')
97101

98102
def test_should_update_host_invalid(self):
99-
""" Client().update_host_status('foo') should raise error. """
103+
"""Client().update_host_status('foo') should raise error."""
100104
with self.assertRaises(MackerelClientError):
101105
self.client.update_host_status(self.id, 'foo')
102106

103107
@patch('mackerel.client.requests.post')
104108
def test_should_retire(self, m):
105-
""" Client().retire_host() should return success. """
109+
"""Client().retire_host() should return success."""
106110
dummy_response(m, 'fixtures/success.json')
107111
ret = self.client.retire_host(self.id)
108112
self.assertEqual(ret['success'], True)
109113

110114
@patch('mackerel.client.requests.get')
111115
def test_should_get_latest_metrics(self, m):
112-
""" Client().get_latest_metrics() should get metrics. """
116+
"""Client().get_latest_metrics() should get metrics."""
113117
dummy_response(m, 'fixtures/get_latest_metrics.json')
114-
ret = self.client.get_latest_metrics([self.id],
115-
['loadavg5', 'memory.free'])
118+
ret = self.client.get_latest_metrics(
119+
[self.id],
120+
['loadavg5', 'memory.free'],
121+
)
116122
for k in ['loadavg5', 'memory.free']:
117123
self.assertTrue(k in ret['tsdbLatest'][self.id].keys())
118124

119125
@patch('mackerel.client.requests.post')
120126
def test_should_post_metrics(self, m):
121-
""" Client().post_metrics() should return success. """
127+
"""Client().post_metrics() should return success."""
122128
dummy_response(m, 'fixtures/success.json')
123129
id = self.id
124130
metrics = [
125131
{
126132
'hostId': id, 'name': 'custom.metrics.loadavg',
127-
'time': 1401537844, 'value': 1.4
133+
'time': 1401537844, 'value': 1.4,
128134
},
129135
{
130136
'hostId': id, 'name': 'custom.metrics.uptime',
131-
'time': 1401537844, 'value': 500
132-
}
137+
'time': 1401537844, 'value': 500,
138+
},
133139

134140
]
135141
ret = self.client.post_metrics(metrics)
136142
self.assertEqual(ret['success'], True)
137143

138144
@patch('mackerel.client.requests.post')
139145
def test_should_post_service_metrics(self, m):
140-
""" Client().post_service_metrics() should return success. """
146+
"""Client().post_service_metrics() should return success."""
141147
dummy_response(m, 'fixtures/success.json')
142148
metrics = [
143149
{
144150
'name': 'custom.metrics.latency',
145-
'time': 1401537844, 'value': 0.5
151+
'time': 1401537844, 'value': 0.5,
146152
},
147153
{
148154
'name': 'custom.metrics.uptime',
149-
'time': 1401537844, 'value': 500
150-
}
155+
'time': 1401537844, 'value': 500,
156+
},
151157
]
152158
ret = self.client.post_service_metrics('service_name', metrics)
153159
self.assertEqual(ret['success'], True)
154160

155161
@patch('mackerel.client.requests.post')
156162
def test_should_raise_error_when_service_not_found(self, m):
157-
""" Client().post_service_metrics() should raise error when service name not found. """
163+
"""Client().post_service_metrics() should raise error when service name not found."""
158164
dummy_response(m, 'fixtures/error.json', 404)
159165
metrics = [
160166
{
161167
'name': 'custom.metrics.latency',
162-
'time': 1401537844, 'value': 0.5
168+
'time': 1401537844, 'value': 0.5,
163169
},
164170
{
165171
'name': 'custom.metrics.uptime',
166-
'time': 1401537844, 'value': 500
167-
}
172+
'time': 1401537844, 'value': 500,
173+
},
168174
]
169175
with self.assertRaises(MackerelClientError):
170176
self.client.post_service_metrics('foobarbaz', metrics)
@@ -179,14 +185,14 @@ def setUpClass(cls):
179185

180186
@patch('mackerel.client.requests.get')
181187
def test_should_get_ipaddress(self, m):
182-
""" Host().ip_addr() should get ipaddress. """
188+
"""Host().ip_addr() should get ipaddress."""
183189
dummy_response(m, 'fixtures/get_host.json')
184190
host = self.client.get_host(self.id)
185191
self.assertEqual(host.ip_addr(), '10.0.2.15')
186192

187193
@patch('mackerel.client.requests.get')
188194
def test_should_get_macaddress(self, m):
189-
""" Host().mac_addr() should get ipaddress. """
195+
"""Host().mac_addr() should get ipaddress."""
190196
dummy_response(m, 'fixtures/get_host.json')
191197
host = self.client.get_host(self.id)
192198
self.assertEqual(host.mac_addr(), '08:00:27:96:ed:36')

tox.ini

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[tox]
2-
envlist = py27,py36,py37,pycodestyle
2+
envlist = py27,py36,py37,flake8
33

44
[testenv]
55
commands=python setup.py test
@@ -8,6 +8,6 @@ deps=
88
simplejson
99
mock
1010

11-
[testenv:pycodestyle]
12-
deps = pycodestyle
13-
commands = pycodestyle --repeat --ignore=E501 --show-source mackerel tests setup.py
11+
[testenv:flake8]
12+
deps = flake8
13+
commands = flake8 mackerel tests setup.py

0 commit comments

Comments
 (0)