Skip to content

Commit

Permalink
Merge pull request #564 from akx/ruff
Browse files Browse the repository at this point in the history
Switch linting to Ruff; fix issues
  • Loading branch information
rdoursenaud committed Nov 29, 2023
2 parents 45b5151 + 92d365b commit 4de3e3c
Show file tree
Hide file tree
Showing 40 changed files with 199 additions and 82 deletions.
8 changes: 0 additions & 8 deletions .flake8

This file was deleted.

6 changes: 4 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ jobs:
run: python3 -m pip install --upgrade pip setuptools wheel
- name: Install mido in dev mode
run: python3 -m pip install --quiet .[lint-code]
- name: Lint code with flake
run: flake8
- name: Lint code with ruff
run: ruff check .
env:
RUFF_OUTPUT_FORMAT: github

reuse:
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
exclude SECURITY.md
prune docs/_build
prune logo
include README.rst LICENSE .readthedocs.yaml .flake8 .reuse/dep5
include README.rst LICENSE .readthedocs.yaml .reuse/dep5
include docs/_static/.gitkeep
recursive-include docs *.bat
recursive-include docs *.py
Expand Down
6 changes: 3 additions & 3 deletions docs/contributing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,12 @@ It's good practice to check your changes *locally* before submitting.
Linting
^^^^^^^

Linting is done with `flake8 <https://flake8.pycqa.org/en/latest/>`_.
Its configuration can be found in `.flake8`.
Linting is done with `ruff <https://docs.astral.sh/ruff>`_.
Its configuration can be found in `pyproject.toml`.

You can lint your code using::

flake8
ruff check .


Copyright and REUSE Compliance
Expand Down
2 changes: 1 addition & 1 deletion examples/midifiles/create_midi_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import random
import sys

from mido import Message, MidiFile, MidiTrack, MAX_PITCHWHEEL
from mido import MAX_PITCHWHEEL, Message, MidiFile, MidiTrack

notes = [64, 64 + 7, 64 + 12]

Expand Down
3 changes: 2 additions & 1 deletion examples/midifiles/midifile_to_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
#
# SPDX-License-Identifier: MIT

import sys
import json
import sys

import mido


Expand Down
4 changes: 3 additions & 1 deletion examples/using_rtmidi_directly.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
http://pypi.python.org/pypi/python-rtmidi/
"""
import time
import mido

import rtmidi

import mido

midiout = rtmidi.MidiOut()
available_ports = midiout.get_ports()

Expand Down
2 changes: 0 additions & 2 deletions extras/hid_joystick.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@
"""
import struct
import select

JS_EVENT_BUTTON = 0x1
JS_EVENT_AXIS = 0x2
Expand Down Expand Up @@ -251,7 +250,6 @@ def play_drums(dev, out):


if __name__ == '__main__':
import sys
import mido

with open('/dev/input/js0') as dev:
Expand Down
62 changes: 50 additions & 12 deletions mido/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,23 +92,63 @@
>>> get_input_names()
['MPK mini MIDI 1', 'SH-201']
"""
import os

from . import ports, sockets
from .backends.backend import Backend
from .messages import (Message, parse_string, parse_string_stream,
format_as_string, MIN_PITCHWHEEL, MAX_PITCHWHEEL,
MIN_SONGPOS, MAX_SONGPOS)
from .midifiles import (MidiFile, MidiTrack, merge_tracks,
MetaMessage, UnknownMetaMessage,
bpm2tempo, tempo2bpm, tick2second, second2tick,
KeySignatureError)
from .messages import (
MAX_PITCHWHEEL,
MAX_SONGPOS,
MIN_PITCHWHEEL,
MIN_SONGPOS,
Message,
format_as_string,
parse_string,
parse_string_stream,
)
from .midifiles import (
KeySignatureError,
MetaMessage,
MidiFile,
MidiTrack,
UnknownMetaMessage,
bpm2tempo,
merge_tracks,
second2tick,
tempo2bpm,
tick2second,
)
from .parser import Parser, parse, parse_all
from .syx import read_syx_file, write_syx_file
from .version import version_info

# Prevent splat import.
__all__ = []
__all__ = [
"KeySignatureError",
"MAX_PITCHWHEEL",
"MAX_SONGPOS",
"MIN_PITCHWHEEL",
"MIN_SONGPOS",
"Message",
"MetaMessage",
"MidiFile",
"MidiTrack",
"Parser",
"UnknownMetaMessage",
"bpm2tempo",
"format_as_string",
"merge_tracks",
"parse",
"parse_all",
"parse_string",
"parse_string_stream",
"ports",
"read_syx_file",
"second2tick",
"sockets",
"tempo2bpm",
"tick2second",
"version_info",
"write_syx_file",
]


def set_backend(name=None, load=False):
Expand Down Expand Up @@ -137,5 +177,3 @@ def set_backend(name=None, load=False):


set_backend()

del os
7 changes: 2 additions & 5 deletions mido/backends/_parser_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@
#
# SPDX-License-Identifier: MIT

import time
from .. import ports
from ..parser import Parser

import queue
from threading import RLock

import queue
from ..parser import Parser


class ParserQueue:
Expand Down
6 changes: 4 additions & 2 deletions mido/backends/amidi.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
"""
import os
import select
import threading
import subprocess
import threading

from ..messages import Message
from ._common import PortMethods, InputMethods, OutputMethods
from ._common import InputMethods, OutputMethods, PortMethods

"""
Dir Device Name
IO hw:1,0,0 UM-1 MIDI 1
Expand Down
3 changes: 2 additions & 1 deletion mido/backends/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
#
# SPDX-License-Identifier: MIT

import os
import importlib
import os

from .. import ports

DEFAULT_BACKEND = 'mido.backends.rtmidi'
Expand Down
1 change: 1 addition & 0 deletions mido/backends/portmidi.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"""
import ctypes
import threading

from ..ports import BaseInput, BaseOutput, sleep
from . import portmidi_init as pm

Expand Down
2 changes: 1 addition & 1 deletion mido/backends/portmidi_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
Copied straight from Grant Yoshida's portmidizero, with slight
modifications.
"""
import sys
import ctypes
import ctypes.util
import sys

dll_name = ''
if sys.platform == 'darwin':
Expand Down
1 change: 1 addition & 0 deletions mido/backends/pygame.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"""

from pygame import midi

from ..ports import BaseInput, BaseOutput


Expand Down
5 changes: 3 additions & 2 deletions mido/backends/rtmidi.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
import threading

import rtmidi
from ._parser_queue import ParserQueue
from .rtmidi_utils import expand_alsa_port_name

from .. import ports
from ..messages import Message
from ._parser_queue import ParserQueue
from .rtmidi_utils import expand_alsa_port_name


def _get_api_lookup():
Expand Down
1 change: 1 addition & 0 deletions mido/backends/rtmidi_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import queue

import rtmidi_python as rtmidi

# TODO: change this to a relative import if the backend is included in
# the package.
from ..ports import BaseInput, BaseOutput
Expand Down
36 changes: 32 additions & 4 deletions mido/messages/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,35 @@
# SPDX-License-Identifier: MIT

from .checks import check_time
from .specs import (SPEC_LOOKUP, SPEC_BY_TYPE, SPEC_BY_STATUS,
MIN_PITCHWHEEL, MAX_PITCHWHEEL, MIN_SONGPOS, MAX_SONGPOS)
from .messages import (BaseMessage, Message, parse_string,
format_as_string, parse_string_stream)
from .messages import (
BaseMessage,
Message,
format_as_string,
parse_string,
parse_string_stream,
)
from .specs import (
MAX_PITCHWHEEL,
MAX_SONGPOS,
MIN_PITCHWHEEL,
MIN_SONGPOS,
SPEC_BY_STATUS,
SPEC_BY_TYPE,
SPEC_LOOKUP,
)

__all__ = [
"BaseMessage",
"MAX_PITCHWHEEL",
"MAX_SONGPOS",
"MIN_PITCHWHEEL",
"MIN_SONGPOS",
"Message",
"SPEC_BY_STATUS",
"SPEC_BY_TYPE",
"SPEC_LOOKUP",
"check_time",
"format_as_string",
"parse_string",
"parse_string_stream",
]
15 changes: 10 additions & 5 deletions mido/messages/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
# SPDX-License-Identifier: MIT

from numbers import Integral, Real
from .specs import (SPEC_BY_TYPE, MIN_SONGPOS, MAX_SONGPOS,
MIN_PITCHWHEEL, MAX_PITCHWHEEL)

from .specs import (
MAX_PITCHWHEEL,
MAX_SONGPOS,
MIN_PITCHWHEEL,
MIN_SONGPOS,
SPEC_BY_TYPE,
)


def check_type(type_):
Expand Down Expand Up @@ -67,8 +73,6 @@ def check_time(time):


_CHECKS = {
'type': check_type,
'data': check_data,
'channel': check_channel,
'control': check_data_byte,
'data': check_data,
Expand All @@ -79,9 +83,10 @@ def check_time(time):
'pos': check_pos,
'program': check_data_byte,
'song': check_data_byte,
'time': check_time,
'type': check_type,
'value': check_data_byte,
'velocity': check_data_byte,
'time': check_time,
}


Expand Down
10 changes: 7 additions & 3 deletions mido/messages/decode.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
#
# SPDX-License-Identifier: MIT

from .specs import (SYSEX_START, SYSEX_END,
SPEC_BY_STATUS, CHANNEL_MESSAGES,
MIN_PITCHWHEEL)
from .checks import check_data
from .specs import (
CHANNEL_MESSAGES,
MIN_PITCHWHEEL,
SPEC_BY_STATUS,
SYSEX_END,
SYSEX_START,
)


def _decode_sysex_data(data):
Expand Down
2 changes: 1 addition & 1 deletion mido/messages/encode.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# SPDX-License-Identifier: MIT

from .specs import CHANNEL_MESSAGES, SPEC_BY_TYPE, MIN_PITCHWHEEL
from .specs import CHANNEL_MESSAGES, MIN_PITCHWHEEL, SPEC_BY_TYPE


def _encode_pitchwheel(msg):
Expand Down
4 changes: 2 additions & 2 deletions mido/messages/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

import re

from .checks import check_msgdict, check_value, check_data
from .checks import check_data, check_msgdict, check_value
from .decode import decode_message
from .encode import encode_message
from .specs import make_msgdict, SPEC_BY_TYPE, REALTIME_TYPES
from .specs import REALTIME_TYPES, SPEC_BY_TYPE, make_msgdict
from .strings import msg2str, str2msg


Expand Down

0 comments on commit 4de3e3c

Please sign in to comment.