From a89b421ebba643e1c038e5d9f52c47f2939847a8 Mon Sep 17 00:00:00 2001 From: enkore Date: Sun, 24 Feb 2013 21:46:29 +0100 Subject: [PATCH] battery --- i3pystatus/backlight.py | 4 +--- i3pystatus/battery.py | 46 +++++++++++++++++++++++++++-------------- i3pystatus/core/util.py | 17 +++++++++++---- 3 files changed, 45 insertions(+), 22 deletions(-) diff --git a/i3pystatus/backlight.py b/i3pystatus/backlight.py index 1c2b5a3..e0878e0 100644 --- a/i3pystatus/backlight.py +++ b/i3pystatus/backlight.py @@ -1,7 +1,5 @@ from i3pystatus.file import File -__imported__ = [File] - class Backlight(File): """ Screen backlight info @@ -22,7 +20,7 @@ class Backlight(File): backlight="acpi_video0" format="{brightness}/{max_brightness}" - interval=1 + interval=5 base_path = "/sys/class/backlight/{backlight}/" components={ "brightness": (int, "brightness"), diff --git a/i3pystatus/battery.py b/i3pystatus/battery.py index 93c40bf..1bbc086 100644 --- a/i3pystatus/battery.py +++ b/i3pystatus/battery.py @@ -2,6 +2,7 @@ # -*- coding: utf-8 -*- from i3pystatus import IntervalModule +from .core.util import PrefixedKeyDict class Battery: """ @@ -36,6 +37,21 @@ class Battery: setattr(self, self.lchop(key), self.convert(value)) +class RemainingCalculator: + def __init__(self, energy, power): + self.remaining_time = (energy / power) * 60 + self.hours, self.minutes = map(int, divmod(self.remaining_time, 60)) + + def get_dict(self, prefix): + d = PrefixedKeyDict(prefix) + d.update({ + "str": "{}:{:02}".format(self.hours, self.minutes), + "hm": "{}h:{:02}m".format(self.hours, self.minutes), + "hours": self.hours, + "mins": self.minutes, + }) + return d + class BatteryChecker(IntervalModule): """ This class uses the /proc/acpi/battery interface to check for the @@ -54,7 +70,7 @@ class BatteryChecker(IntervalModule): color = "#ffffff" battery = Battery(self.base_path) - fdict = dict.fromkeys(("remaining", "remaining_hm"), "") + fdict = dict.fromkeys(("remaining_str", "remaining_hm"), "") status = battery.STATUS energy_now = battery.ENERGY_NOW @@ -65,23 +81,23 @@ class BatteryChecker(IntervalModule): fdict["percentage_design"] = (energy_now / battery.ENERGY_FULL_DESIGN) * 100 fdict["consumption"] = power_now / 1000000 + if not power_now: + return + if status == "Full": fdict["status"] = "FULL" - elif status == "Discharging": - fdict["status"] = "DIS" - remaining_time = (energy_now / power_now) * 60 - hours, minutes = map(int, divmod(remaining_time, 60)) + else: + if status == "Discharging": + fdict["status"] = "DIS" + remaining = RemainingCalculator(energy_now, power_now) - fdict["remaining"] = "{}:{:02}".format(hours, minutes) - fdict["remaining_hm"] = "{}h {:02}m".format(hours, minutes) - fdict["remaining_hours"] = hours - fdict["remaining_mins"] = minutes - - if remaining_time < 15: - urgent = True - color = "#ff0000" - else: # Charging, Unknown etc. (My thinkpad says Unknown if close to fully charged) - fdict["status"] = "CHR" + if remaining.remaining_time < 15: + urgent = True + color = "#ff0000" + else: # Charging, Unknown etc. (My thinkpad says Unknown if close to fully charged) + fdict["status"] = "CHR" + remaining = RemainingCalculator(energy_full-energy_now, power_now) + fdict.update(remaining.get_dict("remaining_")) self.output = { "full_text": self.format.format(**fdict).strip(), diff --git a/i3pystatus/core/util.py b/i3pystatus/core/util.py index 3635ef2..179cf98 100644 --- a/i3pystatus/core/util.py +++ b/i3pystatus/core/util.py @@ -9,6 +9,7 @@ __all__ = [ "SettingsBase", "ClassFinder", "ModuleList", + "KeyConstraintDict", "PrefixedKeyDict", ] class ModuleList(collections.UserList): @@ -22,6 +23,15 @@ class ModuleList(collections.UserList): module.registered(self.status_handler) super().append(module) +class PrefixedKeyDict(collections.UserDict): + def __init__(self, prefix): + super().__init__() + + self.prefix = prefix + + def __setitem__(self, key, value): + super().__setitem__(self.prefix + key, value) + class KeyConstraintDict(collections.UserDict): class MissingKeys(Exception): def __init__(self, keys): @@ -41,15 +51,15 @@ class KeyConstraintDict(collections.UserDict): else: raise KeyError(key) - def missing(self): - return self.required_keys - (self.seen_keys & self.required_keys) - def __iter__(self): if self.missing(): raise self.MissingKeys(self.missing()) return self.data.__iter__() + def missing(self): + return self.required_keys - (self.seen_keys & self.required_keys) + class SettingsBase: """ Support class for providing a nice and flexible settings interface @@ -125,7 +135,6 @@ class ClassFinder: return predicate def search_module(self, module): - # Neat trick: [(x,y),(u,v)] becomes [(x,u),(y,v)] return list(zip(*inspect.getmembers(module, self.predicate_factory(module))))[1] def get_class(self, module):