coding utf-8 import os from subprocess import PIPE Popen def is_chargi

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# -*- coding: utf-8 -*-
import os
from subprocess import PIPE, Popen
def is_charging(info):
line = info[1].split(':')
return line[1].strip() == 'on-line'
def parse(info):
line = info[0].split(':',1)[1].strip().split(', ')
if line[0] in ['charging','discharging']:
charging = line[0] == 'charging'
percent = int(float(line[1][:-1])) # '%'
left = line[2]
return charging,percent,left
else: #'charged'
return True, None, None
def format_left(left):
res = ''
h,m,s = map(int,left.split(':'))
if h>0:
if h == 1:
res += '1 hour '
else:
res += '%d hours ' % (h,)
if m>0:
if m%10 == 1:
res += '%d minute ' % (m,)
else:
res += '%d minutes '%(m,)
if s>0:
if s%10 == 1:
res += '%d second ' % (s,)
else:
res += '%d seconds '%(s,)
return res + 'left'
def warn(percent, left):
phrase = "Warning: low bettery. %d percent. %s"
left_formatted = format_left(left)
res = phrase % (percent, left_formatted)
os.system('espeak "%s"' % (res,))
def check_battery():
info = Popen(['acpitool'], stdout=PIPE).communicate()[0].splitlines()
charging, percent, left = parse(info)
percent =5
if not charging and percent<10:
warn(percent, left)
if __name__ == '__main__':
check_battery()