Second checkin, First as Lithium.
[rox-lithium.git] / apm.py
blob523e027b5bf6d9ff90209764fb14f5822c90be7f
1 ##############################################################################
2 ##
3 ## $Id: apm.py,v 1.2 2003/04/10 22:13:55 riemer Exp $
4 ##
5 ## Copyright (C) 2002-2003 Tilo Riemer <riemer@lincvs.org>
6 ## All rights reserved.
7 ##
8 ## Redistribution and use in source and binary forms, with or without
9 ## modification, are permitted provided that the following conditions
10 ## are met:
12 ## 1. Redistributions of source code must retain the above copyright
13 ## notice, this list of conditions and the following disclaimer.
14 ## 2. Redistributions in binary form must reproduce the above copyright
15 ## notice, this list of conditions and the following disclaimer in the
16 ## documentation and/or other materials provided with the distribution.
17 ## 3. The name of the author may not be used to endorse or promote products
18 ## derived from this software without specific prior written permission.
20 ## THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 ## IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 ## OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 ## IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 ## INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 ## NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 ## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 ## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 ## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 ## THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 ###############################################################################
35 import sys
38 #enums
39 OFFLINE = 0
40 ONLINE = 1
41 CHARGING = 2 #implies ONLINE
42 NOBATTERY = 3
44 UNKNOWN = -1
47 #exceptions
49 class ApmError(Exception):
50 """Base class for APM exceptions"""
51 pass
54 class ApmNoDevice(ApmError):
55 """Apm is not configured on this host"""
57 def __init__(self):
58 pass
60 def __str__(self):
61 return "Apm is not configured on this host"
64 class ApmNotImplemented(ApmError):
65 """No implementation for this operating system"""
67 def __init__(self):
68 pass
70 def __str__(self):
71 return "No implementation for this operating system"
74 class ApmNoApmLowLevel(ApmError):
75 """Apm_lowlevel module not found"""
77 def __init__(self):
78 pass
80 def __str__(self):
81 return "Apm_lowlevel module not found"
85 #interface
87 class Apm:
88 """Interface class for APM"""
90 def __init__(self):
91 res = sys.platform
92 if res.find("freebsd4") > -1:
93 self.apm = ApmGeneric() #we use apm_lowlevel
95 elif res.find("netbsd1") > -1:
96 self.apm = ApmGeneric() #we use apm_lowlevel
98 elif res.find("linux2") > -1:
99 self.apm = ApmLinux()
101 elif res.find("darwin") > -1:
102 self.apm = ApmGeneric() #defined in apm_lowlevel
104 else:
105 self.apm = None #throw exception (os unknown)
106 raise ApmNotImplemented
109 def update(self):
110 """Updates the APM state"""
111 self.apm.update()
114 def percent(self):
115 """Returns percentage capacity of all batteries"""
116 return self.apm.percent()
119 def time(self):
120 """Returns time of all batteries (in minutes)"""
121 return self.apm.time()
124 def charging_state(self):
125 """Returns ac state (off-/online/charging)"""
126 return self.apm.charging_state()
132 #base class for os dependent apm classes
134 class ApmBase:
135 def __init__(self):
136 self.ac_line_state = OFFLINE
138 self.life_percent = 0
139 self.life_time = 0 #0 seconds
141 #initial reading of apm info
142 self.update()
144 def percent(self):
145 #returns percentage capacity of all batteries
146 return self.life_percent
149 def time(self):
150 #returns time of all batteries (in minutes)
151 return self.life_time
154 def charging_state(self):
155 return self.ac_line_state
161 #implementation using apm_lowlevel.py
162 #there are implementations of apmLowlevel.py for NetBSD 1.6 and
163 #soon for FreeBSD 4 at the moment
165 class ApmGeneric(ApmBase):
166 def __init__(self):
167 try:
168 import apm_lowlevel
169 self.apm_lowlevel = apm_lowlevel
170 except ImportError:
171 raise ApmNoApmLowLevel
174 self.ac_line_state = OFFLINE
176 self.life_percent = 0
177 self.life_time = 0 #0 seconds
179 #initial reading of apm info
180 self.update()
183 def update(self):
184 apm_info = self.apm_lowlevel.state()
186 if (apm_info[0] < 0):
187 raise ApmNoDevice
189 self.life_percent = apm_info[1]
190 self.life_time = apm_info[2]
191 self.ac_line_state = apm_info[3]
197 #implementation for Linux
198 #if we want support more than battery info we need a low level module implemented using C
200 class ApmLinux(ApmBase):
201 def update(self):
202 #read /proc/apm and extract needed infos
204 try:
205 apm_proc = open("/proc/apm")
206 except IOError:
207 raise ApmNoDevice
210 line = apm_proc.readline()
211 token = line.split()
212 if token[3] == "0x00":
213 self.ac_line_state = OFFLINE
214 elif token[4] == "0x03":
215 self.ac_line_state = CHARGING
216 else:
217 self.ac_line_state = ONLINE
219 self.life_percent = int(token[6].split("%")[0])
221 self.life_time = int(token[7])
224 apm_proc.close()