battery2go/client/battery2go

40 lines
1.4 KiB
Python
Executable file

#!/usr/bin/env python3
import argparse
import configparser
import os
LOW = 0
MEDIUM = 1
HIGH = 2
LOW_THRESHOLD = 20
HIGH_THRESHOLD = 80
def generate_output(percentage: float, charging: bool, remaining: str):
if percentage >= 0:
status = LOW if percentage < LOW_THRESHOLD else MEDIUM if percentage < HIGH_THRESHOLD else HIGH
color = "green" if charging == True else "blue" if status == HIGH else "orange" if status == MEDIUM else "red"
icon = "battery-" + ("low" if status == LOW else "good" if status == MEDIUM else "full") + ("-charging" if charging else "")
print(f"{percentage:.0f}% | color={color} iconName={icon}")
print("---")
print("Time to %s: %s" % ("full" if charging else "empty", remaining))
else:
print("Error! | color=red iconName=battery-missing")
print("---")
print("Data file corrupted or not found")
def read_values(datafile: os.PathLike):
config = configparser.ConfigParser()
config.read(datafile)
return config.getfloat("BATTERY", "percentage"), config.getboolean("BATTERY", "charging"), config["BATTERY"]["remaining"]
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("datafile")
args = parser.parse_args()
try:
generate_output(*read_values(args.datafile))
except:
generate_output(-1, False, "")