From c72363092d5cc4642b9fa31ce266b97588054514 Mon Sep 17 00:00:00 2001 From: Matthias Pronk Date: Fri, 22 Feb 2013 13:56:37 +0100 Subject: [PATCH 1/4] battery checker added --- i3pystatus/__main__.py.dist | 6 +++++ i3pystatus/batterychecker.py | 46 ++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 i3pystatus/batterychecker.py diff --git a/i3pystatus/__main__.py.dist b/i3pystatus/__main__.py.dist index 8561d0f..9c262c0 100755 --- a/i3pystatus/__main__.py.dist +++ b/i3pystatus/__main__.py.dist @@ -64,5 +64,11 @@ status.register_module(nm) tb = thunderbirdnewmail.ThunderbirdMailChecker() status.register_module(tb) + +# the battery status checker module +battery = batterychecker.BatteryChecker() +status.register(battery) + + # start the handler status.run() diff --git a/i3pystatus/batterychecker.py b/i3pystatus/batterychecker.py new file mode 100644 index 0000000..0f927f8 --- /dev/null +++ b/i3pystatus/batterychecker.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from i3pystatus import IntervalModule + +class BatteryChecker(IntervalModule): + """ + This class uses the /proc/acpi/battery interface to check for the + battery status + """ + + def __init__(self, battery_ident="BAT0"): + self.battery_ident = battery_ident + self.base_path = "/sys/class/power_supply/%s" % self.battery_ident + + def run(self): + urgent = False + color = "#ffffff" + status = open('%s/status' % self.base_path, 'r').readline().strip() + energy_now = float(open('%s/energy_now' % self.base_path, 'r').readline()) + + if status == 'Full': + full_text = 'fully charged' + elif status == 'Charging': + energy_full = float(open('%s/energy_full' % self.base_path, 'r').readline()) + energy_percentage = (energy_now / energy_full) * 100 + full_text = '%.2f%% charged' + elif status == 'Discharging': + power_now = float(open('%s/power_now' % self.base_path, 'r').readline()) + remaining_time_secs = (energy_now / power_now) * 3600 + hours, remainder = divmod(remaining_time_secs, 3600) + minutes, seconds = divmod(remainder, 60) + full_text = "%ih %im %is remaining" % (hours, minutes, seconds) + if remaining_time_secs < (15*60): + urgent = True + color = "#ff0000" + else: + full_text = 'n/a' + + self.output = { + "full_text": full_text, + "name": "pybattery", + "instance": self.battery_ident, + "urgent": urgent, + "color": color + } From fdadd3f143c59755e15b8ec11fa9a2b798d65697 Mon Sep 17 00:00:00 2001 From: Matthias Pronk Date: Fri, 22 Feb 2013 13:59:28 +0100 Subject: [PATCH 2/4] clock added --- i3pystatus/__main__.py.dist | 5 +++++ i3pystatus/clock.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 i3pystatus/clock.py diff --git a/i3pystatus/__main__.py.dist b/i3pystatus/__main__.py.dist index 9c262c0..1888afe 100755 --- a/i3pystatus/__main__.py.dist +++ b/i3pystatus/__main__.py.dist @@ -70,5 +70,10 @@ battery = batterychecker.BatteryChecker() status.register(battery) +# the clock +clock = clock.Clock() +status.register(clock) + + # start the handler status.run() diff --git a/i3pystatus/clock.py b/i3pystatus/clock.py new file mode 100644 index 0000000..13a3263 --- /dev/null +++ b/i3pystatus/clock.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import os, locale, datetime + +from i3pystatus import IntervalModule + +class Clock(IntervalModule): + """ + This class shows a clock + """ + + def __init__(self, format_string=None): + lang = os.environ.get('LANG', None) + if lang: + locale.setlocale(locale.LC_ALL, lang) + if not format_string: + lang = locale.getlocale()[0] + if lang == 'en_US': + # MDY format - United States of America + format_string = "%a %b %-d %X" + else: + # DMY format - almost all other countries + format_string = "%a %-d %b %X" + self.format_string = format_string + + def run(self): + full_text = datetime.datetime.now().strftime(self.format_string) + self.output = { + "full_text": full_text, + "name": "pyclock", + "urgent": False, + "color": "#ffffff" + } From 0adbc0ad28d98c5b098efbecf26bc44ab06eb7dd Mon Sep 17 00:00:00 2001 From: Matthias Pronk Date: Fri, 22 Feb 2013 14:11:05 +0100 Subject: [PATCH 3/4] remove unused import --- i3pystatus/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/i3pystatus/__init__.py b/i3pystatus/__init__.py index effd7a3..36865f0 100644 --- a/i3pystatus/__init__.py +++ b/i3pystatus/__init__.py @@ -2,7 +2,6 @@ import sys import json -import urllib.request, urllib.error, urllib.parse from threading import Thread import time from contextlib import contextmanager From 400d639e31a8787e7aa78c05a01b601ef182ff07 Mon Sep 17 00:00:00 2001 From: Matthias Pronk Date: Fri, 22 Feb 2013 14:15:14 +0100 Subject: [PATCH 4/4] register_module has been renamed to register --- i3pystatus/__main__.py.dist | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/i3pystatus/__main__.py.dist b/i3pystatus/__main__.py.dist index 1888afe..4a88c18 100755 --- a/i3pystatus/__main__.py.dist +++ b/i3pystatus/__main__.py.dist @@ -44,7 +44,7 @@ mailsettings = { ] } mailchecker = mailchecker.MailChecker(mailsettings) -status.register_module(mailchecker) +status.register(mailchecker) # the mods.de forum new bookmarks module mdesettings = { @@ -52,17 +52,17 @@ mdesettings = { "password": "your_password" } mde = modsde.ModsDeChecker(mdesettings) -status.register_module(mde) +status.register(mde) # the notmuch mail checker module db_path = "path_to_your_notmuch_database" nm = notmuch.NotmuchMailChecker(db_path) -status.register_module(nm) +status.register(nm) # the thunderbird dbus new mail checker module tb = thunderbirdnewmail.ThunderbirdMailChecker() -status.register_module(tb) +status.register(tb) # the battery status checker module