Skip to content

Commit

Permalink
Assortment of linter and typing changes.
Browse files Browse the repository at this point in the history
Also replaces all instances of old alias IOError with OSError.
  • Loading branch information
C0rn3j committed Sep 21, 2024
1 parent 8a7a089 commit 03def72
Show file tree
Hide file tree
Showing 17 changed files with 203 additions and 220 deletions.
10 changes: 7 additions & 3 deletions ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ indent-style = 'tab'
[lint]
select = ['ALL']
ignore = [
'W191', # We use tabs for indents, disabling this atrocious PEP 8 recommendation
'D206', # ^
'ERA001' # Test for commented out code, but it has way too many false positives, so disable
'W191', # We use tabs for indents, disabling this atrocious PEP 8 recommendation
'D206', # ^
'D401', # non-imperative-mood - Wants docstrings in imperative language but it's really not foolproof, disable
'ERA001', # Test for commented out code, but it has way too many false positives, so disable
'FBT001', # boolean-type-hint-positional-argument - Allow positional booleans in functions, it's not really that much of an issue
'FBT002', # boolean-default-value-positional-argument - ^
'FBT003', # boolean-positional-value-in-call - ^
]
4 changes: 2 additions & 2 deletions scc/drivers/ds5drv.py
Original file line number Diff line number Diff line change
Expand Up @@ -1099,7 +1099,7 @@ def _gyro_input(self, *a):
new_state = new_state._replace(
**{axis: int(event.value * factor)}
)
except IOError:
except OSError:
# Errors here are not even reported, evdev class handles important ones
return

Expand Down Expand Up @@ -1140,7 +1140,7 @@ def _touchpad_input(self, *a):
buttons=b,
cpad_x=0, cpad_y=0
)
except IOError:
except OSError:
# Errors here are not even reported, evdev class handles important ones
return

Expand Down
4 changes: 2 additions & 2 deletions scc/drivers/evdevdrv.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def input(self, *a):
new_state = new_state._replace(buttons=b, **{ axis : value })
else:
new_state = new_state._replace(**{ axis : value })
except IOError as e:
except OSError as e:
# TODO: Maybe check e.errno to determine exact error
# all of them are fatal for now
log.error(e)
Expand Down Expand Up @@ -462,7 +462,7 @@ def make_new_device(self, factory, evdevdevice: InputDevice, *userdata):
"""
try:
controller = factory(self.daemon, evdevdevice, *userdata)
except IOError as e:
except OSError as e:
print("Failed to open device:", str(e), file=sys.stderr)
return None
if controller:
Expand Down
22 changes: 8 additions & 14 deletions scc/drivers/hiddrv.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Borrows bit of code and configuration from evdevdrv.
"""
from __future__ import annotations
import ctypes
import json
import logging
Expand Down Expand Up @@ -254,7 +255,7 @@ def __init__(self, device, daemon: "SCCDaemon", handle, config_file, config, tes
self._ready = True


def _load_hid_descriptor(self, config, max_size, vid, pid, test_mode):
def _load_hid_descriptor(self, config, max_size, vid: int, pid: int, test_mode):
hid_descriptor = HIDController.find_sys_devices_descriptor(vid, pid)
if hid_descriptor is None:
hid_descriptor = self.handle.getRawDescriptor(
Expand All @@ -265,9 +266,7 @@ def _load_hid_descriptor(self, config, max_size, vid, pid, test_mode):


def _build_button_map(self, config):
"""
Returns button map readed from configuration, in format situable
for HIDDecoder.buttons.button_map field.
"""Return button map readed from configuration, in format situable for HIDDecoder.buttons.button_map field.
Generates default if config is not available.
"""
Expand Down Expand Up @@ -302,10 +301,7 @@ def button_to_bit(sc):


def _build_axis_maping(self, axis, config, mode = AxisMode.AXIS):
"""
Converts configuration mapping for _one_ axis to value situable
for self._decoder.axes field.
"""
"""Convert configuration mapping for _one_ axis to value situable for self._decoder.axes field."""
axis_config = config.get("axes", {}).get(str(int(axis)))
if axis_config:
try:
Expand Down Expand Up @@ -438,10 +434,8 @@ def _build_hid_decoder(self, data, config, max_size):


@staticmethod
def find_sys_devices_descriptor(vid, pid):
"""
Finds, loads and returns HID descriptor available somewhere deep in
/sys/devices structure.
def find_sys_devices_descriptor(vid: int, pid: int) -> str | None:
"""Finds, loads and returns HID descriptor available somewhere deep in /sys/devices structure.
Done by walking /sys/devices recursivelly, searching for file named
'report_descriptor' in subdirectory with name contining vid and pid.
Expand All @@ -450,7 +444,7 @@ def find_sys_devices_descriptor(vid, pid):
as some controllers are presenting descriptor that are completly
broken and kernel already deals with it.
"""
def recursive_search(pattern, path):
def recursive_search(pattern: str, path: str):
for name in os.listdir(path):
full_path = os.path.join(path, name)
if name == "report_descriptor":
Expand All @@ -464,7 +458,7 @@ def recursive_search(pattern, path):
r = recursive_search(pattern, full_path)
if r:
return r
except IOError:
except OSError:
pass
return None

Expand Down
4 changes: 1 addition & 3 deletions scc/drivers/sc_by_bt.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#!/usr/bin/env python3
"""
SC Controller - Steam Controller Driver
"""SC Controller - Steam Controller Driver.
Driver for Steam Controller over bluetooth (evdev)
Expand Down
53 changes: 28 additions & 25 deletions scc/gui/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,27 @@
from scc.config import Config

import scc.osd.menu_generators
import os, sys, platform, re, json, urllib, logging
import os
import sys
import platform
import re
import json
import urllib
import logging

log = logging.getLogger("App")

class App(Gtk.Application, UserDataManager, BindingEditor):
"""
Main application / window.
"""
"""Main application / window."""

HILIGHT_COLOR = "#FF00FF00" # ARGB
OBSERVE_COLOR = "#FF60A0FF" # ARGB
CONFIG = "scc.config.json"
RELEASE_URL = "https://github.com/C0rn3j/sc-controller/releases/tag/v%s"
OSD_MODE_PROF_NAME = ".scc-osd.profile_editor"

def __init__(self, gladepath="/usr/share/scc",
imagepath="/usr/share/scc/images"):
def __init__(self, gladepath: str = "/usr/share/scc",
imagepath: str = "/usr/share/scc/images"):
Gtk.Application.__init__(self,
application_id="me.c0rn3j.scc",
flags=Gio.ApplicationFlags.HANDLES_COMMAND_LINE | Gio.ApplicationFlags.NON_UNIQUE )
Expand All @@ -54,11 +59,11 @@ def __init__(self, gladepath="/usr/share/scc",
# Setup DaemonManager
self.dm = DaemonManager()
self.dm.connect("alive", self.on_daemon_alive)
self.dm.connect('event', self.on_daemon_event_observer)
self.dm.connect("event", self.on_daemon_event_observer)
self.dm.connect("controller-count-changed", self.on_daemon_ccunt_changed)
self.dm.connect("dead", self.on_daemon_dead)
self.dm.connect("error", self.on_daemon_error)
self.dm.connect('reconfigured', self.on_daemon_reconfigured),
self.dm.connect("reconfigured", self.on_daemon_reconfigured),
self.dm.connect("version", self.on_daemon_version)
# Load custom stuff
load_custom_module(log, "gui")
Expand Down Expand Up @@ -104,8 +109,8 @@ def setup_widgets(self):
ps = self.add_switcher(12, 12)
ps.set_allow_new(True)
ps.set_profile(self.load_profile_selection())
ps.connect('new-clicked', self.on_new_clicked)
ps.connect('save-clicked', self.on_save_clicked)
ps.connect("new-clicked", self.on_new_clicked)
ps.connect("save-clicked", self.on_save_clicked)

# Drag&drop target
self.builder.get_object("content").drag_dest_set(Gtk.DestDefaults.ALL, [
Expand All @@ -121,10 +126,10 @@ def setup_widgets(self):

# Background
self.background = ControllerImage(self)
self.background.connect('hover', self.on_background_area_hover)
self.background.connect('leave', self.on_background_area_hover, None)
self.background.connect('click', self.on_background_area_click)
self.background.connect('button-press-event', self.on_background_button_press)
self.background.connect("hover", self.on_background_area_hover)
self.background.connect("leave", self.on_background_area_hover, None)
self.background.connect("click", self.on_background_area_click)
self.background.connect("button-press-event", self.on_background_button_press)
self.main_area.put(self.background, 0, 0)
self.main_area.put(vbc, 0, 0) # (self.IMAGE_SIZE[0] / 2) - 90, self.IMAGE_SIZE[1] - 100)

Expand Down Expand Up @@ -559,7 +564,7 @@ def undeletable_dialog(self, dlg, *a):


def on_btNewProfile_clicked(self, *a):
""" Called when new profile name is set and OK is clicked """
"""Called when new profile name is set and OK is clicked."""
txNewProfile = self.builder.get_object("txNewProfile")
rbNewProfile = self.builder.get_object("rbNewProfile")

Expand All @@ -574,8 +579,8 @@ def on_btNewProfile_clicked(self, *a):


def on_rbNewProfile_group_changed(self, *a):
"""
Called when user clicks 'Copy current profile' button.
"""Called when user clicks 'Copy current profile' button.
If profile name was not changed by user before clicking it,
it's automatically changed.
"""
Expand All @@ -593,10 +598,8 @@ def on_rbNewProfile_group_changed(self, *a):
self.recursing = False


def on_profile_modified(self, update_ui=True):
"""
Called when selected profile is modified in memory.
"""
def on_profile_modified(self, update_ui: bool = True):
"""Called when selected profile is modified in memory."""
if update_ui:
self.profile_switchers[0].set_profile_modified(True, self.current.is_template)

Expand All @@ -607,7 +610,7 @@ def on_profile_modified(self, update_ui=True):
self.save_profile(self.current_file, self.current)


def on_profile_loaded(self, profile, giofile):
def on_profile_loaded(self, profile: Profile, giofile: Gio.File):
self.current = profile
self.current_file = giofile
self.recursing = True
Expand All @@ -620,7 +623,7 @@ def on_profile_loaded(self, profile, giofile):
self.recursing = False


def on_profile_selected(self, ps, name, giofile):
def on_profile_selected(self, ps, name, giofile: Gio.File):
if ps == self.profile_switchers[0]:
self.load_profile(giofile)
if ps.get_controller():
Expand Down Expand Up @@ -681,7 +684,7 @@ def on_switch_to_clicked(self, ps, *a):
self.enable_test_mode()


def on_profile_saved(self, giofile, send=True):
def on_profile_saved(self, giofile: Gio.File, send: bool = True):
"""
Called when selected profile is saved to disk
"""
Expand Down Expand Up @@ -915,7 +918,7 @@ def on_daemon_ccunt_changed(self, daemon, count):
self.controller_count = count


def new_profile(self, profile, name):
def new_profile(self, profile: Profile, name: str):
filename = os.path.join(get_profiles_path(), name + ".sccprofile")
self.current_file = Gio.File.new_for_path(filename)
self.save_profile(self.current_file, profile)
Expand Down
4 changes: 1 addition & 3 deletions scc/gui/creg/dialog.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#!/usr/bin/env python3
"""
SC-Controller - Controller Registration
"""SC-Controller - Controller Registration.
Dialog that asks a lot of question to create configuration node in config file.
Most "interesting" thing here may be that this works 100% independently from
Expand Down
Loading

0 comments on commit 03def72

Please sign in to comment.