Skip to content

Commit

Permalink
Merge pull request #21 from adafruit/pylint-update
Browse files Browse the repository at this point in the history
Ran black, updated to pylint 2.x
  • Loading branch information
kattni authored Mar 16, 2020
2 parents f873915 + 0a1196e commit 5bc88cc
Show file tree
Hide file tree
Showing 23 changed files with 668 additions and 493 deletions.
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
17 changes: 13 additions & 4 deletions adafruit_midi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,16 @@ class MIDI:
"""

def __init__(self, midi_in=None, midi_out=None, *,
in_channel=None, out_channel=0, in_buf_size=30, debug=False):
def __init__(
self,
midi_in=None,
midi_out=None,
*,
in_channel=None,
out_channel=0,
in_buf_size=30,
debug=False
):
if midi_in is None and midi_out is None:
raise ValueError("No midi_in or midi_out provided")
self._midi_in = midi_in
Expand Down Expand Up @@ -135,8 +143,9 @@ def receive(self):
self._in_buf.extend(bytes_in)
del bytes_in

(msg, endplusone,
skipped) = MIDIMessage.from_message_bytes(self._in_buf, self._in_channel)
(msg, endplusone, skipped) = MIDIMessage.from_message_bytes(
self._in_buf, self._in_channel
)
if endplusone != 0:
# This is not particularly efficient as it's copying most of bytearray
# and deleting old one
Expand Down
8 changes: 4 additions & 4 deletions adafruit_midi/channel_pressure.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ class ChannelPressure(MIDIMessage):
:param int pressure: The pressure, 0-127.
"""

_STATUS = 0xd0
_STATUSMASK = 0xf0
_STATUS = 0xD0
_STATUSMASK = 0xF0
LENGTH = 2

def __init__(self, pressure, *, channel=None):
Expand All @@ -56,11 +56,11 @@ def __init__(self, pressure, *, channel=None):
raise self._EX_VALUEERROR_OOR

def __bytes__(self):
return bytes([self._STATUS | (self.channel & self.CHANNELMASK),
self.pressure])
return bytes([self._STATUS | (self.channel & self.CHANNELMASK), self.pressure])

@classmethod
def from_bytes(cls, msg_bytes):
return cls(msg_bytes[1], channel=msg_bytes[0] & cls.CHANNELMASK)


ChannelPressure.register_message_type()
13 changes: 7 additions & 6 deletions adafruit_midi/control_change.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class ControlChange(MIDIMessage):
"""

_STATUS = 0xb0
_STATUSMASK = 0xf0
_STATUS = 0xB0
_STATUSMASK = 0xF0
LENGTH = 3

def __init__(self, control, value, *, channel=None):
Expand All @@ -59,12 +59,13 @@ def __init__(self, control, value, *, channel=None):
raise self._EX_VALUEERROR_OOR

def __bytes__(self):
return bytes([self._STATUS | (self.channel & self.CHANNELMASK),
self.control, self.value])
return bytes(
[self._STATUS | (self.channel & self.CHANNELMASK), self.control, self.value]
)

@classmethod
def from_bytes(cls, msg_bytes):
return cls(msg_bytes[1], msg_bytes[2],
channel=msg_bytes[0] & cls.CHANNELMASK)
return cls(msg_bytes[1], msg_bytes[2], channel=msg_bytes[0] & cls.CHANNELMASK)


ControlChange.register_message_type()
74 changes: 40 additions & 34 deletions adafruit_midi/midi_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,12 @@ def note_parser(note):
if not 0 <= noteidx <= 6:
raise ValueError("Bad note")
sharpen = 0
if note[1] == '#':
if note[1] == "#":
sharpen = 1
elif note[1] == 'b':
elif note[1] == "b":
sharpen = -1
# int may throw exception here
midi_note = (int(note[1 + abs(sharpen):]) * 12
+ NOTE_OFFSET[noteidx]
+ sharpen)
midi_note = int(note[1 + abs(sharpen) :]) * 12 + NOTE_OFFSET[noteidx] + sharpen

return midi_note

Expand All @@ -108,10 +106,11 @@ class MIDIMessage:
This is an *abstract* class.
"""

_STATUS = None
_STATUSMASK = None
LENGTH = None
CHANNELMASK = 0x0f
CHANNELMASK = 0x0F
ENDSTATUS = None

# Commonly used exceptions to save memory
Expand Down Expand Up @@ -150,9 +149,9 @@ def register_message_type(cls):
insert_idx = idx
break

MIDIMessage._statusandmask_to_class.insert(insert_idx,
((cls._STATUS, cls._STATUSMASK), cls))

MIDIMessage._statusandmask_to_class.insert(
insert_idx, ((cls._STATUS, cls._STATUSMASK), cls)
)

# pylint: disable=too-many-arguments
@classmethod
Expand All @@ -171,8 +170,7 @@ def _search_eom_status(cls, buf, eom_status, msgstartidx, msgendidxplusone, endi
else:
bad_termination = True
break
else:
msgendidxplusone += 1
msgendidxplusone += 1

if good_termination or bad_termination:
msgendidxplusone += 1
Expand All @@ -199,22 +197,27 @@ def _match_message_status(cls, buf, msgstartidx, msgendidxplusone, endidx):
break

if msgclass.LENGTH < 0: # indicator of variable length message
(msgendidxplusone,
terminated_msg,
bad_termination) = cls._search_eom_status(buf,
msgclass.ENDSTATUS,
msgstartidx,
msgendidxplusone,
endidx)
(
msgendidxplusone,
terminated_msg,
bad_termination,
) = cls._search_eom_status(
buf, msgclass.ENDSTATUS, msgstartidx, msgendidxplusone, endidx
)
if not terminated_msg:
complete_msg = False
else: # fixed length message
else: # fixed length message
msgendidxplusone = msgstartidx + msgclass.LENGTH
break

return (msgclass, status,
known_msg, complete_msg, bad_termination,
msgendidxplusone)
return (
msgclass,
status,
known_msg,
complete_msg,
bad_termination,
msgendidxplusone,
)

# pylint: disable=too-many-locals,too-many-branches
@classmethod
Expand Down Expand Up @@ -247,23 +250,24 @@ def from_message_bytes(cls, midibytes, channel_in):
return (None, endidx + 1, skipped)

# Try and match the status byte found in midibytes
(msgclass,
status,
known_message,
complete_message,
bad_termination,
msgendidxplusone) = cls._match_message_status(midibytes,
msgstartidx,
msgendidxplusone,
endidx)
(
msgclass,
status,
known_message,
complete_message,
bad_termination,
msgendidxplusone,
) = cls._match_message_status(
midibytes, msgstartidx, msgendidxplusone, endidx
)
channel_match_orna = True
if complete_message and not bad_termination:
try:
msg = msgclass.from_bytes(midibytes[msgstartidx:msgendidxplusone])
if msg.channel is not None:
channel_match_orna = channel_filter(msg.channel, channel_in)

except(ValueError, TypeError) as ex:
except (ValueError, TypeError) as ex:
msg = MIDIBadEvent(midibytes[msgstartidx:msgendidxplusone], ex)

# break out of while loop for a complete message on good channel
Expand All @@ -272,8 +276,8 @@ def from_message_bytes(cls, midibytes, channel_in):
if complete_message:
if channel_match_orna:
break
else: # advance to next message
msgstartidx = msgendidxplusone
# advance to next message
msgstartidx = msgendidxplusone
else:
# Important case of a known message but one that is not
# yet complete - leave bytes in buffer and wait for more
Expand Down Expand Up @@ -314,6 +318,7 @@ class MIDIUnknownEvent(MIDIMessage):
This can either occur because there is no class representing the message
or because it is not imported.
"""

LENGTH = -1

def __init__(self, status):
Expand All @@ -331,6 +336,7 @@ class MIDIBadEvent(MIDIMessage):
This could be due to status bytes appearing where data bytes are expected.
The channel property will not be set.
"""

LENGTH = -1

def __init__(self, msg_bytes, exception):
Expand Down
11 changes: 6 additions & 5 deletions adafruit_midi/note_off.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class NoteOff(MIDIMessage):
"""

_STATUS = 0x80
_STATUSMASK = 0xf0
_STATUSMASK = 0xF0
LENGTH = 3

def __init__(self, note, velocity=0, *, channel=None):
Expand All @@ -60,12 +60,13 @@ def __init__(self, note, velocity=0, *, channel=None):
raise self._EX_VALUEERROR_OOR

def __bytes__(self):
return bytes([self._STATUS | (self.channel & self.CHANNELMASK),
self.note, self.velocity])
return bytes(
[self._STATUS | (self.channel & self.CHANNELMASK), self.note, self.velocity]
)

@classmethod
def from_bytes(cls, msg_bytes):
return cls(msg_bytes[1], msg_bytes[2],
channel=msg_bytes[0] & cls.CHANNELMASK)
return cls(msg_bytes[1], msg_bytes[2], channel=msg_bytes[0] & cls.CHANNELMASK)


NoteOff.register_message_type()
11 changes: 6 additions & 5 deletions adafruit_midi/note_on.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class NoteOn(MIDIMessage):
"""

_STATUS = 0x90
_STATUSMASK = 0xf0
_STATUSMASK = 0xF0
LENGTH = 3

def __init__(self, note, velocity=127, *, channel=None):
Expand All @@ -60,12 +60,13 @@ def __init__(self, note, velocity=127, *, channel=None):
raise self._EX_VALUEERROR_OOR

def __bytes__(self):
return bytes([self._STATUS | (self.channel & self.CHANNELMASK),
self.note, self.velocity])
return bytes(
[self._STATUS | (self.channel & self.CHANNELMASK), self.note, self.velocity]
)

@classmethod
def from_bytes(cls, msg_bytes):
return cls(msg_bytes[1], msg_bytes[2],
channel=msg_bytes[0] & cls.CHANNELMASK)
return cls(msg_bytes[1], msg_bytes[2], channel=msg_bytes[0] & cls.CHANNELMASK)


NoteOn.register_message_type()
20 changes: 13 additions & 7 deletions adafruit_midi/pitch_bend.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ class PitchBend(MIDIMessage):
bend from 0 through 8192 (midpoint, no bend) to 16383.
"""

_STATUS = 0xe0
_STATUSMASK = 0xf0
_STATUS = 0xE0
_STATUSMASK = 0xF0
LENGTH = 3

def __init__(self, pitch_bend, *, channel=None):
Expand All @@ -57,13 +57,19 @@ def __init__(self, pitch_bend, *, channel=None):
raise self._EX_VALUEERROR_OOR

def __bytes__(self):
return bytes([self._STATUS | (self.channel & self.CHANNELMASK),
self.pitch_bend & 0x7f,
(self.pitch_bend >> 7) & 0x7f])
return bytes(
[
self._STATUS | (self.channel & self.CHANNELMASK),
self.pitch_bend & 0x7F,
(self.pitch_bend >> 7) & 0x7F,
]
)

@classmethod
def from_bytes(cls, msg_bytes):
return cls(msg_bytes[2] << 7 | msg_bytes[1],
channel=msg_bytes[0] & cls.CHANNELMASK)
return cls(
msg_bytes[2] << 7 | msg_bytes[1], channel=msg_bytes[0] & cls.CHANNELMASK
)


PitchBend.register_message_type()
13 changes: 7 additions & 6 deletions adafruit_midi/polyphonic_key_pressure.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class PolyphonicKeyPressure(MIDIMessage):
:param int pressure: The pressure, 0-127.
"""

_STATUS = 0xa0
_STATUSMASK = 0xf0
_STATUS = 0xA0
_STATUSMASK = 0xF0
LENGTH = 3

def __init__(self, note, pressure, *, channel=None):
Expand All @@ -59,12 +59,13 @@ def __init__(self, note, pressure, *, channel=None):
raise self._EX_VALUEERROR_OOR

def __bytes__(self):
return bytes([self._STATUS | (self.channel & self.CHANNELMASK),
self.note, self.pressure])
return bytes(
[self._STATUS | (self.channel & self.CHANNELMASK), self.note, self.pressure]
)

@classmethod
def from_bytes(cls, msg_bytes):
return cls(msg_bytes[1], msg_bytes[2],
channel=msg_bytes[0] & cls.CHANNELMASK)
return cls(msg_bytes[1], msg_bytes[2], channel=msg_bytes[0] & cls.CHANNELMASK)


PolyphonicKeyPressure.register_message_type()
8 changes: 4 additions & 4 deletions adafruit_midi/program_change.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ class ProgramChange(MIDIMessage):
:param int patch: The new program/patch number to use, 0-127.
"""

_STATUS = 0xc0
_STATUSMASK = 0xf0
_STATUS = 0xC0
_STATUSMASK = 0xF0
LENGTH = 2

def __init__(self, patch, *, channel=None):
Expand All @@ -56,11 +56,11 @@ def __init__(self, patch, *, channel=None):
raise self._EX_VALUEERROR_OOR

def __bytes__(self):
return bytes([self._STATUS | (self.channel & self.CHANNELMASK),
self.patch])
return bytes([self._STATUS | (self.channel & self.CHANNELMASK), self.patch])

@classmethod
def from_bytes(cls, msg_bytes):
return cls(msg_bytes[1], channel=msg_bytes[0] & cls.CHANNELMASK)


ProgramChange.register_message_type()
Loading

0 comments on commit 5bc88cc

Please sign in to comment.