Skip to content

Commit 58cabf0

Browse files
committed
IMPROVEMENT: Refractor for increased readability
1 parent d079459 commit 58cabf0

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

MethodicConfigurator/backend_filesystem.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
from shutil import copytree as shutil_copytree
1919

2020
from re import compile as re_compile
21-
from re import match as re_match
2221

2322
# from sys import exit as sys_exit
2423
from logging import debug as logging_debug
@@ -110,17 +109,8 @@ def re_init(self, vehicle_dir: str, vehicle_type: str):
110109

111110
self.load_vehicle_components_json_data(vehicle_dir)
112111

113-
if self.vehicle_components and 'Components' in self.vehicle_components:
114-
components = self.vehicle_components['Components']
115-
else:
116-
components = None
117-
if self.fw_version is None and components:
118-
version_str = components.get('Flight Controller', {}).get('Firmware', {}).get('Version', '')
119-
version_str = version_str.lstrip().split(' ')[0] if version_str else ''
120-
if re_match(r'^\d+\.\d+\.\d+$', version_str):
121-
self.fw_version = version_str
122-
else:
123-
logging_error(f"FW version string {version_str} on {self.vehicle_components_json_filename} is invalid")
112+
if self.fw_version is None:
113+
self.fw_version = self.get_fc_fw_version_from_vehicle_components_json()
124114

125115
# Read ArduPilot parameter documentation
126116
xml_dir = vehicle_dir if os_path.isdir(vehicle_dir) else os_path.dirname(os_path.realpath(vehicle_dir))

MethodicConfigurator/backend_filesystem_vehicle_components.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
from logging import warning as logging_warning
1717
from logging import error as logging_error
1818

19+
from re import match as re_match
20+
1921
from json import load as json_load
2022
from json import dump as json_dump
2123
from json import JSONDecodeError
@@ -52,3 +54,17 @@ def save_vehicle_components_json_data(self, data, vehicle_dir: str) -> bool:
5254
logging_error("Error saving JSON data to file '%s': %s", filepath, e)
5355
return True
5456
return False
57+
58+
def get_fc_fw_version_from_vehicle_components_json(self) -> str:
59+
if self.vehicle_components and 'Components' in self.vehicle_components:
60+
components = self.vehicle_components['Components']
61+
else:
62+
components = None
63+
if components:
64+
version_str = components.get('Flight Controller', {}).get('Firmware', {}).get('Version', '')
65+
version_str = version_str.lstrip().split(' ')[0] if version_str else ''
66+
if re_match(r'^\d+\.\d+\.\d+$', version_str):
67+
return version_str
68+
else:
69+
logging_error(f"FW version string {version_str} on {self.vehicle_components_json_filename} is invalid")
70+
return None

0 commit comments

Comments
 (0)