From 2a6025f5f2fb457e300f1988ac133511ac234a8d Mon Sep 17 00:00:00 2001 From: Jared Vititoe Date: Sun, 9 Mar 2025 22:25:29 -0400 Subject: [PATCH] updated parsing --- hwmonDaemon.py | 61 ++++++++++++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 27 deletions(-) diff --git a/hwmonDaemon.py b/hwmonDaemon.py index 0c55d24..896d17c 100644 --- a/hwmonDaemon.py +++ b/hwmonDaemon.py @@ -779,38 +779,45 @@ class SystemHealthMonitor: return False def _parse_size(self, size_str: str) -> float: - """Parse size string with units to bytes""" - if not size_str or not isinstance(size_str, str): - logger.debug(f"Invalid size string: {size_str}") - return 0.0 - - size_str = size_str.strip().upper() + """ + Parse size string with units to bytes. + + :param size_str: String containing size with unit (e.g. '15.7G', '21.8T') + :return: Size in bytes as float + """ logger.debug(f"Parsing size string: {size_str}") + + try: + # Skip non-size strings + if not any(unit in size_str.upper() for unit in ['B', 'K', 'M', 'G', 'T']): + logger.debug(f"No valid size unit found in: {size_str}") + return 0.0 - # Match size with optional decimal and unit - match = re.match(r'^([\d.]+)\s*([KMGT]B?)?$', size_str) - if match: - value = float(match.group(1)) - unit = match.group(2) + # Define multipliers for units + multipliers = { + 'B': 1, + 'K': 1024, + 'M': 1024**2, + 'G': 1024**3, + 'T': 1024**4 + } + + # Extract numeric value and unit + value = float(size_str[:-1].strip()) + unit = size_str[-1].upper() + logger.debug(f"Extracted value: {value}, unit: {unit}") - multipliers = { - 'K': 1024, - 'M': 1024**2, - 'G': 1024**3, - 'T': 1024**4, - 'KB': 1024, - 'MB': 1024**2, - 'GB': 1024**3, - 'TB': 1024**4 - } - byte_value = value * multipliers.get(unit, 1) - logger.debug(f"Converted size to bytes: {byte_value}") - return byte_value - - logger.debug(f"Failed to parse size string: {size_str}") - return 0.0 + # Convert to bytes + bytes_value = value * multipliers.get(unit, 0) + logger.debug(f"Converted size to bytes: {bytes_value}") + + return bytes_value + except (ValueError, AttributeError) as e: + logger.debug(f"Failed to parse size string: {size_str}") + logger.debug(f"Parse error details: {str(e)}") + return 0.0 def _is_physical_disk(self, device_path): """