1 ##############################################################################
3 ## $Id: apm.py,v 1.2 2003/04/10 22:13:55 riemer Exp $
5 ## Copyright (C) 2002-2003 Tilo Riemer <riemer@lincvs.org>
6 ## All rights reserved.
8 ## Redistribution and use in source and binary forms, with or without
9 ## modification, are permitted provided that the following conditions
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 ###############################################################################
41 CHARGING
= 2 #implies ONLINE
49 class ApmError(Exception):
50 """Base class for APM exceptions"""
54 class ApmNoDevice(ApmError
):
55 """Apm is not configured on this host"""
61 return "Apm is not configured on this host"
64 class ApmNotImplemented(ApmError
):
65 """No implementation for this operating system"""
71 return "No implementation for this operating system"
74 class ApmNoApmLowLevel(ApmError
):
75 """Apm_lowlevel module not found"""
81 return "Apm_lowlevel module not found"
88 """Interface class for APM"""
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:
101 elif res
.find("darwin") > -1:
102 self
.apm
= ApmGeneric() #defined in apm_lowlevel
105 self
.apm
= None #throw exception (os unknown)
106 raise ApmNotImplemented
110 """Updates the APM state"""
115 """Returns percentage capacity of all batteries"""
116 return self
.apm
.percent()
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
136 self
.ac_line_state
= OFFLINE
138 self
.life_percent
= 0
139 self
.life_time
= 0 #0 seconds
141 #initial reading of apm info
145 #returns percentage capacity of all batteries
146 return self
.life_percent
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
):
169 self
.apm_lowlevel
= apm_lowlevel
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
184 apm_info
= self
.apm_lowlevel
.state()
186 if (apm_info
[0] < 0):
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
):
202 #read /proc/apm and extract needed infos
205 apm_proc
= open("/proc/apm")
210 line
= apm_proc
.readline()
212 if token
[3] == "0x00":
213 self
.ac_line_state
= OFFLINE
214 elif token
[4] == "0x03":
215 self
.ac_line_state
= CHARGING
217 self
.ac_line_state
= ONLINE
219 self
.life_percent
= int(token
[6].split("%")[0])
221 self
.life_time
= int(token
[7])