From 58a8f5e0b138b03d495c07130ed46d75560476be Mon Sep 17 00:00:00 2001 From: Himadri Bhattacharjee <107522312+lavafroth@users.noreply.github.com> Date: Thu, 23 Nov 2023 07:45:11 +0530 Subject: [PATCH 1/2] refactor: acpi parser: keep working code in happy path to avoid nested branches --- jc/parsers/acpi.py | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/jc/parsers/acpi.py b/jc/parsers/acpi.py index 1457aecb7..95a1fa39b 100644 --- a/jc/parsers/acpi.py +++ b/jc/parsers/acpi.py @@ -337,19 +337,15 @@ def parse(data, raw=False, quiet=False): output_line['state'] = 'Not charging' output_line['charge_percent'] = line.split()[-1].rstrip('%,') - if 'Charging' in line \ - or 'Discharging' in line \ - or 'Full' in line: - + if any(word in line for word in ('Charging', 'Discharging', 'Full')): output_line['state'] = line.split()[2][:-1] output_line['charge_percent'] = line.split()[3].rstrip('%,') - if 'will never fully discharge' in line: + if 'will never fully discharge' in line or 'rate information unavailable' in line: pass - elif 'rate information unavailable' not in line: - if 'Charging' in line: - output_line['until_charged'] = line.split()[4] - if 'Discharging' in line: - output_line['charge_remaining'] = line.split()[4] + if 'Charging' in line: + output_line['until_charged'] = line.split()[4] + if 'Discharging' in line: + output_line['charge_remaining'] = line.split()[4] if 'design capacity' in line: output_line['design_capacity_mah'] = line.split()[4] @@ -359,10 +355,7 @@ def parse(data, raw=False, quiet=False): if obj_type == 'Adapter': output_line['type'] = obj_type output_line['id'] = obj_id - if 'on-line' in line: - output_line['on-line'] = True - else: - output_line['on-line'] = False + output_line['on-line'] = 'on-line' in line if obj_type == 'Thermal': output_line['type'] = obj_type From 661f1425b9b433c0793c34ed8d02c49a494cfd6f Mon Sep 17 00:00:00 2001 From: Himadri Bhattacharjee <107522312+lavafroth@users.noreply.github.com> Date: Fri, 24 Nov 2023 08:36:32 +0530 Subject: [PATCH 2/2] fix: use elif for branches marked charging and discharging --- jc/parsers/acpi.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/jc/parsers/acpi.py b/jc/parsers/acpi.py index 95a1fa39b..749018032 100644 --- a/jc/parsers/acpi.py +++ b/jc/parsers/acpi.py @@ -342,9 +342,9 @@ def parse(data, raw=False, quiet=False): output_line['charge_percent'] = line.split()[3].rstrip('%,') if 'will never fully discharge' in line or 'rate information unavailable' in line: pass - if 'Charging' in line: + elif 'Charging' in line: output_line['until_charged'] = line.split()[4] - if 'Discharging' in line: + elif 'Discharging' in line: output_line['charge_remaining'] = line.split()[4] if 'design capacity' in line: