Skip to content

Commit 2052ff6

Browse files
authored
Merge pull request #74 from adafruit/pylint-update
Ran black, updated to pylint 2.x
2 parents f50290b + e5385fa commit 2052ff6

28 files changed

+763
-411
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jobs:
4040
source actions-ci/install.sh
4141
- name: Pip install pylint, black, & Sphinx
4242
run: |
43-
pip install --force-reinstall pylint==1.9.2 black==19.10b0 Sphinx sphinx-rtd-theme
43+
pip install --force-reinstall pylint black==19.10b0 Sphinx sphinx-rtd-theme
4444
- name: Library version
4545
run: git describe --dirty --always --tags
4646
- name: PyLint

adafruit_ble/__init__.py

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@
2626
building on the native `_bleio` module.
2727
2828
"""
29-
#pylint: disable=wrong-import-position
29+
# pylint: disable=wrong-import-position
3030
import sys
31-
if sys.implementation.name == 'circuitpython' and sys.implementation.version[0] <= 4:
31+
32+
if sys.implementation.name == "circuitpython" and sys.implementation.version[0] <= 4:
3233
raise ImportError(
33-
"This release is not compatible with CircuitPython 4.x; use library release 1.x.x")
34-
#pylint: enable=wrong-import-position
34+
"This release is not compatible with CircuitPython 4.x; use library release 1.x.x"
35+
)
36+
# pylint: enable=wrong-import-position
3537

3638
import _bleio
3739

@@ -41,6 +43,7 @@
4143
__version__ = "0.0.0-auto.0"
4244
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BLE.git"
4345

46+
4447
class BLEConnection:
4548
"""
4649
Represents a connection to a peer BLE device.
@@ -49,6 +52,7 @@ class BLEConnection:
4952
:param bleio_connection _bleio.Connection: the native `_bleio.Connection` object to wrap
5053
5154
"""
55+
5256
def __init__(self, bleio_connection):
5357
self._bleio_connection = bleio_connection
5458
# _bleio.Service objects representing services found during discovery.
@@ -61,7 +65,9 @@ def _discover_remote(self, uuid):
6165
if uuid in self._discovered_bleio_services:
6266
remote_service = self._discovered_bleio_services[uuid]
6367
else:
64-
results = self._bleio_connection.discover_remote_services((uuid.bleio_uuid,))
68+
results = self._bleio_connection.discover_remote_services(
69+
(uuid.bleio_uuid,)
70+
)
6571
if results:
6672
remote_service = results[0]
6773
self._discovered_bleio_services[uuid] = remote_service
@@ -140,6 +146,7 @@ def disconnect(self):
140146
"""Disconnect from peer."""
141147
self._bleio_connection.disconnect()
142148

149+
143150
class BLERadio:
144151
"""
145152
BLERadio provides the interfaces for BLE advertising,
@@ -172,17 +179,28 @@ def start_advertising(self, advertisement, scan_response=None, interval=0.1):
172179
scan_response.tx_power = self.tx_power
173180
if scan_response:
174181
scan_response_bytes = bytes(scan_response)
175-
self._adapter.start_advertising(advertisement_bytes,
176-
scan_response=scan_response_bytes,
177-
connectable=advertisement.connectable,
178-
interval=interval)
182+
self._adapter.start_advertising(
183+
advertisement_bytes,
184+
scan_response=scan_response_bytes,
185+
connectable=advertisement.connectable,
186+
interval=interval,
187+
)
179188

180189
def stop_advertising(self):
181190
"""Stops advertising."""
182191
self._adapter.stop_advertising()
183192

184-
def start_scan(self, *advertisement_types, buffer_size=512, extended=False, timeout=None,
185-
interval=0.1, window=0.1, minimum_rssi=-80, active=True):
193+
def start_scan(
194+
self,
195+
*advertisement_types,
196+
buffer_size=512,
197+
extended=False,
198+
timeout=None,
199+
interval=0.1,
200+
window=0.1,
201+
minimum_rssi=-80,
202+
active=True
203+
):
186204
"""
187205
Starts scanning. Returns an iterator of advertisement objects of the types given in
188206
advertisement_types. The iterator will block until an advertisement is heard or the scan
@@ -214,10 +232,16 @@ def start_scan(self, *advertisement_types, buffer_size=512, extended=False, time
214232
prefixes = b""
215233
if advertisement_types:
216234
prefixes = b"".join(adv.prefix for adv in advertisement_types)
217-
for entry in self._adapter.start_scan(prefixes=prefixes, buffer_size=buffer_size,
218-
extended=extended, timeout=timeout,
219-
interval=interval, window=window,
220-
minimum_rssi=minimum_rssi, active=active):
235+
for entry in self._adapter.start_scan(
236+
prefixes=prefixes,
237+
buffer_size=buffer_size,
238+
extended=extended,
239+
timeout=timeout,
240+
interval=interval,
241+
window=window,
242+
minimum_rssi=minimum_rssi,
243+
active=active,
244+
):
221245
adv_type = Advertisement
222246
for possible_type in advertisement_types:
223247
if possible_type.matches(entry) and issubclass(possible_type, adv_type):

adafruit_ble/advertising/__init__.py

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,16 @@
2525

2626
import struct
2727

28+
2829
def to_hex(seq):
2930
"""Pretty prints a byte sequence as hex values."""
3031
return " ".join("{:02x}".format(v) for v in seq)
3132

33+
3234
def to_bytes_literal(seq):
3335
"""Prints a byte sequence as a Python bytes literal that only uses hex encoding."""
34-
return "b\"" + "".join("\\x{:02x}".format(v) for v in seq) + "\""
36+
return 'b"' + "".join("\\x{:02x}".format(v) for v in seq) + '"'
37+
3538

3639
def decode_data(data, *, key_encoding="B"):
3740
"""Helper which decodes length encoded structures into a dictionary with the given key
@@ -45,7 +48,7 @@ def decode_data(data, *, key_encoding="B"):
4548
if item_length == 0:
4649
break
4750
key = struct.unpack_from(key_encoding, data, i)[0]
48-
value = data[i + key_size:i + item_length]
51+
value = data[i + key_size : i + item_length]
4952
if key in data_dict:
5053
if not isinstance(data_dict[key], list):
5154
data_dict[key] = [data_dict[key]]
@@ -55,6 +58,7 @@ def decode_data(data, *, key_encoding="B"):
5558
i += item_length
5659
return data_dict
5760

61+
5862
def compute_length(data_dict, *, key_encoding="B"):
5963
"""Computes the length of the encoded data dictionary."""
6064
value_size = 0
@@ -66,6 +70,7 @@ def compute_length(data_dict, *, key_encoding="B"):
6670
value_size += len(value)
6771
return len(data_dict) + len(data_dict) * struct.calcsize(key_encoding) + value_size
6872

73+
6974
def encode_data(data_dict, *, key_encoding="B"):
7075
"""Helper which encodes dictionaries into length encoded structures with the given key
7176
encoding."""
@@ -79,17 +84,21 @@ def encode_data(data_dict, *, key_encoding="B"):
7984
item_length = key_size + len(value)
8085
struct.pack_into("B", data, i, item_length)
8186
struct.pack_into(key_encoding, data, i + 1, key)
82-
data[i + 1 + key_size: i + 1 + item_length] = bytes(value)
87+
data[i + 1 + key_size : i + 1 + item_length] = bytes(value)
8388
i += 1 + item_length
8489
return data
8590

91+
8692
class AdvertisingDataField:
8793
"""Top level class for any descriptor classes that live in Advertisement or its subclasses."""
94+
8895
# pylint: disable=too-few-public-methods,unnecessary-pass
8996
pass
9097

98+
9199
class AdvertisingFlag:
92100
"""A single bit flag within an AdvertisingFlags object."""
101+
93102
def __init__(self, bit_position):
94103
self._bitmask = 1 << bit_position
95104

@@ -102,6 +111,7 @@ def __set__(self, obj, value):
102111
else:
103112
obj.flags &= ~self._bitmask
104113

114+
105115
class AdvertisingFlags(AdvertisingDataField):
106116
"""Standard advertising flags"""
107117

@@ -135,10 +145,12 @@ def __str__(self):
135145
parts.append(attr)
136146
return "<AdvertisingFlags {} >".format(" ".join(parts))
137147

148+
138149
class String(AdvertisingDataField):
139150
"""UTF-8 encoded string in an Advertisement.
140151
141152
Not null terminated once encoded because length is always transmitted."""
153+
142154
def __init__(self, *, advertising_data_type):
143155
self._adt = advertising_data_type
144156

@@ -152,8 +164,10 @@ def __get__(self, obj, cls):
152164
def __set__(self, obj, value):
153165
obj.data_dict[self._adt] = value.encode("utf-8")
154166

167+
155168
class Struct(AdvertisingDataField):
156169
"""`struct` encoded data in an Advertisement."""
170+
157171
def __init__(self, struct_format, *, advertising_data_type):
158172
self._format = struct_format
159173
self._adt = advertising_data_type
@@ -171,6 +185,7 @@ def __set__(self, obj, value):
171185

172186
class LazyObjectField(AdvertisingDataField):
173187
"""Non-data descriptor useful for lazily binding a complex object to an advertisement object."""
188+
174189
def __init__(self, cls, attribute_name, *, advertising_data_type, **kwargs):
175190
self._cls = cls
176191
self._attribute_name = attribute_name
@@ -197,15 +212,17 @@ def advertising_data_type(self):
197212
# TODO: Add __set_name__ support to CircuitPython so that we automatically tell the descriptor
198213
# instance the attribute name it has and the class it is on.
199214

215+
200216
class Advertisement:
201217
"""Core Advertisement type"""
202-
prefix = b"\x00" # This is an empty prefix and will match everything.
218+
219+
prefix = b"\x00" # This is an empty prefix and will match everything.
203220
flags = LazyObjectField(AdvertisingFlags, "flags", advertising_data_type=0x01)
204221
short_name = String(advertising_data_type=0x08)
205222
"""Short local device name (shortened to fit)."""
206223
complete_name = String(advertising_data_type=0x09)
207224
"""Complete local device name."""
208-
tx_power = Struct("<b", advertising_data_type=0x0a)
225+
tx_power = Struct("<b", advertising_data_type=0x0A)
209226
"""Transmit power level"""
210227
# DEVICE_ID = 0x10
211228
# """Device identifier."""
@@ -242,7 +259,7 @@ def from_entry(cls, entry):
242259
self = cls()
243260
self.data_dict = decode_data(entry.advertisement_bytes)
244261
self.address = entry.address
245-
self._rssi = entry.rssi # pylint: disable=protected-access
262+
self._rssi = entry.rssi # pylint: disable=protected-access
246263
self.connectable = entry.connectable
247264
self.scan_response = entry.scan_response
248265
self.mutable = False
@@ -272,8 +289,10 @@ def __str__(self):
272289
for attr in dir(self.__class__):
273290
attribute_instance = getattr(self.__class__, attr)
274291
if issubclass(attribute_instance.__class__, AdvertisingDataField):
275-
if (issubclass(attribute_instance.__class__, LazyObjectField) and
276-
not attribute_instance.advertising_data_type in self.data_dict):
292+
if (
293+
issubclass(attribute_instance.__class__, LazyObjectField)
294+
and not attribute_instance.advertising_data_type in self.data_dict
295+
):
277296
# Skip uninstantiated lazy objects; if we get
278297
# their value, they will be be instantiated.
279298
continue
@@ -286,4 +305,6 @@ def __len__(self):
286305
return compute_length(self.data_dict)
287306

288307
def __repr__(self):
289-
return "Advertisement(data={})".format(to_bytes_literal(encode_data(self.data_dict)))
308+
return "Advertisement(data={})".format(
309+
to_bytes_literal(encode_data(self.data_dict))
310+
)

adafruit_ble/advertising/adafruit.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,29 @@
4040
__version__ = "0.0.0-auto.0"
4141
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BLE.git"
4242

43-
_MANUFACTURING_DATA_ADT = const(0xff)
43+
_MANUFACTURING_DATA_ADT = const(0xFF)
4444
_ADAFRUIT_COMPANY_ID = const(0x0822)
4545
_COLOR_DATA_ID = const(0x0000)
4646

4747

4848
class AdafruitColor(Advertisement):
4949
"""Broadcast a single RGB color."""
50+
5051
# This prefix matches all
51-
prefix = struct.pack("<BBHBH",
52-
0x6,
53-
_MANUFACTURING_DATA_ADT,
54-
_ADAFRUIT_COMPANY_ID,
55-
struct.calcsize("<HI"),
56-
_COLOR_DATA_ID)
57-
manufacturer_data = LazyObjectField(ManufacturerData,
58-
"manufacturer_data",
59-
advertising_data_type=_MANUFACTURING_DATA_ADT,
60-
company_id=_ADAFRUIT_COMPANY_ID,
61-
key_encoding="<H")
52+
prefix = struct.pack(
53+
"<BBHBH",
54+
0x6,
55+
_MANUFACTURING_DATA_ADT,
56+
_ADAFRUIT_COMPANY_ID,
57+
struct.calcsize("<HI"),
58+
_COLOR_DATA_ID,
59+
)
60+
manufacturer_data = LazyObjectField(
61+
ManufacturerData,
62+
"manufacturer_data",
63+
advertising_data_type=_MANUFACTURING_DATA_ADT,
64+
company_id=_ADAFRUIT_COMPANY_ID,
65+
key_encoding="<H",
66+
)
6267
color = ManufacturerDataField(_COLOR_DATA_ID, "<I")
6368
"""Color to broadcast as RGB integer."""

0 commit comments

Comments
 (0)