11
11
from os import path as os_path
12
12
from os import getcwd as os_getcwd
13
13
from os import listdir as os_listdir
14
- from os import makedirs as os_makedirs
15
14
from os import rename as os_rename
16
15
from os import walk as os_walk
17
- from os import sep as os_sep
18
16
19
17
from shutil import copy2 as shutil_copy2
20
18
from shutil import copytree as shutil_copytree
21
19
22
20
from re import compile as re_compile
23
- from re import match as re_match
24
- from re import escape as re_escape
25
- from re import sub as re_sub
26
21
27
22
# from sys import exit as sys_exit
28
23
from logging import debug as logging_debug
29
24
from logging import info as logging_info
30
25
from logging import warning as logging_warning
31
26
from logging import error as logging_error
32
27
33
- from json import load as json_load
34
- from json import dump as json_dump
35
-
36
- from platform import system as platform_system
37
-
38
28
from typing import Dict
39
29
from typing import List
40
30
from typing import Tuple
41
31
42
32
from zipfile import ZipFile
43
33
44
- from platformdirs import site_config_dir
45
- from platformdirs import user_config_dir
46
-
47
34
from MethodicConfigurator .annotate_params import BASE_URL , PARAM_DEFINITION_XML_FILE , Par
48
35
from MethodicConfigurator .annotate_params import get_xml_data
49
36
from MethodicConfigurator .annotate_params import create_doc_dict
53
40
54
41
from MethodicConfigurator .backend_filesystem_vehicle_components import VehicleComponents
55
42
from MethodicConfigurator .backend_filesystem_configuration_steps import ConfigurationSteps
43
+ from MethodicConfigurator .backend_filesystem_program_settings import ProgramSettings
56
44
57
45
from MethodicConfigurator .middleware_template_overview import TemplateOverview
58
46
@@ -79,7 +67,7 @@ def is_within_tolerance(x: float, y: float, atol: float = 1e-08, rtol: float = 1
79
67
return abs (x - y ) <= atol + (rtol * abs (y ))
80
68
81
69
82
- class LocalFilesystem (VehicleComponents , ConfigurationSteps ): # pylint: disable=too-many-public-methods
70
+ class LocalFilesystem (VehicleComponents , ConfigurationSteps , ProgramSettings ): # pylint: disable=too-many-public-methods
83
71
"""
84
72
A class to manage local filesystem operations for the ArduPilot methodic configurator.
85
73
@@ -98,6 +86,7 @@ def __init__(self, vehicle_dir: str, vehicle_type: str, fw_version: str, allow_e
98
86
self .file_parameters = None
99
87
VehicleComponents .__init__ (self )
100
88
ConfigurationSteps .__init__ (self , vehicle_dir , vehicle_type )
89
+ ProgramSettings .__init__ (self )
101
90
self .fw_version = fw_version
102
91
self .allow_editing_template_files = allow_editing_template_files
103
92
if vehicle_dir is not None :
@@ -394,16 +383,6 @@ def zip_files(self, files_to_zip: List[Tuple[bool, str]]):
394
383
395
384
logging_info ("Intermediate parameter files and summary files zipped to %s" , zip_file_path )
396
385
397
- @staticmethod
398
- def application_icon_filepath ():
399
- script_dir = os_path .dirname (os_path .abspath (__file__ ))
400
- return os_path .join (script_dir , 'ArduPilot_icon.png' )
401
-
402
- @staticmethod
403
- def application_logo_filepath ():
404
- script_dir = os_path .dirname (os_path .abspath (__file__ ))
405
- return os_path .join (script_dir , 'ArduPilot_logo.png' )
406
-
407
386
def vehicle_image_filepath (self ):
408
387
return os_path .join (self .vehicle_dir , 'vehicle.jpg' )
409
388
@@ -418,19 +397,6 @@ def new_vehicle_dir(base_dir: str, new_dir: str):
418
397
def directory_exists (directory : str ) -> bool :
419
398
return os_path .exists (directory )
420
399
421
- def create_new_vehicle_dir (self , new_vehicle_dir : str ):
422
- # Check if the new vehicle directory already exists
423
- if os_path .exists (new_vehicle_dir ):
424
- return "Directory already exists, choose a different one"
425
-
426
- try :
427
- # Create the new vehicle directory
428
- os_makedirs (new_vehicle_dir , exist_ok = True )
429
- except OSError as e :
430
- logging_error ("Error creating new vehicle directory: %s" , e )
431
- return str (e )
432
- return ""
433
-
434
400
def copy_template_files_to_new_vehicle_dir (self , template_dir : str , new_vehicle_dir : str ):
435
401
# Copy the template files to the new vehicle directory
436
402
for item in os_listdir (template_dir ):
@@ -445,26 +411,6 @@ def copy_template_files_to_new_vehicle_dir(self, template_dir: str, new_vehicle_
445
411
def getcwd ():
446
412
return os_getcwd ()
447
413
448
- @staticmethod
449
- def valid_directory_name (dir_name : str ):
450
- """
451
- Check if a given directory name contains only alphanumeric characters, underscores, hyphens,
452
- and the OS directory separator.
453
-
454
- This function is designed to ensure that the directory name does not contain characters that are
455
- invalid for directory names in many operating systems. It does not guarantee that the name
456
- is valid in all contexts or operating systems, as directory name validity can vary.
457
-
458
- Parameters:
459
- - dir_name (str): The directory name to check.
460
-
461
- Returns:
462
- - bool: True if the directory name matches the allowed pattern, False otherwise.
463
- """
464
- # Include os.sep in the pattern
465
- pattern = r'^[\w' + re_escape (os_sep ) + '-]+$'
466
- return re_match (pattern , dir_name ) is not None
467
-
468
414
def tempcal_imu_result_param_tuple (self ):
469
415
tempcal_imu_result_param_filename = "03_imu_temperature_calibration_results.param"
470
416
return [tempcal_imu_result_param_filename , os_path .join (self .vehicle_dir , tempcal_imu_result_param_filename )]
@@ -480,139 +426,6 @@ def copy_fc_values_to_file(self, selected_file: str, params: Dict[str, float]):
480
426
logging_warning ("Parameter %s not found in the current parameter file" , param )
481
427
return ret
482
428
483
- @staticmethod
484
- def __user_config_dir ():
485
- user_config_directory = user_config_dir (".ardupilot_methodic_configurator" , False , roaming = True , ensure_exists = True )
486
-
487
- if not os_path .exists (user_config_directory ):
488
- raise FileNotFoundError (f"The user configuration directory '{ user_config_directory } ' does not exist." )
489
- if not os_path .isdir (user_config_directory ):
490
- raise NotADirectoryError (f"The path '{ user_config_directory } ' is not a directory." )
491
-
492
- return user_config_directory
493
-
494
- @staticmethod
495
- def __site_config_dir ():
496
- site_config_directory = site_config_dir (".ardupilot_methodic_configurator" , False , version = None , multipath = False ,
497
- ensure_exists = True )
498
-
499
- if not os_path .exists (site_config_directory ):
500
- raise FileNotFoundError (f"The site configuration directory '{ site_config_directory } ' does not exist." )
501
- if not os_path .isdir (site_config_directory ):
502
- raise NotADirectoryError (f"The path '{ site_config_directory } ' is not a directory." )
503
-
504
- return site_config_directory
505
-
506
- @staticmethod
507
- def __get_settings_as_dict ():
508
- settings_path = os_path .join (LocalFilesystem .__user_config_dir (), "settings.json" )
509
-
510
- settings = {}
511
-
512
- try :
513
- with open (settings_path , "r" , encoding = 'utf-8' ) as settings_file :
514
- settings = json_load (settings_file )
515
- except FileNotFoundError :
516
- # If the file does not exist, it will be created later
517
- pass
518
-
519
- if "Format version" not in settings :
520
- settings ["Format version" ] = 1
521
-
522
- if "directory_selection" not in settings :
523
- settings ["directory_selection" ] = {}
524
- return settings
525
-
526
- @staticmethod
527
- def __set_settings_from_dict (settings ):
528
- settings_path = os_path .join (LocalFilesystem .__user_config_dir (), "settings.json" )
529
-
530
- with open (settings_path , "w" , encoding = 'utf-8' ) as settings_file :
531
- json_dump (settings , settings_file , indent = 4 )
532
-
533
- @staticmethod
534
- def __get_settings_config ():
535
- settings = LocalFilesystem .__get_settings_as_dict ()
536
-
537
- # Regular expression pattern to match single backslashes
538
- pattern = r"(?<!\\)\\(?!\\)|(?<!/)/(?!/)"
539
-
540
- # Replacement string
541
- if platform_system () == 'Windows' :
542
- replacement = r"\\"
543
- else :
544
- replacement = r"/"
545
- return settings , pattern , replacement
546
-
547
- @staticmethod
548
- def store_recently_used_template_dirs (template_dir : str , new_base_dir : str ):
549
- settings , pattern , replacement = LocalFilesystem .__get_settings_config ()
550
-
551
- # Update the settings with the new values
552
- settings ["directory_selection" ].update ({
553
- "template_dir" : re_sub (pattern , replacement , template_dir ),
554
- "new_base_dir" : re_sub (pattern , replacement , new_base_dir )
555
- })
556
-
557
- LocalFilesystem .__set_settings_from_dict (settings )
558
-
559
- @staticmethod
560
- def store_template_dir (relative_template_dir : str ):
561
- settings , pattern , replacement = LocalFilesystem .__get_settings_config ()
562
-
563
- template_dir = os_path .join (LocalFilesystem .get_templates_base_dir (), relative_template_dir )
564
-
565
- # Update the settings with the new values
566
- settings ["directory_selection" ].update ({
567
- "template_dir" : re_sub (pattern , replacement , template_dir )
568
- })
569
-
570
- LocalFilesystem .__set_settings_from_dict (settings )
571
-
572
- @staticmethod
573
- def store_recently_used_vehicle_dir (vehicle_dir : str ):
574
- settings , pattern , replacement = LocalFilesystem .__get_settings_config ()
575
-
576
- # Update the settings with the new values
577
- settings ["directory_selection" ].update ({
578
- "vehicle_dir" : re_sub (pattern , replacement , vehicle_dir )
579
- })
580
-
581
- LocalFilesystem .__set_settings_from_dict (settings )
582
-
583
-
584
- @staticmethod
585
- def get_templates_base_dir ():
586
- current_dir = os_path .dirname (os_path .abspath (__file__ ))
587
- if platform_system () == 'Windows' :
588
- current_dir = current_dir .replace ("\\ _internal" , "" )
589
- elif "site-packages" not in current_dir :
590
- current_dir = current_dir .replace ("/MethodicConfigurator" , "" )
591
- program_dir = current_dir
592
-
593
- if platform_system () == 'Windows' :
594
- site_directory = LocalFilesystem .__site_config_dir ()
595
- else :
596
- site_directory = program_dir
597
- return os_path .join (site_directory , "vehicle_templates" )
598
-
599
- @staticmethod
600
- def get_recently_used_dirs ():
601
- template_default_dir = os_path .join (LocalFilesystem .get_templates_base_dir (),
602
- "ArduCopter" , "diatone_taycan_mxc" , "4.5.3-params" )
603
-
604
- settings_directory = LocalFilesystem .__user_config_dir ()
605
- vehicles_default_dir = os_path .join (settings_directory , "vehicles" )
606
- if not os_path .exists (vehicles_default_dir ):
607
- os_makedirs (vehicles_default_dir , exist_ok = True )
608
-
609
- settings = LocalFilesystem .__get_settings_as_dict ()
610
- template_dir = settings ["directory_selection" ].get ("template_dir" , template_default_dir )
611
- new_base_dir = settings ["directory_selection" ].get ("new_base_dir" , vehicles_default_dir )
612
- vehicle_dir = settings ["directory_selection" ].get ("vehicle_dir" , vehicles_default_dir )
613
-
614
- return template_dir , new_base_dir , vehicle_dir
615
-
616
429
def write_last_uploaded_filename (self , current_file : str ):
617
430
try :
618
431
with open (os_path .join (self .vehicle_dir , 'last_uploaded_filename.txt' ), 'w' , encoding = 'utf-8' ) as file :
@@ -679,7 +492,6 @@ def get_eval_variables(self):
679
492
680
493
def copy_fc_params_values_to_template_created_vehicle_files (self , fc_parameters : Dict [str , 'Par' ]):
681
494
eval_variables = self .get_eval_variables ()
682
- eval_variables ['fc_parameters' ] = fc_parameters
683
495
for param_filename , param_dict in self .file_parameters .items ():
684
496
for param_name , param in param_dict .items ():
685
497
if param_name in fc_parameters :
@@ -712,7 +524,7 @@ def get_vehicle_components_overviews():
712
524
"""
713
525
vehicle_components_dict = {}
714
526
file_to_find = VehicleComponents ().vehicle_components_json_filename
715
- template_default_dir = LocalFilesystem .get_templates_base_dir ()
527
+ template_default_dir = ProgramSettings .get_templates_base_dir ()
716
528
for root , _dirs , files in os_walk (template_default_dir ):
717
529
if file_to_find in files :
718
530
relative_path = os_path .relpath (root , template_default_dir )
0 commit comments