-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigure.py
More file actions
288 lines (240 loc) · 9.55 KB
/
Copy pathConfigure.py
File metadata and controls
288 lines (240 loc) · 9.55 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import getopt
import ssl
import sys
import logging
import threading
import time
from typing import Any, Coroutine
from textual.message import Message
import Tools.PluginManager as pman
import Tools.Config as tc
import signal
import ctypes
import datetime
from pathlib import Path
import faulthandler
import dataclasses
LIB = 'libcap.so.2'
try:
libcap = ctypes.CDLL(LIB)
except OSError:
print(
'Library {} not found. Unable to set thread name.'.format(LIB)
)
else:
def _name_hack(self):
# PR_SET_NAME = 15
libcap.prctl(15, self.name.encode())
threading.Thread._bootstrap_original(self)
threading.Thread._bootstrap_original = threading.Thread._bootstrap
threading.Thread._bootstrap = _name_hack
import Tools.PropagetingThread as propt
propt.installProagetingThread()
try:
import setproctitle
except ImportError as ie:
from Tools import error as err
try:
err.try_install_package('setproctitle', throw=ie, ask=False)
except err.RestartError:
try:
import setproctitle
except:
pass
except:
pass
try:
import textual
except ImportError as ie:
from Tools import error as err
try:
err.try_install_package('textual', throw=ie, ask=False)
except err.RestartError:
try:
import setproctitle
except:
pass
from textual.app import App, ComposeResult
from textual.logging import TextualHandler
from textual.widgets import Tabs, Tab, Button, TabbedContent, TabPane, OptionList, Footer, RichLog, Label
from textual.widgets.option_list import Option, Separator
from textual.binding import Binding
from threading import Timer
class TextHandler(logging.Handler):
"""Class for logging to a TextLog widget"""
def __init__(self, textlog: RichLog):
# run the regular Handler __init__
logging.Handler.__init__(self)
# Store a reference to the Text it will log to
self.text = textlog
def emit(self, record):
msg = self.format(record)
style = "gray"
match record.levelno:
case 10: style = "blue"
case 20: style = "white"
case 30: style = "yellow"
case 40: style = "red"
case 50: style = "red"
# style your output depending on e.g record.levelno here
self.text.write(f"[bold {style}]{msg}")
class PluginInstallButton(Button):
_module_list: list[str] = []
_plugin_list = None
_current_option = None
def _press_threaded(self):
self.app._master_logger.info("Beginn Install via pip")
from Tools import error
for i in self._module_list:
try:
self.app._master_logger.info(f"PIP: Installing {i}...")
error.try_install_package(i, throw=ImportError("Install failed!"), ask=False)
except error.RestartError:
self.app._master_logger.info(f"PIP: Install {i}. OK.")
try:
self._plugin_list.on_option_list_option_selected(self._current_option)
except:
self.app._master_logger.exception("Configure Reload failed!")
def press(self):
thr = threading.Thread(name="Pip installer", daemon=True, target=self._press_threaded)
thr.start()
return super().press()
class PluginOption(Option):
_plugin:pman.PluginLoader|None = None
class PluginList(OptionList):
_timer = None
_log: logging.Logger = None
def reload_list(self):
self.app.reload_configs()
self.set_lists(self.app._all_configs, self.app._enabled_configs)
self.app.query_one("#main_tabbed_content").active = "plugin_pane"
def set_app(self, app, log):
self._timer = Timer(2.0, self.reload_list)
self._timer.start()
self._log = log
def set_lists(self, all:list[pman.PluginLoader], enabled:list[pman.PluginLoader]):
self.clear_options()
enabled_str = [x.getConfigKey() for x in enabled]
added = []
for i in all:
en = i.getConfigKey() in enabled_str
if i.getConfigKey() not in added:
op = PluginOption(f"[bold {'green' if en else 'red'}]{i.getConfigKey()}", i.getConfigKey())
op._plugin = i
self.add_option(
op
)
added.append(i.getConfigKey())
self._log.info("Plugin List reloaded!")
def on_option_list_option_selected(self, msg: OptionList.OptionSelected|PluginOption):
opt: PluginOption = msg if isinstance(msg, PluginOption) else msg.option
self.app.query_one("#main_tabbed_content").active = "cfg_plugin_tab"
self._log.debug(f"Selected Option {opt.prompt}")
tab: TabPane = self.app.query_one("#cfg_plugin_editor_label")
tab.remove_children()
l = opt._plugin.getNeededPipModules()
if len(l) > 0:
tab.mount(Label(f"Plugin {opt._plugin.getConfigKey()} PiP installer."))
tab.mount(Label((f" The Plugin Requests these PIP installs:")))
for i in l:
tab.mount(Label(f" {i}"))
btn = PluginInstallButton("Install", name="pip_do_install", id="do_pip_install")
btn._module_list = l
btn._plugin_list = self
btn._current_option = opt
tab.mount(btn)
else:
tab.mount(Label(f"Configure {opt._plugin.getConfigKey()}:"))
class ConfigureApp(App):
BINDINGS = [
Binding(key="q", action="quit", description="Quit"),
Binding(key="s", action="save", description="Save"),
Binding(key="r", action="plugins_reload", description="Reload"),
]
_ml = None
def __init__(self):
super().__init__()
log = logging.getLogger("Configure")
log.setLevel(logging.DEBUG)
# fh = logging.FileHandler('spam.log')
# fh.setLevel(logging.DEBUG)
# create console handler with a higher log level
ch = TextualHandler()
ch.setLevel(logging.DEBUG)
# create formatter and add it to the handlers
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)8s - %(message)s')
# fh.setFormatter(formatter)
ch.setFormatter(formatter)
log.addHandler(ch)
self._master_logger = log
prog_args = sys.argv[1:]
t_args = prog_args.copy()
configPath = "~/.config/mqttra.config"
while True:
try:
opts, args = getopt.gnu_getopt(t_args, "h?c:",
["help", "config=",])
break
except getopt.error as e:
opt = e.msg.replace("option ", "").replace(" not recognized", "")
t_args.remove(opt)
for opt, arg in opts:
if opt == "-c" or opt == "--config":
configPath = arg
elif opt == "-?" or opt == "-h" or opt == "help":
self._master_logger.info("""
============ HILFE ============
-c --config Konfigurations Datei angeben (Standartpfad ~/.config/mqttra.config)
-? -h --help Diese Nachricht anzeigen
""")
return
self.configPath = Path(configPath)
self._enabled_configs = []
self._all_configs = []
self._disabled_configs = []
def reload_configs(self):
self._master_logger.info("Build PluginManager environment...")
config = tc.config_factory(self.configPath, logger=self._master_logger, do_load=True, filesystem_listen=False)
from Tools import _std_dev_info
import Tools.Autodiscovery as ad
devInfo = _std_dev_info.DevInfoFactory.build_std_device_info(self._master_logger.getChild("std_dev"))
ad.Topics.set_standard_deviceinfo(devInfo)
try:
import Tools.error as err
err.set_system_mode(True)
except:
pass
self.pm = pman.PluginManager(self._master_logger, config)
self._master_logger.info("Getting enabled Plugins...")
self._enabled_configs = self.pm.needed_plugins(False)
self._master_logger.info("Get all Plugins...")
self._all_configs = self.pm.needed_plugins(True)
self._disabled_configs = [item for item in self._all_configs if item not in self._enabled_configs]
def compose(self) -> ComposeResult:
with TabbedContent(id="main_tabbed_content"):
with TabPane("Log", id="master_log_pane"):
yield RichLog(highlight=True, name="Logger", id="master_log", markup=True)
with TabPane("Plugins", id="plugin_pane"):
ml = PluginList(name="PluginList", id="plugin_list")
ml.set_app(self, self._master_logger)
self._ml = ml
yield ml
with TabPane("Configure", name="configure_plugin", id="cfg_plugin_tab"):
yield Label("Plugin Configuration Editor", id="cfg_plugin_editor_label")
yield Footer()
def on_ready(self):
text_log: RichLog = self.query_one("#master_log")
logger = self._master_logger
logger.setLevel(logging.DEBUG)
th = TextHandler(text_log)
th.setLevel(logging.DEBUG)
logger.addHandler(th)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)8s - %(message)s')
th.setFormatter(formatter)
def action_plugins_reload(self):
self._ml.reload_list()
if __name__ == "__main__":
app = ConfigureApp()
app.run()