Skip to content

Commit 7ec37dd

Browse files
authored
Add Python Black format check and apply format changes (#3)
* Add Python Black format check and apply format changes Bring this repo in line with our standards to use Python Black formatting conventions, and enforce it in the lint checks. * Split lint from tests and only run on 3.9 (black requires 3.6+) * Ignore spurious E203 from flake8 (see PyCQA/pycodestyle#914)
1 parent be92518 commit 7ec37dd

File tree

11 files changed

+273
-234
lines changed

11 files changed

+273
-234
lines changed

.github/workflows/tests.yaml

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,22 @@ on:
44
- pull_request
55

66
jobs:
7-
lint-unit-and-func-tests:
8-
name: Lint, Unit, & Functional Tests
7+
lint:
8+
name: Lint
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: Check out code
12+
uses: actions/checkout@v2
13+
- name: Setup Python
14+
uses: actions/setup-python@v2
15+
with:
16+
python-version: 3.9
17+
- name: Install Tox
18+
run: pip install tox
19+
- name: Run lint
20+
run: tox -e lint
21+
unit-and-func-tests:
22+
name: Unit, & Functional Tests
923
runs-on: ubuntu-latest
1024
strategy:
1125
matrix:
@@ -19,8 +33,8 @@ jobs:
1933
python-version: ${{ matrix.python }}
2034
- name: Install Tox
2135
run: pip install tox
22-
- name: Run lint, unit, and functional tests
23-
run: tox
36+
- name: Run unit & functional tests
37+
run: tox -e unit,functional
2438

2539
# TODO
2640
#integration-test:

loadbalancer_interface/base.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,40 @@ def __init__(self, charm, relation_name):
1919
# Future-proof against the need to evolve the relation protocol
2020
# by ensuring that we agree on a version number before starting.
2121
# This may or may not be made moot by a future feature in Juju.
22-
for event in (charm.on[relation_name].relation_created,
23-
charm.on.leader_elected,
24-
charm.on.upgrade_charm):
22+
for event in (
23+
charm.on[relation_name].relation_created,
24+
charm.on.leader_elected,
25+
charm.on.upgrade_charm,
26+
):
2527
self.framework.observe(event, self._set_version)
2628

2729
def _set_version(self, event):
2830
if self.unit.is_leader():
29-
if hasattr(event, 'relation'):
31+
if hasattr(event, "relation"):
3032
relations = [event.relation]
3133
else:
3234
relations = self.model.relations.get(self.relation_name, [])
3335
for relation in relations:
34-
relation.data[self.app]['version'] = str(schemas.max_version)
36+
relation.data[self.app]["version"] = str(schemas.max_version)
3537

3638
@cached_property
3739
def relations(self):
3840
relations = self.model.relations.get(self.relation_name, [])
39-
return [relation
40-
for relation in sorted(relations, key=attrgetter('id'))
41-
if self._schema(relation)]
41+
return [
42+
relation
43+
for relation in sorted(relations, key=attrgetter("id"))
44+
if self._schema(relation)
45+
]
4246

4347
def _schema(self, relation=None):
4448
if relation is None:
4549
return schemas.versions[schemas.max_version]
4650
if relation.app not in relation.data:
4751
return None
4852
data = relation.data[relation.app]
49-
if 'version' not in data:
53+
if "version" not in data:
5054
return None
51-
remote_version = int(data['version'])
55+
remote_version = int(data["version"])
5256
return schemas.versions[min((schemas.max_version, remote_version))]
5357

5458
@property

loadbalancer_interface/consumer.py

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ class LBConsumersEvents(ObjectEvents):
2121

2222

2323
class LBConsumers(VersionedInterface):
24-
""" API used to interact with consumers of a loadbalancer provider.
25-
"""
24+
"""API used to interact with consumers of a loadbalancer provider."""
25+
2626
state = StoredState()
2727
on = LBConsumersEvents()
2828

@@ -31,9 +31,11 @@ def __init__(self, charm, relation_name):
3131
self.relation_name = relation_name
3232
self.state.set_default(known_requests={})
3333

34-
for event in (charm.on[relation_name].relation_created,
35-
charm.on[relation_name].relation_joined,
36-
charm.on[relation_name].relation_changed):
34+
for event in (
35+
charm.on[relation_name].relation_created,
36+
charm.on[relation_name].relation_joined,
37+
charm.on[relation_name].relation_changed,
38+
):
3739
self.framework.observe(event, self._check_consumers)
3840

3941
def _check_consumers(self, event):
@@ -42,8 +44,7 @@ def _check_consumers(self, event):
4244

4345
@cached_property
4446
def all_requests(self):
45-
""" A list of all current consumer requests.
46-
"""
47+
"""A list of all current consumer requests."""
4748
if not self.unit.is_leader():
4849
# Only the leader can process requests, so avoid mistakes
4950
# by not even reading the requests if not the leader.
@@ -54,17 +55,15 @@ def all_requests(self):
5455
local_data = relation.data[self.app]
5556
remote_data = relation.data[relation.app]
5657
for key, request_sdata in sorted(remote_data.items()):
57-
if not key.startswith('request_'):
58+
if not key.startswith("request_"):
5859
continue
59-
name = key[len('request_'):]
60-
response_sdata = local_data.get('response_' + name)
61-
request = schema.Request.loads(name,
62-
request_sdata,
63-
response_sdata)
60+
name = key[len("request_") :]
61+
response_sdata = local_data.get("response_" + name)
62+
request = schema.Request.loads(name, request_sdata, response_sdata)
6463
request.relation = relation
6564
if not request.backends:
66-
for unit in sorted(relation.units, key=attrgetter('name')):
67-
addr = relation.data[unit].get('ingress-address')
65+
for unit in sorted(relation.units, key=attrgetter("name")):
66+
addr = relation.data[unit].get("ingress-address")
6867
if addr:
6968
request.backends.append(addr)
7069
requests.append(request)
@@ -73,53 +72,56 @@ def all_requests(self):
7372

7473
@property
7574
def new_requests(self):
76-
""" A list of requests with changes or no response.
77-
"""
78-
return [request for request in self.all_requests
79-
if request.hash != self.state.known_requests[request.id]]
75+
"""A list of requests with changes or no response."""
76+
return [
77+
request
78+
for request in self.all_requests
79+
if request.hash != self.state.known_requests[request.id]
80+
]
8081

8182
@property
8283
def removed_requests(self):
83-
""" A list of requests which have been removed, either explicitly or
84+
"""A list of requests which have been removed, either explicitly or
8485
because the relation was removed.
8586
"""
8687
current_ids = {request.id for request in self.all_requests}
8788
unknown_ids = self.state.known_requests.keys() - current_ids
8889
schema = self._schema()
89-
return [schema.Request._from_id(req_id, self.relations)
90-
for req_id in sorted(unknown_ids)]
90+
return [
91+
schema.Request._from_id(req_id, self.relations)
92+
for req_id in sorted(unknown_ids)
93+
]
9194

9295
def send_response(self, request):
93-
""" Send a specific request's response.
94-
"""
96+
"""Send a specific request's response."""
9597
request.response.received_hash = request.sent_hash
96-
key = 'response_' + request.name
98+
key = "response_" + request.name
9799
request.relation.data[self.app][key] = request.response.dumps()
98100
self.state.known_requests[request.id] = request.hash
99101
if not self.new_requests:
100102
try:
101103
from charms.reactive import clear_flag
102-
prefix = 'endpoint.' + self.relation_name
103-
clear_flag(prefix + '.requests_changed')
104+
105+
prefix = "endpoint." + self.relation_name
106+
clear_flag(prefix + ".requests_changed")
104107
except ImportError:
105108
pass # not being used in a reactive charm
106109

107110
def revoke_response(self, request):
108-
""" Revoke / remove the response for a given request.
109-
"""
111+
"""Revoke / remove the response for a given request."""
110112
if request.id:
111113
self.state.known_requests.pop(request.id, None)
112114
if request.relation:
113-
key = 'response_' + request.name
115+
key = "response_" + request.name
114116
request.relation.data.get(self.app, {}).pop(key, None)
115117

116118
@property
117119
def is_changed(self):
118120
return bool(self.new_requests or self.removed_requests)
119121

120122
def manage_flags(self):
121-
""" Used to interact with charms.reactive-base charms.
122-
"""
123+
"""Used to interact with charms.reactive-base charms."""
123124
from charms.reactive import toggle_flag
124-
prefix = 'endpoint.' + self.relation_name
125-
toggle_flag(prefix + '.requests_changed', self.is_changed)
125+
126+
prefix = "endpoint." + self.relation_name
127+
toggle_flag(prefix + ".requests_changed", self.is_changed)

0 commit comments

Comments
 (0)