2015-06-14 16:22:02 +00:00
|
|
|
from lib.six import moves
|
|
|
|
|
2014-07-15 02:00:53 +00:00
|
|
|
import threading
|
2014-07-08 22:17:34 +00:00
|
|
|
|
2018-01-04 15:05:16 +00:00
|
|
|
|
2014-07-08 22:17:34 +00:00
|
|
|
class Event:
|
2018-01-04 15:05:16 +00:00
|
|
|
def __init__(self, etype):
|
|
|
|
self._type = etype
|
2014-07-08 22:17:34 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def type(self):
|
|
|
|
return self._type
|
|
|
|
|
2018-01-04 15:05:16 +00:00
|
|
|
|
2014-07-15 02:00:53 +00:00
|
|
|
class Events(threading.Thread):
|
2014-07-08 22:17:34 +00:00
|
|
|
def __init__(self, callback):
|
|
|
|
super(Events, self).__init__()
|
2015-06-14 18:13:43 +00:00
|
|
|
self.queue = moves.queue.Queue()
|
2014-07-08 22:17:34 +00:00
|
|
|
self.daemon = True
|
|
|
|
self.callback = callback
|
2018-01-04 15:05:16 +00:00
|
|
|
self.name = 'EVENT-QUEUE'
|
|
|
|
self._stop = threading.Event()
|
|
|
|
|
|
|
|
def put(self, etype):
|
|
|
|
self.queue.put(etype)
|
2014-07-08 22:17:34 +00:00
|
|
|
|
2018-01-04 15:05:16 +00:00
|
|
|
def stop(self):
|
|
|
|
self._stop.set()
|
2014-07-08 22:17:34 +00:00
|
|
|
|
|
|
|
def run(self):
|
2018-01-04 15:05:16 +00:00
|
|
|
while not self._stop.is_set():
|
2014-07-08 22:17:34 +00:00
|
|
|
try:
|
|
|
|
# get event type
|
2018-01-04 15:05:16 +00:00
|
|
|
etype = self.queue.get(True, 1)
|
2014-07-08 22:17:34 +00:00
|
|
|
|
|
|
|
# perform callback if we got a event type
|
2018-01-04 15:05:16 +00:00
|
|
|
self.callback(etype)
|
2014-07-08 22:17:34 +00:00
|
|
|
|
|
|
|
# event completed
|
|
|
|
self.queue.task_done()
|
2015-06-14 18:13:43 +00:00
|
|
|
except moves.queue.Empty:
|
2018-01-04 15:05:16 +00:00
|
|
|
pass
|
2014-07-08 22:17:34 +00:00
|
|
|
|
2014-07-15 02:00:53 +00:00
|
|
|
# exiting thread
|
2018-01-04 15:05:16 +00:00
|
|
|
self._stop.clear()
|
2014-07-15 02:00:53 +00:00
|
|
|
|
2014-07-08 22:17:34 +00:00
|
|
|
# System Events
|
|
|
|
class SystemEvent(Event):
|
2018-01-04 15:05:16 +00:00
|
|
|
RESTART = 'RESTART'
|
|
|
|
SHUTDOWN = 'SHUTDOWN'
|