Skip to content

Ran black, updated to pylint 2.x #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
source actions-ci/install.sh
- name: Pip install pylint, black, & Sphinx
run: |
pip install --force-reinstall pylint==1.9.2 black==19.10b0 Sphinx sphinx-rtd-theme
pip install --force-reinstall pylint black==19.10b0 Sphinx sphinx-rtd-theme
- name: Library version
run: git describe --dirty --always --tags
- name: PyLint
Expand Down
10 changes: 6 additions & 4 deletions adafruit_bluefruit_connect/_xyz_packet.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@

from .packet import Packet


class _XYZPacket(Packet):
"""A packet of x, y, z float values. Used for several different Bluefruit controller packets."""

_FMT_PARSE = '<xxfffx'
_FMT_PARSE = "<xxfffx"
PACKET_LENGTH = struct.calcsize(_FMT_PARSE)
# _FMT_CONSTRUCT doesn't include the trailing checksum byte.
_FMT_CONSTRUCT = '<2sfff'
_FMT_CONSTRUCT = "<2sfff"
# _TYPE_HEADER is set by each concrete subclass.

def __init__(self, x, y, z):
Expand All @@ -53,8 +54,9 @@ def __init__(self, x, y, z):
def to_bytes(self):
"""Return the bytes needed to send this packet.
"""
partial_packet = struct.pack(self._FMT_CONSTRUCT, self._TYPE_HEADER,
self._x, self._y, self._z)
partial_packet = struct.pack(
self._FMT_CONSTRUCT, self._TYPE_HEADER, self._x, self._y, self._z
)
return self.add_checksum(partial_packet)

@property
Expand Down
3 changes: 2 additions & 1 deletion adafruit_bluefruit_connect/accelerometer_packet.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@

from ._xyz_packet import _XYZPacket


class AccelerometerPacket(_XYZPacket):
"""A packet of x, y, z float values from an accelerometer."""

# Everything else is handled by _XYZPacket.
_TYPE_HEADER = b'!A'
_TYPE_HEADER = b"!A"


# Register this class with the superclass. This allows the user to import only what is needed.
Expand Down
37 changes: 21 additions & 16 deletions adafruit_bluefruit_connect/button_packet.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,33 @@

from .packet import Packet


class ButtonPacket(Packet):
"""A packet containing a button name and its state."""

BUTTON_1 = '1'
BUTTON_1 = "1"
"""Code for Button 1 on the Bluefruit LE Connect app Control Pad screen."""
BUTTON_2 = '2'
BUTTON_2 = "2"
"""Button 2."""
BUTTON_3 = '3'
BUTTON_3 = "3"
"""Button 3."""
BUTTON_4 = '4'
BUTTON_4 = "4"
"""Button 4."""
#pylint: disable= invalid-name
UP = '5'
# pylint: disable= invalid-name
UP = "5"
"""Up Button."""
DOWN = '6'
DOWN = "6"
"""Down Button."""
LEFT = '7'
LEFT = "7"
"""Left Button."""
RIGHT = '8'
RIGHT = "8"
"""Right Button."""

_FMT_PARSE = '<xxssx'
_FMT_PARSE = "<xxssx"
PACKET_LENGTH = struct.calcsize(_FMT_PARSE)
# _FMT_CONSTRUCT doesn't include the trailing checksum byte.
_FMT_CONSTRUCT = '<2sss'
_TYPE_HEADER = b'!B'
_FMT_CONSTRUCT = "<2sss"
_TYPE_HEADER = b"!B"

def __init__(self, button, pressed):
"""Construct a ButtonPacket from a button name and the button's state.
Expand All @@ -83,14 +84,18 @@ def parse_private(cls, packet):
pylint makes it difficult to call this method _parse(), hence the name.
"""
button, pressed = struct.unpack(cls._FMT_PARSE, packet)
if not pressed in b'01':
if not pressed in b"01":
raise ValueError("Bad button press/release value")
return cls(chr(button[0]), pressed == b'1')
return cls(chr(button[0]), pressed == b"1")

def to_bytes(self):
"""Return the bytes needed to send this packet."""
partial_packet = struct.pack(self._FMT_CONSTRUCT, self._TYPE_HEADER,
self._button, b'1' if self._pressed else b'0')
partial_packet = struct.pack(
self._FMT_CONSTRUCT,
self._TYPE_HEADER,
self._button,
b"1" if self._pressed else b"0",
)
return self.add_checksum(partial_packet)

@property
Expand Down
13 changes: 8 additions & 5 deletions adafruit_bluefruit_connect/color_packet.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@

from .packet import Packet


class ColorPacket(Packet):
"""A packet containing an RGB color value."""

_FMT_PARSE = '<xx3Bx'
_FMT_PARSE = "<xx3Bx"
PACKET_LENGTH = struct.calcsize(_FMT_PARSE)
# _FMT_CONSTRUCT doesn't include the trailing checksum byte.
_FMT_CONSTRUCT = '<2s3B'
_TYPE_HEADER = b'!C'
_FMT_CONSTRUCT = "<2s3B"
_TYPE_HEADER = b"!C"

def __init__(self, color):
"""Construct a ColorPacket from a 3-tuple of RGB values,
Expand All @@ -50,7 +51,7 @@ def __init__(self, color):
or an int color value ``0xRRGGBB``
"""
if isinstance(color, int):
self._color = tuple(color.to_bytes('BBB', 'big'))
self._color = tuple(color.to_bytes("BBB", "big"))
elif len(color) == 3 and all(0 <= c <= 255 for c in color):
self._color = color
else:
Expand All @@ -67,7 +68,9 @@ def parse_private(cls, packet):
def to_bytes(self):
"""Return the bytes needed to send this packet.
"""
partial_packet = struct.pack(self._FMT_CONSTRUCT, self._TYPE_HEADER, *self._color)
partial_packet = struct.pack(
self._FMT_CONSTRUCT, self._TYPE_HEADER, *self._color
)
return self.add_checksum(partial_packet)

@property
Expand Down
3 changes: 2 additions & 1 deletion adafruit_bluefruit_connect/gyro_packet.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@

from ._xyz_packet import _XYZPacket


class GyroPacket(_XYZPacket):
"""A packet of x, y, z float values from a gyroscope."""

# Everything else is handled by _XYZPacket.
_TYPE_HEADER = b'!G'
_TYPE_HEADER = b"!G"


# Register this class with the superclass. This allows the user to import only what is needed.
Expand Down
17 changes: 12 additions & 5 deletions adafruit_bluefruit_connect/location_packet.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@

from .packet import Packet


class LocationPacket(Packet):
"""A packet of latitude, longitude, and altitude values."""

_FMT_PARSE = '<xxfffx'
_FMT_PARSE = "<xxfffx"
PACKET_LENGTH = struct.calcsize(_FMT_PARSE)
# _FMT_CONSTRUCT doesn't include the trailing checksum byte.
_FMT_CONSTRUCT = '<2sfff'
_TYPE_HEADER = b'!L'
_FMT_CONSTRUCT = "<2sfff"
_TYPE_HEADER = b"!L"

def __init__(self, latitude, longitude, altitude):
"""Construct a LocationPacket from the given values."""
Expand All @@ -51,8 +52,13 @@ def __init__(self, latitude, longitude, altitude):
def to_bytes(self):
"""Return the bytes needed to send this packet.
"""
partial_packet = struct.pack(self._FMT_CONSTRUCT, self._TYPE_HEADER,
self._latitude, self._longitude, self._altitude)
partial_packet = struct.pack(
self._FMT_CONSTRUCT,
self._TYPE_HEADER,
self._latitude,
self._longitude,
self._altitude,
)
return self.add_checksum(partial_packet)

@property
Expand All @@ -70,5 +76,6 @@ def altitude(self):
"""The altitude value."""
return self._altitude


# Register this class with the superclass. This allows the user to import only what is needed.
LocationPacket.register_packet_type()
3 changes: 2 additions & 1 deletion adafruit_bluefruit_connect/magnetometer_packet.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@

from ._xyz_packet import _XYZPacket


class MagnetometerPacket(_XYZPacket):
"""A packet of x, y, z float values from a magnetometer."""

# Everything else is handled by _XYZPacket.
_TYPE_HEADER = b'!M'
_TYPE_HEADER = b"!M"


# Register this class with the superclass. This allows the user to import only what is needed.
Expand Down
10 changes: 5 additions & 5 deletions adafruit_bluefruit_connect/packet.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import struct


class Packet:
"""
A Bluefruit app controller packet. A packet consists of these bytes, in order:
Expand Down Expand Up @@ -80,7 +81,7 @@ def from_bytes(cls, packet):
# In case this was called from a subclass, make sure the parsed
# type matches up with the current class.
if not issubclass(packet_class, cls):
raise ValueError('Packet type is not a {}'.format(cls.__name__))
raise ValueError("Packet type is not a {}".format(cls.__name__))

if len(packet) != packet_class.PACKET_LENGTH:
raise ValueError("Wrong length packet")
Expand Down Expand Up @@ -110,14 +111,13 @@ def from_stream(cls, stream):
if not start:
# Timeout: nothing read.
return None
if start == b'!':
if start == b"!":
# Found start of packet.
packet_type = stream.read(1)
if not packet_type:
# Timeout: nothing more read.
return None
else:
break
break
# Didn't find a packet start. Loop and try again.

header = start + packet_type
Expand All @@ -142,7 +142,7 @@ def parse_private(cls, packet):
@staticmethod
def checksum(partial_packet):
"""Compute checksum for bytes, not including the checksum byte itself."""
return ~sum(partial_packet) & 0xff
return ~sum(partial_packet) & 0xFF

def add_checksum(self, partial_packet):
"""Compute the checksum of partial_packet and return a new bytes
Expand Down
13 changes: 8 additions & 5 deletions adafruit_bluefruit_connect/quaternion_packet.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,17 @@

from ._xyz_packet import _XYZPacket


class QuaternionPacket(_XYZPacket):
"""A packet of x, y, z float values. Used for several different Bluefruit controller packets."""

# Use _XYZPacket to handle x, y, z, and add w.

_FMT_PARSE = '<xxffffx'
_FMT_PARSE = "<xxffffx"
PACKET_LENGTH = struct.calcsize(_FMT_PARSE)
# _FMT_CONSTRUCT doesn't include the trailing checksum byte.
_FMT_CONSTRUCT = '<2sffff'
_TYPE_HEADER = b'!Q'
_FMT_CONSTRUCT = "<2sffff"
_TYPE_HEADER = b"!Q"

def __init__(self, x, y, z, w):
"""Construct a QuaternionPacket from the given x, y, z, and w float values."""
Expand All @@ -52,14 +53,16 @@ def __init__(self, x, y, z, w):
def to_bytes(self):
"""Return the bytes needed to send this packet.
"""
partial_packet = struct.pack(self._FMT_CONSTRUCT, self._TYPE_HEADER,
self._x, self._y, self._z, self._w)
partial_packet = struct.pack(
self._FMT_CONSTRUCT, self._TYPE_HEADER, self._x, self._y, self._z, self._w
)
return partial_packet + self.checksum(partial_packet)

@property
def w(self):
"""The w value."""
return self._w


# Register this class with the superclass. This allows the user to import only what is needed.
QuaternionPacket.register_packet_type()
Loading