From 34f9327afd4107371f81ae058090d6f8b4d34891 Mon Sep 17 00:00:00 2001 From: Jared Vititoe Date: Sun, 9 Mar 2025 20:29:04 -0400 Subject: [PATCH] better parsing --- hwmonDaemon.py | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/hwmonDaemon.py b/hwmonDaemon.py index aaa113a..1a9790d 100644 --- a/hwmonDaemon.py +++ b/hwmonDaemon.py @@ -778,24 +778,20 @@ class SystemHealthMonitor: def _parse_size(self, size_str: str) -> float: """ - Parse size string with units to bytes. - Examples: '1.5G', '500M', '2.3T', etc. + Parse size string with units to float value + Examples: '44.9G', '1.9G', '192.2M', '5.3T' """ if not size_str or not isinstance(size_str, str): return 0.0 - # Remove any spaces and convert to uppercase for consistency + # Remove any spaces and normalize format size_str = size_str.strip().upper() - # Handle special case for MB notation - if 'MB' in size_str: - size_str = size_str.replace('MB', 'M') - # Define unit multipliers units = { 'B': 1, 'K': 1024, - 'M': 1024**2, + 'M': 1024**2, 'G': 1024**3, 'T': 1024**4, 'P': 1024**5 @@ -803,13 +799,13 @@ class SystemHealthMonitor: try: # Extract numeric value and unit - if size_str[-1] in units: - number = float(size_str[:-1]) - unit = size_str[-1] - return number * units[unit] - # Handle case where the input is just a number + match = re.match(r'^([\d.]+)([BKMGTP])$', size_str) + if match: + value = float(match.group(1)) + unit = match.group(2) + return value * units[unit] return float(size_str) - except (ValueError, IndexError): + except (ValueError, AttributeError): logger.debug(f"Could not parse size: {size_str}") return 0.0