From f18becd77ba3d9f30ecc3f4797d50d22d5761ae8 Mon Sep 17 00:00:00 2001 From: Loic Nageleisen Date: Thu, 23 Jan 2014 14:16:46 +0100 Subject: [PATCH] battery cruft cleanup --- battery.py => battery | 109 +++++++++++++++++++++---------------- battery.sh | 122 ------------------------------------------ 2 files changed, 63 insertions(+), 168 deletions(-) rename battery.py => battery (57%) delete mode 100755 battery.sh diff --git a/battery.py b/battery similarity index 57% rename from battery.py rename to battery index 57b5682..21119d4 100755 --- a/battery.py +++ b/battery @@ -5,36 +5,43 @@ from __future__ import division from subprocess import Popen, PIPE + def ioreg_battery_info(): - output = Popen(["ioreg", "-r", "-k", "LegacyBatteryInfo", "-w", "0"], stdout=PIPE).communicate()[0] - try: #python3 + output = Popen(["ioreg", "-r", "-k", "LegacyBatteryInfo", "-w", "0"], + stdout=PIPE).communicate()[0] + try: # python3 return str(output, encoding='utf-8') - except TypeError: #python2 + except TypeError: # python2 return output + def parse_ioreg_dict(output): return dict( - [ (kw.strip().strip('"'), vw.strip()) - for kw, vw - in [ line.split("=", 1) - for line - in output.split('\n') if line.find('=')>0 - ] - ] - ) + [(kw.strip().strip('"'), vw.strip()) + for kw, vw + in [line.split("=", 1) + for line + in output.split('\n') if line.find('=') > 0 + ] + ] + ) + def is_two_complement_negative(value): return value > (2**63-1) + def two_complement(value): return 2**64 - value + def fix_negative(value): if is_two_complement_negative(value): return -two_complement(value) else: return value + def ioreg_battery_dict(): output = ioreg_battery_info() return parse_ioreg_dict(output) @@ -43,25 +50,28 @@ def ioreg_battery_dict(): def fix_integer(string): return fix_negative(int(string)) + def format_time(string): minutes = int(string) if minutes == 65535: return None else: - return "%s:%s" % (minutes//60, minutes%60) + return "%s:%s" % (minutes // 60, minutes % 60) + def percentage(ratio): - return "%s%%" % (int(ratio*100)) + return "%s%%" % (int(ratio * 100)) humanize_index = { - "TimeRemaining": format_time, - "AvgTimeToEmpty": format_time, - "AvgTimeToFull": format_time, - "InstantTimeToEmpty": format_time, - "FullToEmptyTime": format_time, - "WearRatio": percentage, - "ChargeRatio": percentage, - } + "TimeRemaining": format_time, + "AvgTimeToEmpty": format_time, + "AvgTimeToFull": format_time, + "InstantTimeToEmpty": format_time, + "FullToEmptyTime": format_time, + "WearRatio": percentage, + "ChargeRatio": percentage, + } + def humanize_data(k, v): if k in humanize_index: @@ -69,12 +79,15 @@ def humanize_data(k, v): else: return v + def wear_ratio(info): return int(info["MaxCapacity"]) / int(info["DesignCapacity"]) + def charge_ratio(info): return int(info["CurrentCapacity"]) / int(info["MaxCapacity"]) + def full_to_empty_time(info): if get_data(info, "Amperage") < 0: return -int(info["MaxCapacity"])*60 / get_data(info, "Amperage") @@ -82,17 +95,19 @@ def full_to_empty_time(info): return 65535 synthetize_index = { - "Amperage": lambda i: fix_integer(i["Amperage"]), - "InstantAmperage": lambda i: fix_integer(i["InstantAmperage"]), - "WearRatio": wear_ratio, - "ChargeRatio": charge_ratio, - "FullToEmptyTime": full_to_empty_time, - } + "Amperage": lambda i: fix_integer(i["Amperage"]), + "InstantAmperage": lambda i: fix_integer(i["InstantAmperage"]), + "WearRatio": wear_ratio, + "ChargeRatio": charge_ratio, + "FullToEmptyTime": full_to_empty_time, + } + def synthetize_data(battery_info, k): if k in synthetize_index: return synthetize_index[k](battery_info) + def get_data(battery_info, k): if k in synthetize_index: return synthetize_data(battery_info, k) @@ -101,31 +116,33 @@ def get_data(battery_info, k): else: raise KeyError("%s" % k) + keys_to_show = [ - "Temperature", - "CycleCount", - #"DesignCycleCount9C", - "DesignCapacity", - "MaxCapacity", - "WearRatio", - "CurrentCapacity", - "ChargeRatio", - "Voltage", - "Amperage", - "InstantAmperage", - "InstantTimeToEmpty", - "TimeRemaining", - "AvgTimeToEmpty", - "AvgTimeToFull", - "FullToEmptyTime", - ] + "Temperature", + "CycleCount", + #"DesignCycleCount9C", + "DesignCapacity", + "MaxCapacity", + "WearRatio", + "CurrentCapacity", + "ChargeRatio", + "Voltage", + "Amperage", + "InstantAmperage", + "InstantTimeToEmpty", + "TimeRemaining", + "AvgTimeToEmpty", + "AvgTimeToFull", + "FullToEmptyTime", + ] + def print_key_value(k, v): if v is not None: print("%s = %s" % (k, v)) -battery_info = ioreg_battery_dict() + +battery_info = ioreg_battery_dict() for k in keys_to_show: print_key_value(k, humanize_data(k, get_data(battery_info, k))) - diff --git a/battery.sh b/battery.sh deleted file mode 100755 index c0b8f8f..0000000 --- a/battery.sh +++ /dev/null @@ -1,122 +0,0 @@ -#!/bin/bash - -legacyinfo=$(ioreg -l -w 2048 | grep 'LegacyBatteryInfo') - -get_attribute() -{ - echo $legacyinfo | sed "s/.*\"$@\"=\([^,}][^,}]*\)[,}].*/\1/" -} - -is_negative() -{ - [ $(echo "$@ > (2^63-1)" | bc) == "1" ] -} - -two_complement() -{ - echo "2^64 - $@" | bc -} - -fix_negative() -{ - # is the value negative? - if is_negative "$@"; then - # compute two's complement - echo "-$(two_complement "$@")" - else - echo "$@" - fi -} - -get_integer_attribute() -{ - fix_negative "$(get_attribute "$@")" -} - -amperage() -{ - get_integer_attribute 'Amperage' -} - -full_capacity() -{ - get_integer_attribute 'Capacity' -} - -current_capacity() -{ - get_integer_attribute 'Current' -} - -voltage() -{ - get_integer_attribute 'Voltage' -} - -cycle_count() -{ - get_integer_attribute 'Cycle Count' -} - -discharge_time() -{ - hours=$(echo "-$(current_capacity) / $(amperage)" | bc) - minutes=$(echo "-( $(current_capacity) % $(amperage) ) * 60 / $(amperage)" | bc) - echo "$hours:$minutes" -} - -full_discharge_time() -{ - hours=$(echo "-$(full_capacity) / $(amperage)" | bc) - minutes=$(echo "-( $(full_capacity) % $(amperage) ) * 60 / $(amperage)" | bc) - echo "$hours:$minutes" -} - -charge_time() -{ - hours=$(echo "( $(full_capacity) - $(current_capacity) ) / $(amperage)" | bc) - minutes=$(echo "( ( $(full_capacity) - $(current_capacity) ) % $(amperage) ) * 60 / $(amperage)" | bc) - echo "$hours:$minutes" -} - -full_charge_time() -{ - hours=$(echo "$(full_capacity) / $(amperage)" | bc) - minutes=$(echo "( $(full_capacity) % $(amperage) ) * 60 / $(amperage)" | bc) - echo "$hours:$minutes" -} - -remaining_time() -{ - if [ $(echo "$(amperage) < 0" | bc) == "1" ]; then - discharge_time - elif [ $(echo "$(amperage) > 0" | bc) == "1" ]; then - charge_time - else - echo "0:00" - fi -} - -full_time() -{ - if [ $(echo "$(amperage) < 0" | bc) == "1" ]; then - full_discharge_time - elif [ $(echo "$(amperage) > 0" | bc) == "1" ]; then - full_charge_time - else - echo "0:00" - fi -} - -charge_ratio() -{ - echo "$(echo "( $(current_capacity) * 100 / $(full_capacity) )" | bc)%" -} - -attributes=('amperage' 'full_capacity' 'current_capacity' 'voltage' 'cycle_count' 'remaining_time' 'charge_ratio' 'full_time') - -for ((i=0;i<${#attributes[*]};i++)); do - data=$(${attributes[$i]}) - echo "${attributes[$i]}: $data" -done -