Skip to content

Commit a32f0a2

Browse files
committed
Format and test
- Add black / zimports formatting - Add tox.ini Change-Id: Ie529f3c28d683accb00700573dfdc80b41c8a1e6
1 parent b35ec20 commit a32f0a2

File tree

17 files changed

+348
-228
lines changed

17 files changed

+348
-228
lines changed

.pre-commit-config.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# See https://pre-commit.com for more information
2+
# See https://pre-commit.com/hooks.html for more hooks
3+
repos:
4+
- repo: https://github.com/python/black/
5+
rev: 19.3b0
6+
hooks:
7+
- id: black
8+
args: [-l 79]
9+
10+
- repo: https://github.com/sqlalchemyorg/zimports/
11+
rev: master
12+
hooks:
13+
- id: zimports
14+
15+

setup.cfg

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,17 @@ addopts= --tb native -v -r fxX --ignore=sqlalchemy
55
python_files=sqlalchemy_collectd/*/test_*.py
66

77

8+
[flake8]
9+
enable-extensions = G
10+
# E203 is due to https://github.com/PyCQA/pycodestyle/issues/373
11+
ignore =
12+
A003,
13+
D,
14+
E203,E305,E711,E712,E721,E722,E741,
15+
N801,N802,N806,
16+
RST304,RST303,RST299,RST399,
17+
W503,W504
18+
exclude = .venv,.git,.tox,dist,doc,*egg,build
19+
import-order-style = google
20+
application-import-names = sqlalchemy_collectd
21+

setup.py

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
1-
from setuptools import setup
2-
from setuptools import find_packages
3-
from setuptools.command.test import test as TestCommand # noqa
41
import os
52
import re
63
import sys
74

5+
from setuptools import find_packages
6+
from setuptools import setup
7+
from setuptools.command.test import test as TestCommand # noqa
8+
89

910
with open(
1011
os.path.join(
11-
os.path.dirname(__file__), 'sqlalchemy_collectd', '__init__.py')
12+
os.path.dirname(__file__), "sqlalchemy_collectd", "__init__.py"
13+
)
1214
) as file_:
13-
VERSION = re.compile(
14-
r".*__version__ = '(.*?)'", re.S).match(file_.read()).group(1)
15+
VERSION = (
16+
re.compile(r".*__version__ = [\"'](.*?)[\"']", re.S)
17+
.match(file_.read())
18+
.group(1)
19+
)
1520

1621

17-
readme = os.path.join(os.path.dirname(__file__), 'README.rst')
22+
readme = os.path.join(os.path.dirname(__file__), "README.rst")
1823

19-
requires = [
20-
'SQLAlchemy>=1.1',
21-
]
24+
requires = ["SQLAlchemy>=1.1"]
2225

2326

2427
class PyTest(TestCommand):
25-
user_options = [('pytest-args=', 'a', "Arguments to pass to py.test")]
28+
user_options = [("pytest-args=", "a", "Arguments to pass to py.test")]
2629

2730
def initialize_options(self):
2831
TestCommand.initialize_options(self)
@@ -36,36 +39,37 @@ def finalize_options(self):
3639
def run_tests(self):
3740
# import here, cause outside the eggs aren't loaded
3841
import pytest
42+
3943
errno = pytest.main(self.pytest_args)
4044
sys.exit(errno)
4145

4246

4347
setup(
44-
name='sqlalchemy-collectd',
48+
name="sqlalchemy-collectd",
4549
version=VERSION,
4650
description="Send database connection pool stats to collectd",
4751
long_description=open(readme).read(),
4852
classifiers=[
49-
'Development Status :: 3 - Alpha',
50-
'Intended Audience :: Developers',
51-
'Programming Language :: Python',
52-
'Programming Language :: Python :: 3',
53-
'Topic :: Database :: Front-Ends',
53+
"Development Status :: 3 - Alpha",
54+
"Intended Audience :: Developers",
55+
"Programming Language :: Python",
56+
"Programming Language :: Python :: 3",
57+
"Topic :: Database :: Front-Ends",
5458
],
55-
keywords='SQLAlchemy collectd',
56-
author='Mike Bayer',
57-
author_email='[email protected]',
58-
url='https://github.com/sqlalchemy/sqlalchemy-collectd',
59-
license='MIT',
59+
keywords="SQLAlchemy collectd",
60+
author="Mike Bayer",
61+
author_email="[email protected]",
62+
url="https://github.com/sqlalchemy/sqlalchemy-collectd",
63+
license="MIT",
6064
packages=find_packages(".", exclude=["examples*", "*.tests"]),
6165
include_package_data=True,
62-
tests_require=['pytest', 'mock'],
63-
cmdclass={'test': PyTest},
66+
tests_require=["pytest", "mock"],
67+
cmdclass={"test": PyTest},
6468
zip_safe=False,
6569
install_requires=requires,
6670
entry_points={
67-
'sqlalchemy.plugins': [
68-
'collectd = sqlalchemy_collectd.client.plugin:Plugin'
71+
"sqlalchemy.plugins": [
72+
"collectd = sqlalchemy_collectd.client.plugin:Plugin"
6973
]
70-
}
74+
},
7175
)

sqlalchemy_collectd/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.0.4'
1+
__version__ = "0.0.4"

sqlalchemy_collectd/client/collector.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
from sqlalchemy import event
1+
import logging
22
import threading
33
import weakref
4-
import logging
4+
5+
from sqlalchemy import event
6+
57
from . import worker
68

79

@@ -56,10 +58,11 @@ def num_pools(self):
5658

5759
@property
5860
def num_checkedout(self):
59-
checkedout = self.connections.\
60-
difference(self.detached).\
61-
difference(self.invalidated).\
62-
difference(self.checkedin)
61+
checkedout = (
62+
self.connections.difference(self.detached)
63+
.difference(self.invalidated)
64+
.difference(self.checkedin)
65+
)
6366
return len(checkedout)
6467

6568
@property
@@ -84,7 +87,6 @@ def num_transactions(self):
8487

8588

8689
class EngineCollector(object):
87-
8890
def __init__(self, collection_target, engine):
8991
self.collection_target = collection_target
9092
self.engine = engine
@@ -157,7 +159,8 @@ def _close_evt(self, dbapi_conn, connection_rec):
157159
def _warn_missing_connection(self, dbapi_conn):
158160
self._warn(
159161
"connection %s was closed but not part of "
160-
"total connections" % dbapi_conn)
162+
"total connections" % dbapi_conn
163+
)
161164

162165
def _warn(self, msg):
163166
self.logger.warn(msg)
@@ -179,6 +182,3 @@ def _close_detached_evt(self, dbapi_conn):
179182
self.collection_target.total_disconnects += 1
180183
except KeyError:
181184
self._warn_missing_connection(dbapi_conn)
182-
183-
184-

sqlalchemy_collectd/client/plugin.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,28 @@
44

55
from sqlalchemy.engine import CreateEnginePlugin
66

7-
from .. import protocol
7+
from . import collector
88
from . import sender
99
from . import worker
10-
from . import collector
10+
from .. import protocol
1111

1212

1313
class Plugin(CreateEnginePlugin):
1414
def __init__(self, url, kwargs):
1515
self.url = url
1616
self.config = {}
1717
self._get_argument(
18-
"collectd_host", "collectd_host", self.config, url, kwargs)
18+
"collectd_host", "collectd_host", self.config, url, kwargs
19+
)
1920
self._get_argument(
20-
"collectd_port", "collectd_port", self.config, url, kwargs)
21+
"collectd_port", "collectd_port", self.config, url, kwargs
22+
)
2123
self._get_argument(
22-
"collectd_report_host", "hostname", self.config, url, kwargs)
24+
"collectd_report_host", "hostname", self.config, url, kwargs
25+
)
2326
self._get_argument(
24-
"collectd_program_name", "progname", self.config, url, kwargs)
27+
"collectd_program_name", "progname", self.config, url, kwargs
28+
)
2529

2630
def _get_argument(self, name, dest_name, dest, url, kwargs):
2731
# favor the URL but pop the name from both
@@ -48,8 +52,12 @@ def engine_created(self, engine):
4852

4953

5054
def start_plugin(
51-
engine, hostname=None, progname=None,
52-
collectd_host="localhost", collectd_port=25827):
55+
engine,
56+
hostname=None,
57+
progname=None,
58+
collectd_host="localhost",
59+
collectd_port=25827,
60+
):
5361

5462
if hostname is None:
5563
hostname = socket.gethostname()
@@ -59,13 +67,12 @@ def start_plugin(
5967

6068
sender_ = sender.Sender(hostname, progname)
6169
collection_target = collector.CollectionTarget.collection_for_name(
62-
progname)
70+
progname
71+
)
6372
collector.EngineCollector(collection_target, engine)
6473

6574
connection = protocol.ClientConnection.for_host_port(
66-
collectd_host, collectd_port)
75+
collectd_host, collectd_port
76+
)
6777

68-
worker.add_target(
69-
connection,
70-
collection_target,
71-
sender_)
78+
worker.add_target(connection, collection_target, sender_)

sqlalchemy_collectd/client/sender.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from .. import protocol
21
from . import internal_types
2+
from .. import protocol
33

44

55
senders = []
@@ -22,33 +22,39 @@ def __init__(self, hostname, stats_name, plugin="sqlalchemy"):
2222
def send(self, connection, collection_target, timestamp, interval, pid):
2323
for protocol_type, sender in senders:
2424
message_sender = protocol.MessageSender(
25-
protocol_type, self.hostname, self.plugin,
26-
plugin_instance=self.stats_name, type_instance=str(pid),
27-
interval=interval
25+
protocol_type,
26+
self.hostname,
27+
self.plugin,
28+
plugin_instance=self.stats_name,
29+
type_instance=str(pid),
30+
interval=interval,
2831
)
2932
sender(message_sender, connection, collection_target, timestamp)
3033

3134

3235
@sends(internal_types.pool)
3336
def _send_pool(message_sender, connection, collection_target, timestamp):
3437
message_sender.send(
35-
connection, timestamp,
38+
connection,
39+
timestamp,
3640
collection_target.num_pools,
3741
collection_target.num_checkedout,
3842
collection_target.num_checkedin,
3943
collection_target.num_detached,
4044
# collection_target.num_invalidated,
41-
collection_target.num_connections
45+
collection_target.num_connections,
4246
)
4347

4448

4549
@sends(internal_types.totals)
4650
def _send_connection_totals(
47-
message_sender, connection, collection_target, timestamp):
51+
message_sender, connection, collection_target, timestamp
52+
):
4853
message_sender.send(
49-
connection, timestamp,
54+
connection,
55+
timestamp,
5056
collection_target.total_checkouts,
5157
collection_target.total_invalidated,
5258
collection_target.total_connects,
53-
collection_target.total_disconnects
59+
collection_target.total_disconnects,
5460
)

sqlalchemy_collectd/client/tests/test_plugin.py

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,31 @@
1-
2-
import mock
31
import unittest
42

3+
import mock
54
from sqlalchemy.engine import url as sqla_url
65

76
from .. import plugin
87

98

109
class PluginTest(unittest.TestCase):
11-
1210
def test_start_no_args(self):
1311
with mock.patch.object(plugin, "start_plugin") as start_plugin:
14-
url = sqla_url.URL(
15-
"mysql+pymysql://scott:tiger@localhost/"
16-
)
12+
url = sqla_url.URL("mysql+pymysql://scott:tiger@localhost/")
1713
p = plugin.Plugin(url, {})
1814
engine = mock.Mock()
1915
p.engine_created(engine)
2016

21-
self.assertEqual(
22-
[mock.call(engine)],
23-
start_plugin.mock_calls
24-
)
17+
self.assertEqual([mock.call(engine)], start_plugin.mock_calls)
2518

2619
def test_start_engine_args(self):
2720
with mock.patch.object(plugin, "start_plugin") as start_plugin:
28-
url = sqla_url.URL(
29-
"mysql+pymysql://scott:tiger@localhost/"
30-
)
21+
url = sqla_url.URL("mysql+pymysql://scott:tiger@localhost/")
3122
p = plugin.Plugin(url, {"collectd_host": "127.0.0.1"})
3223
engine = mock.Mock()
3324
p.engine_created(engine)
3425

3526
self.assertEqual(
3627
[mock.call(engine, collectd_host="127.0.0.1")],
37-
start_plugin.mock_calls
28+
start_plugin.mock_calls,
3829
)
3930

4031
def test_start_url_args(self):
@@ -50,7 +41,7 @@ def test_start_url_args(self):
5041

5142
self.assertEqual(
5243
[mock.call(engine, collectd_host="127.0.0.1")],
53-
start_plugin.mock_calls
44+
start_plugin.mock_calls,
5445
)
5546
self.assertEqual({"somekey": "somevalue"}, url.query)
5647
self.assertEqual({"unrelated": "bar"}, kwargs)
@@ -69,7 +60,7 @@ def test_start_both_args(self):
6960
# argument is popped from both but favors url argument
7061
self.assertEqual(
7162
[mock.call(engine, collectd_host="127.0.0.1")],
72-
start_plugin.mock_calls
63+
start_plugin.mock_calls,
7364
)
7465
self.assertEqual({}, url.query)
7566
self.assertEqual({}, kwargs)

0 commit comments

Comments
 (0)