Version 2.3.4

Signed-off-by: Jeroen Oudshoorn <oudshoorn.jeroen@gmail.com>

Signed-off-by: Jeroen Oudshoorn <oudshoorn.jeroen@gmail.com>
This commit is contained in:
Jeroen Oudshoorn
2023-09-08 20:20:46 +02:00
parent ed0b06b701
commit 58cad38d2f

View File

@ -1,18 +1,16 @@
import os import os
import glob import glob
import _thread
import threading import threading
import importlib, importlib.util import importlib, importlib.util
import logging import logging
from concurrent.futures import ThreadPoolExecutor
default_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "default") default_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "default")
loaded = {} loaded = {}
database = {} database = {}
locks = {} locks = {}
THREAD_POOL_SIZE = 5
executor = ThreadPoolExecutor(max_workers=THREAD_POOL_SIZE)
class Plugin: class Plugin:
@classmethod @classmethod
@ -48,13 +46,13 @@ def toggle_plugin(name, enable=True):
if name not in pwnagotchi.config['main']['plugins']: if name not in pwnagotchi.config['main']['plugins']:
pwnagotchi.config['main']['plugins'][name] = dict() pwnagotchi.config['main']['plugins'][name] = dict()
pwnagotchi.config['main']['plugins'][name]['enabled'] = enable pwnagotchi.config['main']['plugins'][name]['enabled'] = enable
save_config(pwnagotchi.config, '/etc/pwnagotchi/config.toml')
if not enable and name in loaded: if not enable and name in loaded:
if getattr(loaded[name], 'on_unload', None): if getattr(loaded[name], 'on_unload', None):
loaded[name].on_unload(view.ROOT) loaded[name].on_unload(view.ROOT)
del loaded[name] del loaded[name]
if pwnagotchi.config:
save_config(pwnagotchi.config, '/etc/pwnagotchi/config.toml')
return True return True
if enable and name in database and name not in loaded: if enable and name in database and name not in loaded:
@ -66,6 +64,8 @@ def toggle_plugin(name, enable=True):
one(name, 'config_changed', pwnagotchi.config) one(name, 'config_changed', pwnagotchi.config)
one(name, 'ui_setup', view.ROOT) one(name, 'ui_setup', view.ROOT)
one(name, 'ready', view.ROOT._agent) one(name, 'ready', view.ROOT._agent)
if pwnagotchi.config:
save_config(pwnagotchi.config, '/etc/pwnagotchi/config.toml')
return True return True
return False return False
@ -88,21 +88,19 @@ def locked_cb(lock_name, cb, *args, **kwargs):
def one(plugin_name, event_name, *args, **kwargs): def one(plugin_name, event_name, *args, **kwargs):
global loaded global loaded
if not executor._shutdown:
if plugin_name in loaded: if plugin_name in loaded:
plugin = loaded[plugin_name] plugin = loaded[plugin_name]
cb_name = 'on_%s' % event_name cb_name = 'on_%s' % event_name
callback = getattr(plugin, cb_name, None) callback = getattr(plugin, cb_name, None)
if callback is not None and callable(callback): if callback is not None and callable(callback):
try: try:
lock_name = "%s::%s" % (plugin_name, cb_name) lock_name = "%s::%s" % (plugin_name, cb_name)
locked_cb_args = (lock_name, callback, *args, *kwargs) locked_cb_args = (lock_name, callback, *args, *kwargs)
executor.submit(locked_cb, *locked_cb_args) _thread.start_new_thread(locked_cb, locked_cb_args)
except Exception as e: except Exception as e:
logging.error("error while running %s.%s : %s" % (plugin_name, cb_name, e)) logging.error("error while running %s.%s : %s" % (plugin_name, cb_name, e))
logging.error(e, exc_info=True) logging.error(e, exc_info=True)
else:
logging.warning("Executor is shut down. Cannot schedule new futures.")
def load_from_file(filename): def load_from_file(filename):