2
2
Utilities and configuration file parsing.
3
3
"""
4
4
5
+ from typing import Dict , Optional , Union
6
+
7
+ from can import typechecking
8
+
5
9
import json
6
10
import os
7
11
import os .path
29
33
CONFIG_FILES .extend (["can.ini" , os .path .join (os .getenv ("APPDATA" , "" ), "can.ini" )])
30
34
31
35
32
- def load_file_config (path = None , section = "default" ):
36
+ def load_file_config (
37
+ path : Optional [typechecking .AcceptedIOType ] = None , section : str = "default"
38
+ ) -> Dict [str , str ]:
33
39
"""
34
40
Loads configuration from file with following content::
35
41
@@ -57,7 +63,7 @@ def load_file_config(path=None, section="default"):
57
63
return _config
58
64
59
65
60
- def load_environment_config (context = None ):
66
+ def load_environment_config (context : Optional [ str ] = None ) -> Dict [ str , str ] :
61
67
"""
62
68
Loads config dict from environmental variables (if set):
63
69
@@ -83,20 +89,22 @@ def load_environment_config(context=None):
83
89
84
90
context_suffix = "_{}" .format (context ) if context else ""
85
91
86
- config = {}
87
-
88
92
can_config_key = "CAN_CONFIG" + context_suffix
89
- if can_config_key in os .environ :
90
- config = json .loads (os .environ .get (can_config_key ))
93
+ config : Dict [str , str ] = json .loads (os .environ .get (can_config_key , "{}" ))
91
94
92
95
for key , val in mapper .items ():
93
- if val in os .environ :
94
- config [key ] = os .environ .get (val + context_suffix )
96
+ config_option = os .environ .get (val + context_suffix , None )
97
+ if config_option :
98
+ config [key ] = config_option
95
99
96
100
return config
97
101
98
102
99
- def load_config (path = None , config = None , context = None ):
103
+ def load_config (
104
+ path : Optional [typechecking .AcceptedIOType ] = None ,
105
+ config = None ,
106
+ context : Optional [str ] = None ,
107
+ ) -> typechecking .BusConfig :
100
108
"""
101
109
Returns a dict with configuration details which is loaded from (in this order):
102
110
@@ -213,26 +221,25 @@ def load_config(path=None, config=None, context=None):
213
221
return config
214
222
215
223
216
- def set_logging_level (level_name = None ):
224
+ def set_logging_level (level_name : Optional [ str ] = None ):
217
225
"""Set the logging level for the "can" logger.
218
226
Expects one of: 'critical', 'error', 'warning', 'info', 'debug', 'subdebug'
219
227
"""
220
228
can_logger = logging .getLogger ("can" )
221
229
222
230
try :
223
- can_logger .setLevel (getattr (logging , level_name .upper ()))
231
+ can_logger .setLevel (getattr (logging , level_name .upper ())) # type: ignore
224
232
except AttributeError :
225
233
can_logger .setLevel (logging .DEBUG )
226
234
log .debug ("Logging set to {}" .format (level_name ))
227
235
228
236
229
- def len2dlc (length ) :
237
+ def len2dlc (length : int ) -> int :
230
238
"""Calculate the DLC from data length.
231
239
232
240
:param int length: Length in number of bytes (0-64)
233
241
234
242
:returns: DLC (0-15)
235
- :rtype: int
236
243
"""
237
244
if length <= 8 :
238
245
return length
@@ -242,25 +249,23 @@ def len2dlc(length):
242
249
return 15
243
250
244
251
245
- def dlc2len (dlc ) :
252
+ def dlc2len (dlc : int ) -> int :
246
253
"""Calculate the data length from DLC.
247
254
248
- :param int dlc: DLC (0-15)
255
+ :param dlc: DLC (0-15)
249
256
250
257
:returns: Data length in number of bytes (0-64)
251
- :rtype: int
252
258
"""
253
259
return CAN_FD_DLC [dlc ] if dlc <= 15 else 64
254
260
255
261
256
- def channel2int (channel ) :
262
+ def channel2int (channel : Optional [ Union [ typechecking . Channel ]]) -> Optional [ int ] :
257
263
"""Try to convert the channel to an integer.
258
264
259
265
:param channel:
260
266
Channel string (e.g. can0, CAN1) or integer
261
267
262
268
:returns: Channel integer or `None` if unsuccessful
263
- :rtype: int
264
269
"""
265
270
if channel is None :
266
271
return None
0 commit comments