summaryrefslogtreecommitdiff
path: root/external/plyer/platforms/linux/battery.py
diff options
context:
space:
mode:
Diffstat (limited to 'external/plyer/platforms/linux/battery.py')
-rw-r--r--external/plyer/platforms/linux/battery.py46
1 files changed, 46 insertions, 0 deletions
diff --git a/external/plyer/platforms/linux/battery.py b/external/plyer/platforms/linux/battery.py
new file mode 100644
index 0000000..0cdb763
--- /dev/null
+++ b/external/plyer/platforms/linux/battery.py
@@ -0,0 +1,46 @@
+from subprocess import Popen, PIPE
+from plyer.facades import Battery
+from plyer.utils import whereis_exe
+
+from os import environ
+
+
+class LinuxBattery(Battery):
+ def _get_state(self):
+ old_lang = environ.get('LANG')
+ environ['LANG'] = 'C'
+
+ status = {"isCharging": None, "percentage": None}
+
+ # We are supporting only one battery now
+ dev = "/org/freedesktop/UPower/device/battery_BAT0"
+ upower_process = Popen(["upower", "-d", dev],
+ stdout=PIPE)
+ output = upower_process.communicate()[0]
+
+ environ['LANG'] = old_lang
+
+ if not output:
+ return status
+
+ power_supply = percentage = None
+ for l in output.splitlines():
+ if 'power supply' in l:
+ power_supply = l.rpartition(':')[-1].strip()
+ if 'percentage' in l:
+ percentage = float(l.rpartition(':')[-1].strip()[:-1])
+
+ if(power_supply):
+ status['isCharging'] = power_supply != "yes"
+
+ status['percentage'] = percentage
+
+ return status
+
+
+def instance():
+ import sys
+ if whereis_exe('upower'):
+ return LinuxBattery()
+ sys.stderr.write("upower not found.")
+ return Battery()