battery cruft cleanup

This commit is contained in:
Loic Nageleisen 2014-01-23 14:16:46 +01:00
parent e7833c745a
commit f18becd77b
2 changed files with 63 additions and 168 deletions

View file

@ -5,13 +5,16 @@ 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]
output = Popen(["ioreg", "-r", "-k", "LegacyBatteryInfo", "-w", "0"],
stdout=PIPE).communicate()[0]
try: # python3
return str(output, encoding='utf-8')
except TypeError: # python2
return output
def parse_ioreg_dict(output):
return dict(
[(kw.strip().strip('"'), vw.strip())
@ -23,18 +26,22 @@ def parse_ioreg_dict(output):
]
)
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,6 +50,7 @@ def ioreg_battery_dict():
def fix_integer(string):
return fix_negative(int(string))
def format_time(string):
minutes = int(string)
if minutes == 65535:
@ -50,6 +58,7 @@ def format_time(string):
else:
return "%s:%s" % (minutes // 60, minutes % 60)
def percentage(ratio):
return "%s%%" % (int(ratio * 100))
@ -63,18 +72,22 @@ humanize_index = {
"ChargeRatio": percentage,
}
def humanize_data(k, v):
if k in humanize_index:
return humanize_index[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")
@ -89,10 +102,12 @@ synthetize_index = {
"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,6 +116,7 @@ def get_data(battery_info, k):
else:
raise KeyError("%s" % k)
keys_to_show = [
"Temperature",
"CycleCount",
@ -120,12 +136,13 @@ keys_to_show = [
"FullToEmptyTime",
]
def print_key_value(k, v):
if v is not None:
print("%s = %s" % (k, v))
battery_info = ioreg_battery_dict()
for k in keys_to_show:
print_key_value(k, humanize_data(k, get_data(battery_info, k)))

View file

@ -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