From 5c0c13e8d0f6ef5e8bb810b4f4efd6e925e8b8a7 Mon Sep 17 00:00:00 2001 From: Josh Klar Date: Wed, 24 Jul 2019 22:56:10 -0700 Subject: [PATCH] Prepare for Black autoformatting: dependencies, string staging, single-quote string override --- Makefile | 4 ++ Pipfile | 2 + Pipfile.lock | 38 +++++++++++- README.md | 11 ++++ kmk/handlers/layers.py | 29 ++++++--- kmk/keyboard_config.py | 5 +- kmk/keys.py | 6 +- kmk/led.py | 12 ++-- kmk/rgb.py | 68 ++++++++++----------- pyproject.toml | 22 +++++++ setup.cfg | 14 ++++- user_keymaps/kdb424/gherkin.py | 2 +- user_keymaps/kdb424/klanck.py | 2 +- user_keymaps/klardotsh/iris_r2.py | 2 +- user_keymaps/klardotsh/klarank_featherm4.py | 2 +- 15 files changed, 159 insertions(+), 60 deletions(-) create mode 100644 pyproject.toml diff --git a/Makefile b/Makefile index 3734385..5ce5448 100644 --- a/Makefile +++ b/Makefile @@ -89,8 +89,12 @@ docker-base-deploy: docker-base devdeps: .devdeps lint: devdeps + @$(PIPENV) run black --check @$(PIPENV) run flake8 +fix-formatting: devdeps + @$(PIPENV) run black . + fix-isort: devdeps @find kmk/ tests/ user_keymaps/ -name "*.py" | xargs $(PIPENV) run isort diff --git a/Pipfile b/Pipfile index e6eb58b..a8579ee 100644 --- a/Pipfile +++ b/Pipfile @@ -18,6 +18,8 @@ ipython = "*" isort = "*" neovim = "*" s3cmd = "*" +black = "==19.3b0" +flake8-quotes = "*" [requires] python_version = "3.7" diff --git a/Pipfile.lock b/Pipfile.lock index 93fb96e..ddcad6c 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "8f51706fcfb278497a6d3083109744210150dc3c644ead11eda3e3cd806e2e14" + "sha256": "019b67ce05e7e68766ba54ddbfd3e2faf5dd81c7ded79f80ff3ef85b0017c99d" }, "pipfile-spec": 6, "requires": { @@ -25,6 +25,20 @@ "index": "pypi", "version": "==1.0.7" }, + "appdirs": { + "hashes": [ + "sha256:9e5896d1372858f8dd3344faf4e5014d21849c756c8d5701f78f8a103b372d92", + "sha256:d8b24664561d0d34ddfaec54636d502d7cea6e29c3eaf68f3df6180863e2166e" + ], + "version": "==1.4.3" + }, + "attrs": { + "hashes": [ + "sha256:69c0dbf2ed392de1cb5ec704444b08a5ef81680a61cb899dc08127123af36a79", + "sha256:f0b870f674851ecbfbbbd364d6b5cbdff9dcedbc7f3f5e18a6891057f21fe399" + ], + "version": "==19.1.0" + }, "backcall": { "hashes": [ "sha256:38ecd85be2c1e78f77fd91700c76e14667dc21e2713b63876c0eb901196e01e4", @@ -32,6 +46,14 @@ ], "version": "==0.1.0" }, + "black": { + "hashes": [ + "sha256:09a9dcb7c46ed496a9850b76e4e825d6049ecd38b611f1224857a79bd985a8cf", + "sha256:68950ffd4d9169716bcb8719a56c07a2f4485354fec061cdd5910aa07369731c" + ], + "index": "pypi", + "version": "==19.3b0" + }, "click": { "hashes": [ "sha256:2335065e6395b9e67ca716de5f7526736bfa6ceead690adf616d925bdc622b13", @@ -86,6 +108,13 @@ "index": "pypi", "version": "==0.8.1" }, + "flake8-quotes": { + "hashes": [ + "sha256:10c9af6b472d4302a8e721c5260856c3f985c5c082b04841aefd2f808ac02038" + ], + "index": "pypi", + "version": "==2.0.1" + }, "greenlet": { "hashes": [ "sha256:000546ad01e6389e98626c1367be58efa613fa82a1be98b0c6fc24b563acc6d0", @@ -304,6 +333,13 @@ ], "version": "==6.10.0" }, + "toml": { + "hashes": [ + "sha256:229f81c57791a41d65e399fc06bf0848bab550a9dfd5ed66df18ce5f05e73d5c", + "sha256:235682dd292d5899d361a811df37e04a8828a5b1da3115886b73cf81ebc9100e" + ], + "version": "==0.10.0" + }, "traitlets": { "hashes": [ "sha256:9c4bd2d267b7153df9152698efb1050a5d84982d3384a37b2c1f7723ba3e7835", diff --git a/README.md b/README.md index 8f42fbd..0fcc6a0 100644 --- a/README.md +++ b/README.md @@ -120,6 +120,17 @@ below (we'll try to keep this up to date!): > their help has been crucial to KMK's success, KMK is not an official Adafruit > project, and the Core team is not compensated by Adafruit for its development. +## Code Style + +KMK uses [Black](https://github.com/psf/black) with a Python 3.6 target and, +[(controversially?)](https://github.com/psf/black/issues/594) single quotes. +Further code styling is enforced with isort and flake8 with several plugins. +`make fix-isort fix-formatting` before a commit is a good idea, and CI will fail +if inbound code does not adhere to these formatting rules. Some exceptions are +found in `setup.cfg` loosening the rules in isolated cases, notably +`user_keymaps` (which is *also* not subject to Black formatting for reasons +documented in `pyproject.toml`). + ## License, Copyright, and Legal All software in this repository is licensed under the [GNU Public License, diff --git a/kmk/handlers/layers.py b/kmk/handlers/layers.py index 599f4d1..8bddbc2 100644 --- a/kmk/handlers/layers.py +++ b/kmk/handlers/layers.py @@ -2,13 +2,17 @@ from kmk.kmktime import ticks_diff, ticks_ms def df_pressed(key, state, *args, **kwargs): - """Switches the default layer""" + ''' + Switches the default layer + ''' state.active_layers[-1] = key.meta.layer return state def mo_pressed(key, state, *args, **kwargs): - """Momentarily activates layer, switches off when you let go""" + ''' + Momentarily activates layer, switches off when you let go + ''' state.active_layers.insert(0, key.meta.layer) return state @@ -32,7 +36,9 @@ def mo_released(key, state, KC, *args, **kwargs): def lm_pressed(key, state, *args, **kwargs): - """As MO(layer) but with mod active""" + ''' + As MO(layer) but with mod active + ''' state.hid_pending = True # Sets the timer start and acts like MO otherwise state.start_time['lm'] = ticks_ms() @@ -41,7 +47,9 @@ def lm_pressed(key, state, *args, **kwargs): def lm_released(key, state, *args, **kwargs): - """As MO(layer) but with mod active""" + ''' + As MO(layer) but with mod active + ''' state.hid_pending = True state.keys_pressed.discard(key.meta.kc) state.start_time['lm'] = None @@ -68,8 +76,9 @@ def lt_released(key, state, *args, **kwargs): def tg_pressed(key, state, *args, **kwargs): - """Toggles the layer (enables it if not active, and vise versa)""" - + ''' + Toggles the layer (enables it if not active, and vise versa) + ''' # See mo_released for implementation details around this try: del_idx = state.active_layers.index(key.meta.layer) @@ -81,7 +90,9 @@ def tg_pressed(key, state, *args, **kwargs): def to_pressed(key, state, *args, **kwargs): - """Activates layer and deactivates all other layers""" + ''' + Activates layer and deactivates all other layers + ''' state.active_layers.clear() state.active_layers.insert(0, key.meta.layer) @@ -89,7 +100,9 @@ def to_pressed(key, state, *args, **kwargs): def tt_pressed(key, state, *args, **kwargs): - """Momentarily activates layer if held, toggles it if tapped repeatedly""" + ''' + Momentarily activates layer if held, toggles it if tapped repeatedly + ''' # TODO Make this work with tap dance to function more correctly, but technically works. if state.start_time['tt'] is None: # Sets the timer start and acts like MO otherwise diff --git a/kmk/keyboard_config.py b/kmk/keyboard_config.py index b4fb95b..e32ce66 100644 --- a/kmk/keyboard_config.py +++ b/kmk/keyboard_config.py @@ -274,10 +274,9 @@ class KeyboardConfig: if self.split_flip and not self.is_master: self.col_pins = list(reversed(self.col_pins)) - - if self.split_side == "Left": + if self.split_side == 'Left': self.split_master_left = self.is_master - elif self.split_side == "Right": + elif self.split_side == 'Right': self.split_master_left = not self.is_master else: self.is_master = True diff --git a/kmk/keys.py b/kmk/keys.py index 1ffb6e5..54ff112 100644 --- a/kmk/keys.py +++ b/kmk/keys.py @@ -361,7 +361,7 @@ def make_argumented_key( else: raise ValueError( 'Argumented key validator failed for unknown reasons. ' - 'This may not be the keymap\'s fault, as a more specific error ' + "This may not be the keymap's fault, as a more specific error " 'should have been raised.', ) @@ -439,7 +439,7 @@ make_key(code=47, names=('LBRACKET', 'LBRC', '[')) make_key(code=48, names=('RBRACKET', 'RBRC', ']')) make_key(code=49, names=('BACKSLASH', 'BSLASH', 'BSLS', '\\')) make_key(code=51, names=('SEMICOLON', 'SCOLON', 'SCLN', ';')) -make_key(code=52, names=('QUOTE', 'QUOT', '\'')) +make_key(code=52, names=('QUOTE', 'QUOT', "'")) make_key(code=53, names=('GRAVE', 'GRV', 'ZKHK', '`')) make_key(code=54, names=('COMMA', 'COMM', ',')) make_key(code=55, names=('DOT', '.')) @@ -539,7 +539,7 @@ make_shifted_key('LBRACKET', names=('LEFT_CURLY_BRACE', 'LCBR', '{')) make_shifted_key('RBRACKET', names=('RIGHT_CURLY_BRACE', 'RCBR', '}')) make_shifted_key('BACKSLASH', names=('PIPE', '|')) make_shifted_key('SEMICOLON', names=('COLON', 'COLN', ':')) -make_shifted_key('QUOTE', names=('DOUBLE_QUOTE', 'DQUO', 'DQT', '\'')) +make_shifted_key('QUOTE', names=('DOUBLE_QUOTE', 'DQUO', 'DQT', '"')) make_shifted_key('COMMA', names=('LEFT_ANGLE_BRACKET', 'LABK', '<')) make_shifted_key('DOT', names=('RIGHT_ANGLE_BRACKET', 'RABK', '>')) make_shifted_key('SLSH', names=('QUESTION', 'QUES', '?')) diff --git a/kmk/led.py b/kmk/led.py index d72456f..4d0e14c 100644 --- a/kmk/led.py +++ b/kmk/led.py @@ -88,20 +88,20 @@ class led: self.set_brightness(0) def increase_ani(self): - """ + ''' Increases animation speed by 1 amount stopping at 10 :param step: - """ + ''' if (self.animation_speed + 1) >= 10: self.animation_speed = 10 else: self.val += 1 def decrease_ani(self): - """ + ''' Decreases animation speed by 1 amount stopping at 0 :param step: - """ + ''' if (self.val - 1) <= 0: self.val = 0 else: @@ -127,10 +127,10 @@ class led: return self def animate(self): - """ + ''' Activates a "step" in the animation based on the active mode :return: Returns the new state in animation - """ + ''' if self.effect_init: self._init_effect() if self.enabled: diff --git a/kmk/rgb.py b/kmk/rgb.py index 1aef3a1..74f8ca0 100644 --- a/kmk/rgb.py +++ b/kmk/rgb.py @@ -97,13 +97,13 @@ class RGB: return int(time.monotonic() * 1000) def hsv_to_rgb(self, hue, sat, val): - """ + ''' Converts HSV values, and returns a tuple of RGB values :param hue: :param sat: :param val: :return: (r, g, b) - """ + ''' r = 0 g = 0 b = 0 @@ -149,24 +149,24 @@ class RGB: return int(r), int(g), int(b) def hsv_to_rgbw(self, hue, sat, val): - """ + ''' Converts HSV values, and returns a tuple of RGBW values :param hue: :param sat: :param val: :return: (r, g, b, w) - """ + ''' rgb = self.hsv_to_rgb(hue, sat, val) return rgb[0], rgb[1], rgb[2], min(rgb) def set_hsv(self, hue, sat, val, index): - """ + ''' Takes HSV values and displays it on a single LED/Neopixel :param hue: :param sat: :param val: :param index: Index of LED/Pixel - """ + ''' if self.neopixel: if self.rgbw: self.set_rgb(self.hsv_to_rgbw(hue, sat, val), index) @@ -176,12 +176,12 @@ class RGB: return self def set_hsv_fill(self, hue, sat, val): - """ + ''' Takes HSV values and displays it on all LEDs/Neopixels :param hue: :param sat: :param val: - """ + ''' if self.neopixel: if self.rgbw: self.set_rgb_fill(self.hsv_to_rgbw(hue, sat, val)) @@ -190,11 +190,11 @@ class RGB: return self def set_rgb(self, rgb, index): - """ + ''' Takes an RGB or RGBW and displays it on a single LED/Neopixel :param rgb: RGB or RGBW :param index: Index of LED/Pixel - """ + ''' if self.neopixel and 0 <= index <= self.num_pixels - 1: self.neopixel[index] = rgb if not self.disable_auto_write: @@ -203,10 +203,10 @@ class RGB: return self def set_rgb_fill(self, rgb): - """ + ''' Takes an RGB or RGBW and displays it on all LEDs/Neopixels :param rgb: RGB or RGBW - """ + ''' if self.neopixel: self.neopixel.fill(rgb) if not self.disable_auto_write: @@ -215,10 +215,10 @@ class RGB: return self def increase_hue(self, step=None): - """ + ''' Increases hue by step amount rolling at 360 and returning to 0 :param step: - """ + ''' if not step: step = self.hue_step @@ -230,10 +230,10 @@ class RGB: return self def decrease_hue(self, step=None): - """ + ''' Decreases hue by step amount rolling at 0 and returning to 360 :param step: - """ + ''' if not step: step = self.hue_step @@ -248,10 +248,10 @@ class RGB: return self def increase_sat(self, step=None): - """ + ''' Increases saturation by step amount stopping at 100 :param step: - """ + ''' if not step: step = self.sat_step @@ -266,10 +266,10 @@ class RGB: return self def decrease_sat(self, step=None): - """ + ''' Decreases saturation by step amount stopping at 0 :param step: - """ + ''' if not step: step = self.sat_step @@ -284,10 +284,10 @@ class RGB: return self def increase_val(self, step=None): - """ + ''' Increases value by step amount stopping at 100 :param step: - """ + ''' if not step: step = self.val_step if (self.val + step) >= 100: @@ -301,10 +301,10 @@ class RGB: return self def decrease_val(self, step=None): - """ + ''' Decreases value by step amount stopping at 0 :param step: - """ + ''' if not step: step = self.val_step if (self.val - step) <= 0: @@ -318,20 +318,20 @@ class RGB: return self def increase_ani(self): - """ + ''' Increases animation speed by 1 amount stopping at 10 :param step: - """ + ''' if (self.animation_speed + 1) >= 10: self.animation_speed = 10 else: self.val += 1 def decrease_ani(self): - """ + ''' Decreases animation speed by 1 amount stopping at 0 :param step: - """ + ''' if (self.val - 1) <= 0: self.val = 0 else: @@ -340,28 +340,28 @@ class RGB: return self def off(self): - """ + ''' Turns off all LEDs/Neopixels without changing stored values - """ + ''' if self.neopixel: self.set_hsv_fill(0, 0, 0) return self def show(self): - """ + ''' Turns on all LEDs/Neopixels without changing stored values - """ + ''' if self.neopixel: self.neopixel.show() return self def animate(self): - """ + ''' Activates a "step" in the animation based on the active mode :return: Returns the new state in animation - """ + ''' if self.effect_init: self._init_effect() diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..a7c1e5c --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,22 @@ +[tool.black] +# since black refuses to allow single-quotes... see locked conversation at +# https://github.com/psf/black/issues/594 +skip-string-normalization = true +target = "py36" +# explicitly exculde user_keymaps from black formatting rules +# because a visually-appealing keymap list will be flattened +# by black into a much harder to understand format +exclude = ''' +/( + \.git + | \.mypy_cache + | \.tox + | \.venv + | \.pytest_cache + | \.compiled + | dist + | build + | docs + | user_keymaps +)/ +''' diff --git a/setup.cfg b/setup.cfg index c8b74b0..d4d6f77 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,13 @@ [flake8] exclude = .git,__pycache__,vendor,.venv,build -max_line_length = 99 +# match black expectations +max_line_length = 88 + +# enforce single quotes +select = Q0 +docstring-quotes = ''' +multiline-quotes = ''' + ignore = X100, E262 per-file-ignores = # Allow crazy line lengths, unused variables, and multiple spaces after commas in lists (for grid alignment) @@ -11,3 +18,8 @@ per-file-ignores = [isort] known_third_party = analogio,bitbangio,bleio,board,busio,digitalio,framebuf,gamepad,gc,microcontroller,micropython,pulseio,pyb,pydux,uio,ubluepy,machine,pyb,uos +multi_line_output=3 +include_trailing_comma=True +force_grid_wrap=0 +use_parentheses=True +line_length=88 diff --git a/user_keymaps/kdb424/gherkin.py b/user_keymaps/kdb424/gherkin.py index 615d25d..1d55b55 100644 --- a/user_keymaps/kdb424/gherkin.py +++ b/user_keymaps/kdb424/gherkin.py @@ -70,7 +70,7 @@ keyboard.leader_dictionary = { 'yay': emoticons.YAY, } -WPM = send_string("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Bibendum arcu vitae elementum curabitur vitae nunc sed. Facilisis sed odio morbi quis.") +WPM = send_string('Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Bibendum arcu vitae elementum curabitur vitae nunc sed. Facilisis sed odio morbi quis.') # ---------------------- Keymap --------------------------------------------------------- diff --git a/user_keymaps/kdb424/klanck.py b/user_keymaps/kdb424/klanck.py index 97c3d1a..9bf8b7f 100644 --- a/user_keymaps/kdb424/klanck.py +++ b/user_keymaps/kdb424/klanck.py @@ -43,7 +43,7 @@ keyboard.leader_dictionary = { 'yay': emoticons.YAY, } -WPM = send_string("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Bibendum arcu vitae elementum curabitur vitae nunc sed. Facilisis sed odio morbi quis.") +WPM = send_string('Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Bibendum arcu vitae elementum curabitur vitae nunc sed. Facilisis sed odio morbi quis.') # ---------------------- Keymap --------------------------------------------------------- diff --git a/user_keymaps/klardotsh/iris_r2.py b/user_keymaps/klardotsh/iris_r2.py index 892089c..e686a47 100644 --- a/user_keymaps/klardotsh/iris_r2.py +++ b/user_keymaps/klardotsh/iris_r2.py @@ -41,7 +41,7 @@ emoticons = cuss({ 'TABLE_FLIP': r'(╯°□°)╯︵ ┻━┻', }) -WPM = send_string("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Bibendum arcu vitae elementum curabitur vitae nunc sed. Facilisis sed odio morbi quis.") +WPM = send_string('Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Bibendum arcu vitae elementum curabitur vitae nunc sed. Facilisis sed odio morbi quis.') keyboard.leader_mode = LeaderMode.ENTER keyboard.leader_dictionary = { diff --git a/user_keymaps/klardotsh/klarank_featherm4.py b/user_keymaps/klardotsh/klarank_featherm4.py index b6525c7..c1e5897 100644 --- a/user_keymaps/klardotsh/klarank_featherm4.py +++ b/user_keymaps/klardotsh/klarank_featherm4.py @@ -40,7 +40,7 @@ emoticons = cuss({ 'TABLE_FLIP': r'(╯°□°)╯︵ ┻━┻', }) -WPM = send_string("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Bibendum arcu vitae elementum curabitur vitae nunc sed. Facilisis sed odio morbi quis.") +WPM = send_string('Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Bibendum arcu vitae elementum curabitur vitae nunc sed. Facilisis sed odio morbi quis.') keyboard.leader_mode = LeaderMode.TIMEOUT keyboard.leader_dictionary = {