Add plugins page

This commit is contained in:
dadav
2019-11-27 18:31:41 +01:00
parent 19775b7d27
commit cc5c46906f
7 changed files with 118 additions and 13 deletions

View File

@ -6,7 +6,7 @@ import logging
default_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "default")
loaded = {}
database = {}
class Plugin:
@classmethod
@ -18,6 +18,26 @@ class Plugin:
logging.debug("loaded plugin %s as %s" % (plugin_name, plugin_instance))
loaded[plugin_name] = plugin_instance
def toggle_plugin(name, enable=True):
"""
Load or unload a plugin
returns True if changed, otherwise False
"""
global loaded, database
if not enable and name in loaded:
if getattr(loaded[name], 'on_unload', None):
loaded[name].on_unload()
del loaded[name]
return True
if enable and name in database and name not in loaded:
load_from_file(database[name])
one(name, 'loaded')
return True
return False
def on(event_name, *args, **kwargs):
for plugin_name, plugin in loaded.items():
@ -48,10 +68,11 @@ def load_from_file(filename):
def load_from_path(path, enabled=()):
global loaded
global loaded, database
logging.debug("loading plugins from %s - enabled: %s" % (path, enabled))
for filename in glob.glob(os.path.join(path, "*.py")):
plugin_name = os.path.basename(filename.replace(".py", ""))
database[plugin_name] = filename
if plugin_name in enabled:
try:
load_from_file(filename)

View File

@ -466,7 +466,11 @@ class BTTether(plugins.Plugin):
logging.info("BT-TETHER: Successfully loaded ...")
self.ready = True
def on_unload(self):
self.ui.remove_element('bluetooth')
def on_ui_setup(self, ui):
self.ui = ui
ui.add_element('bluetooth', LabeledValue(color=BLACK, label='BT', value='-', position=(ui.width() / 2 - 15, 0),
label_font=fonts.Bold, text_font=fonts.Medium))

View File

@ -25,6 +25,10 @@ class Example(plugins.Plugin):
def on_loaded(self):
logging.warning("WARNING: this plugin should be disabled! options = " % self.options)
# called before the plugin is unloaded
def on_unload(self):
pass
# called hen there's internet connectivity
def on_internet_available(self, agent):
pass