22
33Handles mapping profile stored in json file
44"""
5+ from __future__ import annotations
6+
57import json
68import 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