Skip to content

Commit 629af3e

Browse files
authored
Merge pull request #79 from utdrmac/ruff_linting
Ruff linting
2 parents 1e997e7 + 720bcde commit 629af3e

24 files changed

+348
-359
lines changed

classes/protocol_settings.py

Lines changed: 83 additions & 82 deletions
Large diffs are not rendered by default.

classes/transports/canbus.py

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010

1111
from .transport_base import transport_base
12-
from ..protocol_settings import Data_Type, Registry_Type, registry_map_entry, protocol_settings
13-
from defs.common import strtobool, strtoint
12+
from ..protocol_settings import Registry_Type, registry_map_entry, protocol_settings
13+
from defs.common import strtoint
1414
from collections import OrderedDict
1515

1616
from typing import TYPE_CHECKING
@@ -46,7 +46,7 @@ class canbus(transport_base):
4646
cacheTimeout : int = 120
4747
''' seconds to keep message in cache '''
4848

49-
emptyTime : float = None
49+
emptyTime : float = None
5050
''' the last time values were read for watchdog'''
5151

5252
watchDogTime : float = 120
@@ -93,7 +93,7 @@ def __init__(self, settings : 'SectionProxy', protocolSettings : 'protocol_setti
9393

9494

9595
thread = threading.Thread(target=self.start_loop)
96-
thread.daemon = True
96+
thread.daemon = True
9797
thread.start()
9898

9999
self.connected = True
@@ -116,21 +116,21 @@ def is_socketcan_up(self) -> bool:
116116
if not self.linux:
117117
self._log.error("socketcan status not implemented for windows")
118118
return True
119-
119+
120120
try:
121121
with open(f'/sys/class/net/{self.port}/operstate', 'r') as f:
122122
state = f.read().strip()
123123
return state == 'up'
124124
except FileNotFoundError:
125125
return False
126-
126+
127127
def start_loop(self):
128128
self.read_bus(self.bus)
129129

130130
def read_bus(self, bus : can.BusABC):
131131
''' read canbus asynco and store results in cache'''
132132
msg = None #fix scope bug
133-
133+
134134
while True:
135135
try:
136136
msg = self.bus.recv() # This will be non-blocking with asyncio
@@ -145,49 +145,49 @@ def read_bus(self, bus : can.BusABC):
145145
except Exception as e:
146146
# Handle unexpected errors
147147
self._log.error(f"An unexpected error occurred: {e}")
148-
148+
149149

150150
if msg:
151151
self._log.info(f"Received message: {msg.arbitration_id:X}, data: {msg.data}")
152-
153-
with self.lock:
154-
#convert bytearray to bytes; were working with bytes.
152+
153+
with self.lock:
154+
#convert bytearray to bytes; were working with bytes.
155155
self.cache[msg.arbitration_id] = (bytes(msg.data), time.time())
156156

157157
#time.sleep(1) no need for sleep because recv is blocking
158158

159159

160160
def clean_cache(self):
161161
current_time = time.time()
162-
162+
163163
with self.lock:
164164
# Create a list of keys to remove (don't remove while iterating)
165165
keys_to_delete = [msg_id for msg_id, (_, timestamp) in self.cache.items() if current_time - timestamp > self.cacheTimeout]
166-
166+
167167
# Remove old messages from the dictionary
168168
for key in keys_to_delete:
169169
del self.cache[key]
170170

171171
def init_after_connect(self):
172172
return True
173-
173+
174174
''' todo, a startup phase to get serial number'''
175175
#from transport_base settings
176176
if self.write_enabled:
177177
self.enable_write()
178178

179179
#if sn is empty, attempt to autoread it
180-
if not self.device_serial_number:
180+
if not self.device_serial_number:
181181
self.device_serial_number = self.read_serial_number()
182-
182+
183183
def read_serial_number(self) -> str:
184184
''' not so simple in canbus'''
185185
return ''
186186
serial_number = str(self.read_variable("Serial Number", Registry_Type.HOLDING))
187187
print("read SN: " +serial_number)
188188
if serial_number:
189189
return serial_number
190-
190+
191191
sn2 = ""
192192
sn3 = ""
193193
fields = ['Serial No 1', 'Serial No 2', 'Serial No 3', 'Serial No 4', 'Serial No 5']
@@ -200,19 +200,19 @@ def read_serial_number(self) -> str:
200200
if not hasattr(data, 'registers') or data.registers is None:
201201
self._log.critical("Failed to get serial number register ("+field+") ; exiting")
202202
exit()
203-
203+
204204
serial_number = serial_number + str(data.registers[0])
205205

206206
data_bytes = data.registers[0].to_bytes((data.registers[0].bit_length() + 7) // 8, byteorder='big')
207-
sn2 = sn2 + str(data_bytes.decode('utf-8'))
207+
sn2 = sn2 + str(data_bytes.decode('utf-8'))
208208
sn3 = str(data_bytes.decode('utf-8')) + sn3
209209

210210
time.sleep(self.modbus_delay*2) #sleep inbetween requests so modbus can rest
211-
211+
212212
print(sn2)
213213
print(sn3)
214-
215-
if not re.search("[^a-zA-Z0-9\_]", sn2) :
214+
215+
if not re.search(r"[^a-zA-Z0-9\_]", sn2) :
216216
serial_number = sn2
217217

218218
return serial_number
@@ -236,7 +236,7 @@ def read_data(self) -> dict[str, str]:
236236

237237
new_info = self.protocolSettings.process_registery(registry, self.protocolSettings.get_registry_map(Registry_Type.ZERO))
238238

239-
info.update(new_info)
239+
info.update(new_info)
240240

241241
currentTime = time.time()
242242

@@ -255,13 +255,13 @@ def read_data(self) -> dict[str, str]:
255255

256256
def read_variable(self, variable_name : str, registry_type : Registry_Type, entry : registry_map_entry = None):
257257
''' read's variable from cache'''
258-
##clean for convinecne
258+
##clean for convinecne
259259
if variable_name:
260260
variable_name = variable_name.strip().lower().replace(' ', '_')
261261

262262
registry_map = self.protocolSettings.get_registry_map(registry_type)
263263

264-
if entry == None:
264+
if entry is None:
265265
for e in registry_map:
266266
if e.variable_name == variable_name:
267267
entry = e
@@ -274,4 +274,4 @@ def read_variable(self, variable_name : str, registry_type : Registry_Type, entr
274274
results = self.protocolSettings.process_register_bytes(self.cache, entry)
275275
return results[entry.variable_name]
276276
else:
277-
return None #empty
277+
return None #empty

0 commit comments

Comments
 (0)