Skip to content

Commit

Permalink
Formatting and linting using ruff (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonarddeR authored Sep 12, 2023
1 parent ef8ed08 commit e82c02d
Show file tree
Hide file tree
Showing 34 changed files with 3,056 additions and 2,928 deletions.
20 changes: 15 additions & 5 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
name: Lint

on:
push:
branches:
Expand All @@ -9,7 +8,18 @@ on:
- main

jobs:

lint:
uses: nvdaes/nvdaAddonWorkflows/.github/workflows/lint.yaml@main

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install Python
uses: actions/setup-python@v4
with:
python-version: "3.7"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install ruff
# Include `--format=github` to enable automatic inline annotations.
- name: Run Ruff
run: ruff check --format=github .
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ repos:
- id: check-ast
- id: check-case-conflict
- id: check-yaml
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.0.289
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
1 change: 1 addition & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Extension identifier format: ${publisher}.${name}. Example: vscode.csharp
// List of extensions which should be recommended for users of this workspace.
"recommendations": [
"charliermarsh.ruff",
"ms-python.python",
"ms-python.vscode-pylance",
"redhat.vscode-yaml"
Expand Down
15 changes: 6 additions & 9 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
{
"editor.accessibilitySupport": "on",
"python.linting.enabled": true,
"python.linting.maxNumberOfProblems": 10000,
"python.linting.flake8Args": [
"--config=flake8.ini"
],
"python.linting.flake8Enabled": true,
"python.linting.pylintEnabled": false,
"python.autoComplete.extraPaths": [
"../nvda/source",
"../nvda/miscDeps/python"
],
],
"files.insertFinalNewline": true,
"files.trimFinalNewlines": true,
"editor.insertSpaces": false,
Expand All @@ -21,5 +14,9 @@
"../nvda/source",
"../nvda/miscDeps/python"
],
"python.defaultInterpreterPath": "${workspaceFolder}/../nvda/.venv/scripts/python.exe"
"python.defaultInterpreterPath": "${workspaceFolder}/../nvda/.venv/scripts/python.exe",
"ruff.enableExperimentalFormatter": true,
"[python]": {
"editor.defaultFormatter": "charliermarsh.ruff"
}
}
156 changes: 78 additions & 78 deletions addon/brailleDisplayDrivers/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,88 +10,88 @@
from logHandler import log

if typing.TYPE_CHECKING:
from ..lib import detection
from ..lib import driver
from ..lib import protocol
from ..lib import detection
from ..lib import driver
from ..lib import protocol
else:
addon: addonHandler.Addon = addonHandler.getCodeAddon()
detection = addon.loadModule("lib.detection")
driver = addon.loadModule("lib.driver")
protocol = addon.loadModule("lib.protocol")
addon: addonHandler.Addon = addonHandler.getCodeAddon()
detection = addon.loadModule("lib.detection")
driver = addon.loadModule("lib.driver")
protocol = addon.loadModule("lib.protocol")


class RemoteBrailleDisplayDriver(driver.RemoteDriver, braille.BrailleDisplayDriver):
# Translators: Name for a remote braille display.
description = _("Remote Braille")
isThreadSafe = True
supportsAutomaticDetection = True
driverType = protocol.DriverType.BRAILLE

@classmethod
def registerAutomaticDetection(cls, driverRegistrar):
driverRegistrar.addDeviceScanner(detection.bgScanRD, moveToStart=True)

def _getModifierGestures(self, model: typing.Optional[str] = None):
"""Hacky override that throws an instance at the underlying class method.
If we don't do this, the method can't acces the gesture map at the instance level.
"""
return super()._getModifierGestures.__func__(self, model)

def _handleRemoteDisconnect(self):
# Raise an exception because handleDisplayUnavailable expects one
try:
raise RuntimeError("remote client disconnected")
except RuntimeError:
braille.handler.handleDisplayUnavailable()

@protocol.attributeReceiver(protocol.BrailleAttribute.NUM_CELLS, defaultValue=0)
def _incoming_numCells(self, payload: bytes) -> int:
assert len(payload) == 1
return ord(payload)

def _get_numCells(self) -> int:
attribute = protocol.BrailleAttribute.NUM_CELLS
try:
value = self._attributeValueProcessor.getValue(attribute, fallBackToDefault=False)
except KeyError:
value = self._attributeValueProcessor._getDefaultValue(attribute)
self.requestRemoteAttribute(attribute)
return value

@protocol.attributeReceiver(protocol.BrailleAttribute.GESTURE_MAP)
def _incoming_gestureMapUpdate(self, payload: bytes) -> inputCore.GlobalGestureMap:
assert len(payload) > 0
return self._unpickle(payload)

@_incoming_gestureMapUpdate.defaultValueGetter
def _default_gestureMap(self, attribute: protocol.AttributeT):
return inputCore.GlobalGestureMap()

def _get_gestureMap(self) -> inputCore.GlobalGestureMap:
attribute = protocol.BrailleAttribute.GESTURE_MAP
try:
value = self._attributeValueProcessor.getValue(attribute, fallBackToDefault=False)
except KeyError:
value = self._attributeValueProcessor._getDefaultValue(attribute)
self.requestRemoteAttribute(attribute)
return value

@protocol.commandHandler(protocol.BrailleCommand.EXECUTE_GESTURE)
def _command_executeGesture(self, payload: bytes):
assert len(payload) > 0
gesture = self._unpickle(payload)
try:
inputCore.manager.executeGesture(gesture)
except inputCore.NoInputGestureAction:
log.error("Unexpected NoInputGestureAction", exc_info=True)

def display(self, cells: List[int]):
# cells will already be padded up to numCells.
assert len(cells) == self.numCells
if len(cells) == 0:
return
arg = bytes(cells)
self.writeMessage(protocol.BrailleCommand.DISPLAY, arg)
# Translators: Name for a remote braille display.
description = _("Remote Braille")
isThreadSafe = True
supportsAutomaticDetection = True
driverType = protocol.DriverType.BRAILLE

@classmethod
def registerAutomaticDetection(cls, driverRegistrar):
driverRegistrar.addDeviceScanner(detection.bgScanRD, moveToStart=True)

def _getModifierGestures(self, model: typing.Optional[str] = None):
"""Hacky override that throws an instance at the underlying class method.
If we don't do this, the method can't acces the gesture map at the instance level.
"""
return super()._getModifierGestures.__func__(self, model)

def _handleRemoteDisconnect(self):
# Raise an exception because handleDisplayUnavailable expects one
try:
raise RuntimeError("remote client disconnected")
except RuntimeError:
braille.handler.handleDisplayUnavailable()

@protocol.attributeReceiver(protocol.BrailleAttribute.NUM_CELLS, defaultValue=0)
def _incoming_numCells(self, payload: bytes) -> int:
assert len(payload) == 1
return ord(payload)

def _get_numCells(self) -> int:
attribute = protocol.BrailleAttribute.NUM_CELLS
try:
value = self._attributeValueProcessor.getValue(attribute, fallBackToDefault=False)
except KeyError:
value = self._attributeValueProcessor._getDefaultValue(attribute)
self.requestRemoteAttribute(attribute)
return value

@protocol.attributeReceiver(protocol.BrailleAttribute.GESTURE_MAP)
def _incoming_gestureMapUpdate(self, payload: bytes) -> inputCore.GlobalGestureMap:
assert len(payload) > 0
return self._unpickle(payload)

@_incoming_gestureMapUpdate.defaultValueGetter
def _default_gestureMap(self, attribute: protocol.AttributeT):
return inputCore.GlobalGestureMap()

def _get_gestureMap(self) -> inputCore.GlobalGestureMap:
attribute = protocol.BrailleAttribute.GESTURE_MAP
try:
value = self._attributeValueProcessor.getValue(attribute, fallBackToDefault=False)
except KeyError:
value = self._attributeValueProcessor._getDefaultValue(attribute)
self.requestRemoteAttribute(attribute)
return value

@protocol.commandHandler(protocol.BrailleCommand.EXECUTE_GESTURE)
def _command_executeGesture(self, payload: bytes):
assert len(payload) > 0
gesture = self._unpickle(payload)
try:
inputCore.manager.executeGesture(gesture)
except inputCore.NoInputGestureAction:
log.error("Unexpected NoInputGestureAction", exc_info=True)

def display(self, cells: List[int]):
# cells will already be padded up to numCells.
assert len(cells) == self.numCells
if len(cells) == 0:
return
arg = bytes(cells)
self.writeMessage(protocol.BrailleCommand.DISPLAY, arg)


BrailleDisplayDriver = RemoteBrailleDisplayDriver
Loading

0 comments on commit e82c02d

Please sign in to comment.