Skip to content

Commit 2839ec3

Browse files
committed
chore: Typing & linting
1 parent beb04a2 commit 2839ec3

File tree

5 files changed

+68
-74
lines changed

5 files changed

+68
-74
lines changed

scc/controller.py

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from scc.constants import HapticPos
66

77
if TYPE_CHECKING:
8-
from scc.controller import HapticData
98
from scc.mapper import Mapper
109
import logging
1110
import time
@@ -32,8 +31,9 @@ def __init__(self) -> None:
3231

3332

3433
def get_type(self) -> None:
35-
"""
36-
This method has to return type identifier - short string without spaces
34+
"""Has to return type identifier
35+
36+
Returns a short string without spaces
3737
that describes type of controller which should be unique for each
3838
driver.
3939
String is used by UI to assign icons and, along with ID,
@@ -45,16 +45,15 @@ def get_type(self) -> None:
4545

4646

4747
def get_id(self):
48-
"""
49-
Returns identifier that has to be unique at least until daemon
50-
is restarted, ideally derived from HW device serial number.
48+
"""Returns identifier that has to be unique at least until daemon is restarted.
49+
50+
Ideally derived from HW device serial number.
5151
"""
5252
return self._id
5353

5454

5555
def get_gui_config_file(self) -> None:
56-
"""
57-
Returns file name of json file that GUI can use to load more data about
56+
"""Returns file name of json file that GUI can use to load more data about
5857
controller (background image, button images, available buttons and
5958
axes, etc...) File name may be absolute path or just name of file in
6059
/usr/share/scc
@@ -71,63 +70,61 @@ def set_mapper(self, mapper: Mapper):
7170

7271

7372
def get_mapper(self):
74-
""" Returns mapper set for controller """
73+
"""Returns mapper set for controller"""
7574
return self.mapper
7675

7776

7877
def apply_config(self, config) -> None:
79-
"""
80-
Called from daemon to apply controller configuration stored
81-
in config file.
78+
"""Called from daemon to apply controller configuration stored in config file.
8279
8380
Does nothing by default.
8481
"""
8582
pass
8683

8784

8885
def set_led_level(self, level) -> None:
89-
"""
90-
Configures LED intensity, if supported.
86+
"""Configures LED intensity, if supported.
87+
9188
'level' goes from 0.0 to 100.0
9289
"""
9390
pass
9491

9592

9693
def set_gyro_enabled(self, enabled) -> None:
97-
""" Enables or disables gyroscope, if supported """
94+
"""Enables or disables gyroscope, if supported"""
9895
pass
9996

10097

10198
def get_gyro_enabled(self) -> bool:
102-
""" Returns True if gyroscope is enabled """
99+
"""Returns True if gyroscope is enabled"""
103100
return False
104101

105102

106103
def feedback(self, data) -> None:
107-
"""
108-
Generates feedback effect, if supported.
104+
"""Generates feedback effect, if supported.
105+
109106
'data' is HapticData instance.
110107
"""
111108
pass
112109

113110

114111
def turnoff(self) -> None:
115-
""" Turns off controller, if supported """
112+
"""Turns off controller, if supported"""
116113
pass
117114

118115

119116
def disconnected(self) -> None:
120-
""" Called from daemon after controller is disconnected """
117+
"""Called from daemon after controller is disconnected"""
121118
pass
122119

123120

124121
class HapticData:
125122
""" Simple container to hold haptic feedback settings """
126123

127124
def __init__(self, position, amplitude=512, frequency=4, period=1024, count=1):
128-
"""
129-
'frequency' is used only when emulating touchpad and describes how many
130-
pixels should mouse travell between two feedback ticks.
125+
"""'frequency' is used only when emulating touchpad
126+
127+
and describes how many pixels should mouse travel between two feedback ticks.
131128
"""
132129
data = tuple([ int(x) for x in (position, amplitude, period, count) ])
133130
if data[0] not in (HapticPos.LEFT, HapticPos.RIGHT, HapticPos.BOTH):
@@ -143,19 +140,19 @@ def __init__(self, position, amplitude=512, frequency=4, period=1024, count=1):
143140
self.frequency = frequency # used internally
144141

145142

146-
def with_position(self, position):
147-
""" Creates copy of HapticData with position value changed """
143+
def with_position(self, position) -> HapticData:
144+
"""Creates copy of HapticData with position value changed"""
148145
trash, amplitude, period, count = self.data
149146
return HapticData(position, amplitude, self.frequency, period, count)
150147

151148

152-
def get_position(self):
149+
def get_position(self) -> HapticPos:
153150
return HapticPos(self.data[0])
154151

155152
def get_amplitude(self):
156153
return self.data[1]
157154

158-
def get_frequency(self):
155+
def get_frequency(self) -> float:
159156
return float(self.frequency) / 1000.0
160157

161158
def get_period(self):
@@ -164,11 +161,8 @@ def get_period(self):
164161
def get_count(self):
165162
return self.data[3]
166163

167-
def __mul__(self, by) -> 'HapticData':
168-
"""
169-
Allows multiplying HapticData by scalar to get same values
170-
with increased amplitude.
171-
"""
164+
def __mul__(self, by) -> HapticData:
165+
"""Allows multiplying HapticData by scalar to get same values with increased amplitude."""
172166
position, amplitude, period, count = self.data
173167
amplitude = min(amplitude * by, 0x8000)
174168
return HapticData(position, amplitude, self.frequency, period, count)

scc/parser.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ class Token(NamedTuple):
6161
CONSTS = build_action_constants()
6262

6363

64-
def __init__(self, string:str = ""):
64+
def __init__(self, string:str = "") -> None:
6565
self.restart(string)
66+
self.tokens: list[ActionParser.Token] | None
6667

6768

6869
def from_json_data(self, data: dict, key: str | None = None):

scc/paths.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import os
1212
import sys
1313

14+
1415
def get_config_path() -> str:
1516
"""Return configuration directory.
1617

scc/poller.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,35 @@ class Poller:
1919
POLLOUT = select.POLLOUT
2020
POLLPRI = select.POLLPRI
2121

22-
def __init__(self):
22+
def __init__(self) -> None:
2323
self._events = {}
2424
self._callbacks = {}
2525
self._pool_in = ()
2626
self._pool_out = ()
2727
self._pool_pri = ()
2828

2929

30-
def register(self, fd, events, callback):
30+
def register(self, fd, events, callback) -> None:
3131
if fd < 0:
3232
raise ValueError("Invalid file descriptor")
3333
self._events[fd] = events
3434
self._callbacks[fd] = callback
3535
self._generate_lists()
3636

3737

38-
def unregister(self, fd):
38+
def unregister(self, fd) -> None:
3939
if fd in self._events: del self._events[fd]
4040
if fd in self._callbacks: del self._callbacks[fd]
4141
self._generate_lists()
4242

4343

44-
def _generate_lists(self):
44+
def _generate_lists(self) -> None:
4545
self._pool_in = [ fd for fd, events in self._events.items() if events & Poller.POLLIN ]
4646
self._pool_out = [ fd for fd, events in self._events.items() if events & Poller.POLLOUT ]
4747
self._pool_pri = [ fd for fd, events in self._events.items() if events & Poller.POLLPRI ]
4848

4949

50-
def poll(self, timeout=0.01):
50+
def poll(self, timeout=0.01) -> None:
5151
inn, out, pri = select.select( self._pool_in, self._pool_out, self._pool_pri, timeout )
5252

5353
for fd in inn:

scc/profile.py

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
33
Handles mapping profile stored in json file
44
"""
5+
from __future__ import annotations
6+
57
import json
68
import logging
79

@@ -36,33 +38,33 @@ class Profile:
3638
RPAD_AXES = { X : "rpad_x", Y : "rpad_y" }
3739
TRIGGERS = [ LEFT, RIGHT ]
3840

39-
def __init__(self, parser):
41+
def __init__(self, parser) -> None:
4042
self.parser = parser
4143
self.clear()
42-
self.filename = None
44+
self.filename: str | None = None
4345
# UI-only values
44-
self.is_template = False
45-
self.description = ""
46+
self.is_template: bool = False
47+
self.description: str = ""
4648

4749

48-
def save(self, filename):
49-
""" Saves profile into file. Returns self """
50+
def save(self, filename: str) -> Profile:
51+
"""Saves profile into file. Returns self"""
5052
with open(filename, "w") as fileobj:
5153
self.save_fileobj(fileobj)
5254
return self
5355

5456

55-
def save_fileobj(self, fileobj):
56-
""" Saves profile into file-like object. Returns self """
57+
def save_fileobj(self, fileobj) -> Profile:
58+
"""Saves profile into file-like object. Returns self"""
5759
data = {
5860
"_" : (self.description if "\n" not in self.description
5961
else self.description.strip("\n").split("\n")),
60-
'buttons' : {},
61-
'stick' : self.stick,
62-
'rstick' : self.rstick,
63-
'gyro' : self.gyro,
64-
'trigger_left' : self.triggers[Profile.LEFT],
65-
'trigger_right' : self.triggers[Profile.RIGHT],
62+
"buttons" : {},
63+
"stick" : self.stick,
64+
"rstick" : self.rstick,
65+
"gyro" : self.gyro,
66+
"trigger_left" : self.triggers[Profile.LEFT],
67+
"trigger_right" : self.triggers[Profile.RIGHT],
6668
"pad_left" : self.pads[Profile.LEFT],
6769
"pad_right" : self.pads[Profile.RIGHT],
6870
"cpad" : self.pads[Profile.CPAD],
@@ -82,17 +84,17 @@ def save_fileobj(self, fileobj):
8284
return self
8385

8486

85-
def load(self, filename):
86-
""" Loads profile from file. Returns self """
87-
with open(filename, "r") as fileobj:
87+
def load(self, filename: str) -> Profile:
88+
"""Loads profile from file. Returns self"""
89+
with open(filename) as fileobj:
8890
self.load_fileobj(fileobj)
8991
self.filename = filename
9092
return self
9193

9294

93-
def load_fileobj(self, fileobj):
94-
"""
95-
Loads profile from file-like object.
95+
def load_fileobj(self, fileobj) -> Profile:
96+
"""Loads profile from file-like object.
97+
9698
Filename attribute is not set, what may cause some trouble if used in GUI.
9799
98100
Returns self.
@@ -101,7 +103,8 @@ def load_fileobj(self, fileobj):
101103
# Version
102104
try:
103105
version = float(data["version"])
104-
except:
106+
except Exception:
107+
logging.exception("Failed to load file-like object")
105108
version = 0
106109

107110
# Settings - Description
@@ -182,8 +185,8 @@ def load_fileobj(self, fileobj):
182185
return self
183186

184187

185-
def clear(self):
186-
""" Clears all actions and adds default menu action on center button """
188+
def clear(self) -> None:
189+
"""Clears all actions and adds default menu action on center button"""
187190
self.buttons = { x : NoAction() for x in SCButtons }
188191
self.buttons[SCButtons.C] = HoldModifier(
189192
MenuAction("Default.menu"),
@@ -204,8 +207,8 @@ def clear(self):
204207

205208

206209
def get_all_actions(self):
207-
"""
208-
Returns generator with every action defined in this profile,
210+
"""Returns generator with every action defined in this profile,
211+
209212
including actions in menus.
210213
Recursively walks into macros, dpads and everything else that can have
211214
nested actions, so both parent and all child actions are yielded.
@@ -223,10 +226,7 @@ def get_all_actions(self):
223226

224227

225228
def get_actions(self):
226-
"""
227-
As get_all_actions, but returns only root actions, without children,
228-
and ignores menus.
229-
"""
229+
"""As get_all_actions, but returns only root actions, without children, and ignores menus."""
230230
for dct in (self.buttons, self.triggers, self.pads):
231231
for k in dct:
232232
yield dct[k]
@@ -235,15 +235,13 @@ def get_actions(self):
235235

236236

237237
def get_filename(self):
238-
"""
239-
Returns filename of last loaded file or None.
240-
"""
238+
"""Returns filename of last loaded file or None."""
241239
return self.filename
242240

243241

244-
def compress(self):
245-
"""
246-
Calls compress on every action to throw out some redundant stuff.
242+
def compress(self) -> None:
243+
"""Calls compress on every action to throw out some redundant stuff.
244+
247245
Note that calling save() after compress() will cause data loss.
248246
"""
249247
for dct in (self.buttons, self.triggers, self.pads):
@@ -257,7 +255,7 @@ def compress(self):
257255

258256

259257
def _convert(self, from_version):
260-
""" Performs conversion from older profile version """
258+
"""Performs conversion from older profile version"""
261259
if from_version < 1:
262260
from scc.modifiers import ModeModifier
263261
# Add 'display Default.menu if center button is held' for old profiles

0 commit comments

Comments
 (0)