gajim-plural/common/sleepy.py

53 lines
1.2 KiB
Python
Raw Normal View History

2004-01-02 16:21:02 +01:00
#!/usr/bin/env python
"""A Quick class to tell if theres any activity on your machine"""
import time
import idle
2004-01-02 16:21:02 +01:00
from string import find, lower
STATE_UNKNOWN = "OS probably not supported"
2004-01-03 00:27:28 +01:00
STATE_XAWAY = "extanted away"
STATE_AWAY = "away"
2004-01-02 16:21:02 +01:00
STATE_AWAKE = "awake"
NOT_SUPPORTED = 0
class Sleepy:
2004-01-03 00:27:28 +01:00
def __init__(self, interval1 = 60, interval2 = 120, devices = ['keyboard', 'mouse', 'ts'] ):
2004-01-02 16:21:02 +01:00
self.interval1 = interval1
self.interval2 = interval2
2004-01-02 16:21:02 +01:00
self.state = STATE_AWAKE ## assume were awake to stake with
try:
idle.init()
2004-01-02 16:21:02 +01:00
except:
NOT_SUPPORTED = 1
self.state = STATE_UNKNOWN
def poll(self):
if NOT_SUPPORTED: return -1
now = time.time()
idleTime = idle.getIdleSec()
if idleTime > self.interval2:
self.state = STATE_XAWAY
elif idleTime > self.interval1:
self.state = STATE_AWAY
else:
self.state = STATE_AWAKE
2004-01-02 16:21:02 +01:00
return 1
def getState(self):
return self.state
def setState(self,val):
self.state = val
if __name__ == '__main__':
s = Sleepy(10)
while s.poll():
print "state is %s" % s.getState()
time.sleep(5)