-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponents.py
More file actions
85 lines (71 loc) · 2.62 KB
/
components.py
File metadata and controls
85 lines (71 loc) · 2.62 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import platform
from os import path
from threading import Thread
from queue import Queue, Empty
from .config import ConfigLoader
from time import sleep
class _BaseComponent(Thread):
def __init__(self, queues):
super().__init__()
self._is_init = True
self._queues = queues
def run(self, *args, **kwargs):
raise NotImplementedError()
def stop(self):
raise NotImplementedError()
class ComponentManager(object):
def __init__(self):
self._components = []
self._config_keys = {}
self._queues = {}
self._queues['QueueComponentManager'] = Queue()
self._config = ConfigLoader(
path.dirname(path.realpath(__file__)), self._queues)
self._config.load('settings.json')
def add_component(self, Component):
component = Component(self._queues)
component.daemon = True
self._components.append(component)
self._queues['Queue{0}'.format(Component.__name__)] = Queue()
self._queues['ConfigQueue{0}'.format(Component.__name__)] = Queue()
def start_all(self):
for component in self._components:
if getattr(component, 'setup', None):
component.setup()
component.start()
print('Loading {} ... \033[0;32m[OK]\033[0;0m'.format(
component.__class__.__name__))
def stop_all(self):
print(self._components)
for component in self._components:
print('[{0}]'.format(component.__class__.__name__))
component.stop()
def join_all(self):
while True:
sleep(1)
for component in self._components:
component.join(0.1)
try:
cmd = self._queues['QueueComponentManager'].get(False)
except Empty:
pass
else:
func, *args = cmd.split(' ')
getattr(self, func)(*args)
def subscribe(self, component_name, key):
list_component = self._config_keys.get(key)
if list_component is None:
list_component = [component_name]
else:
list_component.append(component_name)
self._config_keys[key] = list_component
def update(self, key, value):
print('update')
list_component = self._config_keys.get(key, [])
for component in list_component:
self._queues['ConfigQueue{0}'.format(component)].put(
'{0} {1}'.format(key, value))
def ready(self):
for component in self._components:
if getattr(component, 'running', None):
component.running()