Skip to content

Commit

Permalink
refactor: Separate out the custom handling of different devices' soft…
Browse files Browse the repository at this point in the history
…ware/firmware version parsing (#373)
  • Loading branch information
nfelt14 authored Jan 21, 2025
1 parent 34973e5 commit c42a140
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
5 changes: 1 addition & 4 deletions src/tm_devices/driver_mixins/device_control/pi_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,7 @@ def sw_version(self) -> Version:
sw_version = id_string_parts[-1]
if ":" in id_string_parts[-1]:
sw_version = id_string_parts[-1].rsplit(":", 1)[-1]
if "-" in sw_version:
split_sw = sw_version.split("-")
retval = get_version(split_sw[0])
elif " " in sw_version.lstrip():
if " " in sw_version.lstrip():
split_sw = sw_version.split(" ")
retval = get_version(split_sw[0])
else:
Expand Down
14 changes: 14 additions & 0 deletions src/tm_devices/drivers/power_supplies/psu22xx/psu2230.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
"""PSU2230 device driver."""

from packaging.version import Version

from tm_devices.drivers.power_supplies.psu22xx.psu2200 import PSU2200
from tm_devices.helpers import get_version
from tm_devices.helpers import ReadOnlyCachedProperty as cached_property # noqa: N813


class PSU2230(PSU2200):
Expand All @@ -9,3 +13,13 @@ class PSU2230(PSU2200):
################################################################################################
# Magic Methods
################################################################################################

################################################################################################
# Cached Properties
################################################################################################
@cached_property
def sw_version(self) -> Version:
"""Return the software version of the device."""
id_string_parts = self.idn_string.split(",")
sw_version = id_string_parts[-1].split("-")[0]
return get_version(sw_version)
6 changes: 6 additions & 0 deletions src/tm_devices/helpers/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@
# For this reason, the regex string itself is stored in a separate, private constant.
_SUPPORTED_MODEL_REGEX_MAPPING = re.compile(__SUPPORTED_MODEL_REGEX_STRING)

_VALID_VERSION_REGEX = re.compile(r"^\d+\.\d+(\.\d+)*$")


####################################################################################################
# Public Functions
Expand Down Expand Up @@ -537,6 +539,10 @@ def get_version(version_string: str) -> Version:
version = Version(
version_string.replace("." + version_parts[-1], "+" + version_parts[-1])
)
elif version_string.count("-") == 1 and _VALID_VERSION_REGEX.match(
version_string.split("-")[0]
):
version = Version(version_string.replace("-", "+"))
else:
output_str = ""
for char in version_parts[-1]:
Expand Down
5 changes: 5 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,9 @@ def test_detect_visa_resource_expression(
("1.2.3.4abc", Version("1.2.3.4+abc")),
("1.20.345.abc", Version("1.20.345+abc")),
("1.20.345.4abc123", Version("1.20.345.4+abc123")),
("1.2.3-alpha.4", Version("1.2.3.alpha.4")),
("1.2.3-custom.52", Version("1.2.3+custom.52")),
("1.2.3-123custom.52", Version("1.2.3+123custom.52")),
],
)
def test_get_version(version_string: str, expected_result: Version) -> None:
Expand All @@ -422,6 +425,8 @@ def test_get_version_error() -> None:
get_version("1a.2.3.abc")
with pytest.raises(InvalidVersion):
get_version("invalid-version")
with pytest.raises(InvalidVersion):
get_version("1.2.3-invalid-version.1234")


def test_read_only_cached_property() -> None:
Expand Down

0 comments on commit c42a140

Please sign in to comment.