|
|
|
|
@@ -5,7 +5,6 @@ import sys
|
|
|
|
|
import traceback
|
|
|
|
|
from configparser import ConfigParser
|
|
|
|
|
import logging
|
|
|
|
|
from systemd import journal
|
|
|
|
|
import inspect
|
|
|
|
|
from enum import Enum, auto
|
|
|
|
|
import signal
|
|
|
|
|
@@ -13,7 +12,7 @@ from importlib import import_module
|
|
|
|
|
from threading import Thread, Lock
|
|
|
|
|
|
|
|
|
|
import inspect
|
|
|
|
|
from threading import Thread
|
|
|
|
|
from threading import Thread, Event
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def is_number(s):
|
|
|
|
|
@@ -63,6 +62,7 @@ class Commands(Thread):
|
|
|
|
|
self.cmods = {}
|
|
|
|
|
self.config = config
|
|
|
|
|
self.log = log
|
|
|
|
|
self.stop_all_event = None
|
|
|
|
|
self.stopped = False
|
|
|
|
|
self.nickname = None
|
|
|
|
|
if not (self.config is None):
|
|
|
|
|
@@ -187,6 +187,10 @@ class Commands(Thread):
|
|
|
|
|
|
|
|
|
|
return self.cmods[module]._execute_command(method, *args, **kwargs)
|
|
|
|
|
|
|
|
|
|
def stop_all(self):
|
|
|
|
|
if self.stop_all_event is not None:
|
|
|
|
|
self.stop_all_event.set()
|
|
|
|
|
|
|
|
|
|
def cmd_help(self, *args, **kwargs):
|
|
|
|
|
"""Help of module commands.
|
|
|
|
|
Params:
|
|
|
|
|
@@ -218,7 +222,7 @@ class Commands(Thread):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CommandsLoader:
|
|
|
|
|
def __init__(self, config: ConfigParser, log: logging.Handler, modpath: str):
|
|
|
|
|
def __init__(self, stop_event: Event, config: ConfigParser, log: logging.Handler, modpath: str):
|
|
|
|
|
self.config = config
|
|
|
|
|
self.modpath = modpath
|
|
|
|
|
sys.path.append(os.path.join(modpath))
|
|
|
|
|
@@ -232,6 +236,7 @@ class CommandsLoader:
|
|
|
|
|
self.prefcmds = prefcmds
|
|
|
|
|
self.log = log
|
|
|
|
|
self.lock = Lock()
|
|
|
|
|
self.stop_event = stop_event
|
|
|
|
|
self._load_commands()
|
|
|
|
|
self._set_cmods()
|
|
|
|
|
|
|
|
|
|
@@ -276,6 +281,7 @@ class CommandsLoader:
|
|
|
|
|
obj = c(conf, self.log)
|
|
|
|
|
obj.log = self.log
|
|
|
|
|
obj.lock = self.lock
|
|
|
|
|
obj.stop_all_event = self.stop_event
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
return obj
|
|
|
|
|
@@ -338,6 +344,10 @@ class AppEngine:
|
|
|
|
|
self.log.setLevel(logging.DEBUG)
|
|
|
|
|
else:
|
|
|
|
|
self.log.setLevel(logging.WARNING)
|
|
|
|
|
# Mechanism to stop the application
|
|
|
|
|
self.stop_event = Event()
|
|
|
|
|
self.stop_thread = Thread(target=self.wait_stop, args=(self.stop_event,))
|
|
|
|
|
self.stop_thread.start()
|
|
|
|
|
|
|
|
|
|
def signal_handler(self, sig, frame):
|
|
|
|
|
print("\nExiting.")
|
|
|
|
|
@@ -364,17 +374,17 @@ class AppEngine:
|
|
|
|
|
if is_writeable:
|
|
|
|
|
self.log.addHandler(logging.FileHandler(fname))
|
|
|
|
|
else:
|
|
|
|
|
self.log.addHandler(journal.JournalHandler())
|
|
|
|
|
self.log.error('No write permissions: "{}"'.format(fname))
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
self.log.addHandler(journal.JournalHandler())
|
|
|
|
|
|
|
|
|
|
def exec(self, modpath: str = ""):
|
|
|
|
|
self.cl = CommandsLoader(self.conf, self.log, modpath)
|
|
|
|
|
self.cl = CommandsLoader(self.stop_event, self.conf, self.log, modpath)
|
|
|
|
|
self.cl.start()
|
|
|
|
|
self.cl.join()
|
|
|
|
|
self.cl.free()
|
|
|
|
|
|
|
|
|
|
def wait_stop(self, evnt):
|
|
|
|
|
evnt.wait()
|
|
|
|
|
self.cl.stop()
|
|
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
|
self.cl.stop()
|
|
|
|
|
self.stop_event.set()
|