My favorites | Sign in
Project Home Downloads Wiki Issues Source
Repository:
Checkout   Browse   Changes   Clones    
 
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
# Linux 2.x platform config. Requires acpitool.
#
# Send bug reports and suggestions to:
# Andy Mikhaylenko <andy@neithere.net>

import subprocess

__all__ = ['ac', 'charging', 'percent', 'lifetime', 'tooltip']

# XXX what if there are more batteries?
BATTERY = 'Battery #1'

def _parse(data):
lines = data.split('\n')
pairs = (x.split(':', 1) for x in lines if x)
clean_pairs = ((k.strip(), v.strip()) for k, v in pairs)
return dict(clean_pairs)

try:
p = subprocess.Popen(['acpitool'], stdout=subprocess.PIPE)
except OSError:
raise OSError('acpitool is required. Try "apt-get install acpitool" '
'or whatever makes sense on your distro.')
data = p.communicate()[0]
info = _parse(data)

ac = info['AC adapter'] == 'on-line'

try:
# "discharging, 83.18%, 00:31:28"
b_status, b_percent, b_life = info[BATTERY].split(', ')
except ValueError:
# "charged, 81.89%"
b_status, b_percent = info[BATTERY].split(', ')
lifetime = -1
else:
h,m,s = (int(x) for x in b_life.split(':'))
lifetime = h + 60*(m + 60*s)

charging = b_status == 'charging'
percent = float(b_percent[:-1])

tooltip = '' #'Hi ;-)'

Change log

fcfdb10cbe74 by Martin Tournoij <mar...@arp242.net> on Jul 13, 2011   Diff
Many minor updates
Go to: 
Project members, sign in to write a code review

Older revisions

a54427f2dfc9 by Martin Tournoij <mar...@arp242.net> on Jul 12, 2011   Diff
Hello world!
All revisions of this file

File info

Size: 1113 bytes, 43 lines
Powered by Google Project Hosting