1
- #!/usr/bin/env python3
2
-
3
1
"""
2
+ Filesystem operations.
3
+
4
4
This file is part of Ardupilot methodic configurator. https://github.com/ArduPilot/MethodicConfigurator
5
5
6
6
SPDX-FileCopyrightText: 2024 Amilcar do Carmo Lucas <[email protected] >
@@ -54,14 +54,15 @@ def is_within_tolerance(x: float, y: float, atol: float = 1e-08, rtol: float = 1
54
54
the sum of the absolute tolerance (`atol`) and the product of the relative tolerance (`rtol`)
55
55
and the absolute value of `y`.
56
56
57
- Parameters :
58
- - x (float): The first number to compare.
59
- - y (float): The second number to compare.
60
- - atol (float, optional): The absolute tolerance. Defaults to 1e-08.
61
- - rtol (float, optional): The relative tolerance. Defaults to 1e-03.
57
+ Args :
58
+ x (float): The first number to compare.
59
+ y (float): The second number to compare.
60
+ atol (float, optional): The absolute tolerance. Defaults to 1e-08.
61
+ rtol (float, optional): The relative tolerance. Defaults to 1e-03.
62
62
63
63
Returns:
64
- - bool: True if the difference is within the tolerance, False otherwise.
64
+ bool: True if the difference is within the tolerance, False otherwise.
65
+
65
66
"""
66
67
return abs (x - y ) <= atol + (rtol * abs (y ))
67
68
@@ -74,12 +75,13 @@ class LocalFilesystem(VehicleComponents, ConfigurationSteps, ProgramSettings):
74
75
reading parameters from files, and handling configuration steps. It is designed to simplify
75
76
the interaction with the local filesystem for managing ArduPilot configuration files.
76
77
77
- Attributes :
78
+ Args :
78
79
vehicle_dir (str): The directory path where the vehicle configuration files are stored.
79
80
vehicle_type (str): The type of the vehicle (e.g., "ArduCopter", "Rover").
80
81
file_parameters (dict): A dictionary of parameters read from intermediate parameter files.
81
82
param_default_dict (dict): A dictionary of default parameter values.
82
83
doc_dict (dict): A dictionary containing documentation for each parameter.
84
+
83
85
"""
84
86
85
87
def __init__ (self , vehicle_dir : str , vehicle_type : str , fw_version : str , allow_editing_template_files : bool ) -> None :
@@ -227,6 +229,7 @@ def read_params_from_files(self) -> dict[str, dict[str, "Par"]]:
227
229
Returns:
228
230
- Dict[str, Dict[str, 'Par']]: A dictionary with filenames as keys and as values
229
231
a dictionary with (parameter names, values) pairs.
232
+
230
233
"""
231
234
parameters : dict [str , dict [str , Par ]] = {}
232
235
if os_path .isdir (self .vehicle_dir ):
@@ -243,18 +246,19 @@ def read_params_from_files(self) -> dict[str, dict[str, "Par"]]:
243
246
return parameters
244
247
245
248
@staticmethod
246
- def str_to_bool (s ) -> Optional [bool ]:
249
+ def str_to_bool (s : str ) -> Optional [bool ]:
247
250
"""
248
251
Converts a string representation of a boolean value to a boolean.
249
252
250
253
This function interprets the string 'true', 'yes', '1' as True, and 'false', 'no', '0' as False.
251
254
Any other input will return None.
252
255
253
- Parameters :
254
- - s (str): The string to convert.
256
+ Args :
257
+ s (str): The string to convert.
255
258
256
259
Returns:
257
- - Optional[bool]: True, False, or None if the string does not match any known boolean representation.
260
+ Optional[bool]: True, False, or None if the string does not match any known boolean representation.
261
+
258
262
"""
259
263
if s .lower () == "true" or s .lower () == "yes" or s .lower () == "1" :
260
264
return True
@@ -269,10 +273,11 @@ def export_to_param(self, params: dict[str, "Par"], filename_out: str, annotate_
269
273
This function formats the provided parameters into a string suitable for a .param file,
270
274
writes the string to the specified output file, and optionally updates the parameter documentation.
271
275
272
- Parameters:
273
- - params (Dict[str, 'Par']): A dictionary of parameters to export.
274
- - filename_out (str): The name of the output file.
275
- - annotate_doc (bool, optional): Whether to update the parameter documentation. Defaults to True.
276
+ Args:
277
+ params (Dict[str, 'Par']): A dictionary of parameters to export.
278
+ filename_out (str): The name of the output file.
279
+ annotate_doc (bool, optional): Whether to update the parameter documentation. Defaults to True.
280
+
276
281
"""
277
282
Par .export_to_param (Par .format_params (params ), os_path .join (self .vehicle_dir , filename_out ))
278
283
if annotate_doc :
@@ -284,11 +289,12 @@ def vehicle_configuration_file_exists(self, filename: str) -> bool:
284
289
"""
285
290
Check if a vehicle configuration file exists in the vehicle directory.
286
291
287
- Parameters :
288
- - filename (str): The name of the file to check.
292
+ Args :
293
+ filename (str): The name of the file to check.
289
294
290
295
Returns:
291
- - bool: True if the file exists and is a file (not a directory), False otherwise.
296
+ bool: True if the file exists and is a file (not a directory), False otherwise.
297
+
292
298
"""
293
299
return os_path .exists (os_path .join (self .vehicle_dir , filename )) and os_path .isfile (
294
300
os_path .join (self .vehicle_dir , filename )
@@ -304,6 +310,7 @@ def __all_intermediate_parameter_file_comments(self) -> dict[str, str]:
304
310
305
311
Returns:
306
312
- Dict[str, str]: A dictionary mapping parameter names to their comments.
313
+
307
314
"""
308
315
ret = {}
309
316
for params in self .file_parameters .values ():
@@ -320,11 +327,12 @@ def annotate_intermediate_comments_to_param_dict(self, param_dict: dict[str, flo
320
327
intermediate parameter files to create a new dictionary where each parameter is represented
321
328
by a 'Par' object containing both the value and the comment.
322
329
323
- Parameters :
324
- - param_dict (Dict[str, float]): A dictionary of parameters with only values.
330
+ Args :
331
+ param_dict (Dict[str, float]): A dictionary of parameters with only values.
325
332
326
333
Returns:
327
- - Dict[str, 'Par']: A dictionary of parameters with intermediate parameter file comments.
334
+ Dict[str, 'Par']: A dictionary of parameters with intermediate parameter file comments.
335
+
328
336
"""
329
337
ret = {}
330
338
ip_comments = self .__all_intermediate_parameter_file_comments ()
@@ -341,12 +349,13 @@ def categorize_parameters(self, param: dict[str, "Par"]) -> tuple[dict[str, "Par
341
349
- Non-default, writable calibrations
342
350
- Non-default, writable non-calibrations
343
351
344
- Parameters :
345
- - param (Dict[str, 'Par']): A dictionary mapping parameter names to their 'Par' objects.
352
+ Args :
353
+ param (Dict[str, 'Par']): A dictionary mapping parameter names to their 'Par' objects.
346
354
347
355
Returns:
348
- - Tuple[Dict[str, "Par"], Dict[str, "Par"], Dict[str, "Par"]]: A tuple of three dictionaries.
356
+ Tuple[Dict[str, "Par"], Dict[str, "Par"], Dict[str, "Par"]]: A tuple of three dictionaries.
349
357
Each dictionary represents one of the categories mentioned above.
358
+
350
359
"""
351
360
non_default__read_only_params = {}
352
361
non_default__writable_calibrations = {}
@@ -374,9 +383,7 @@ def get_directory_name_from_full_path(full_path: str) -> str:
374
383
normalized_path = os_path .normpath (full_path )
375
384
376
385
# Split the path into head and tail, then get the basename of the tail
377
- directory_name = os_path .basename (os_path .split (normalized_path )[1 ])
378
-
379
- return directory_name
386
+ return os_path .basename (os_path .split (normalized_path )[1 ])
380
387
381
388
# Extract the vehicle name from the directory path
382
389
def get_vehicle_directory_name (self ) -> str :
@@ -390,7 +397,7 @@ def zip_file_exists(self) -> bool:
390
397
zip_file_path = self .zip_file_path ()
391
398
return os_path .exists (zip_file_path ) and os_path .isfile (zip_file_path )
392
399
393
- def add_configuration_file_to_zip (self , zipf , filename ) -> None :
400
+ def add_configuration_file_to_zip (self , zipf : ZipFile , filename : str ) -> None :
394
401
if self .vehicle_configuration_file_exists (filename ):
395
402
zipf .write (os_path .join (self .vehicle_dir , filename ), arcname = filename )
396
403
@@ -403,9 +410,10 @@ def zip_files(self, files_to_zip: list[tuple[bool, str]]) -> None:
403
410
intermediate parameter files. The method checks for the existence of each file before
404
411
attempting to add it to the zip archive.
405
412
406
- Parameters :
407
- - files_to_zip (List[Tuple[bool, str]]): A list of tuples, where each tuple contains a boolean
413
+ Args :
414
+ files_to_zip (List[Tuple[bool, str]]): A list of tuples, where each tuple contains a boolean
408
415
indicating if the file was written and a string for the filename.
416
+
409
417
"""
410
418
zip_file_path = self .zip_file_path ()
411
419
with ZipFile (zip_file_path , "w" ) as zipf :
0 commit comments