Skip to content

Commit

Permalink
[add] beefy plugin checker + failsafe plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
tomsimonart committed May 8, 2018
1 parent 03cce1c commit ab029df
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 25 deletions.
89 changes: 72 additions & 17 deletions GLM/glm.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
import importlib
if __name__ == "__main__" and __package__ == None:
from source.libs.rainbow import color, msg
from source.libs.pluginbase import PluginBase, VERSION
from source.specialplugins import failsafe
else:
from .source.libs.rainbow import color, msg
from .source.libs.pluginbase import PluginBase, VERSION
from .source.specialplugins import failsafe

VERSION = "0.0.3"
PLUGIN_PREFIX = "plugins"
PLUGIN_PACKAGE = "source.plugins"
PLUGIN_DIRECTORY = "./source/" + PLUGIN_PREFIX + "/"
Expand All @@ -35,25 +38,73 @@ def import_plugin(plugin):


def plugin_checker(main_plugin, start, *args):
"""Check wether a plugin is suitable for launch or not
"""Check whether a plugin is suitable for execution or not
Here are some free msg templates:
msg('OK', 0, 'plugin_checker', '', level=2, slevel='check')
msg('WARN', 1, 'plugin_checker', '', level=1, slevel='check')
msg('ERROR', 2, 'plugin_checker', '', level=0, slevel='check')
msg('FATAL', 3, 'plugin_checker', '', level=0, slevel='check')
"""
if not hasattr(main_plugin, "Plugin"):
msg("Plugin outdated", 2, "plugin_checker", level=0)
return False
if hasattr(main_plugin, "Plugin"): # Check if plugin is launchable
msg('OK', 0, 'plugin_checker', 'Plugin', level=3, slevel='check')
else:
loaded_plugin = main_plugin.Plugin(start, *args)
if not hasattr(loaded_plugin, "_start"):
msg("Plugin outdated", 2, "plugin_checker", level=0)
return False
if not hasattr(loaded_plugin, "version"):
msg("Plugin outdated", 2, "plugin_checker", level=0)
return False
msg('FATAL', 3, 'plugin_checker', 'Plugin', level=0, slevel='check')
return None

# Launch plugin (without start)
loaded_plugin = main_plugin.Plugin(start, *args)

if isinstance(loaded_plugin, PluginBase):
msg('OK', 0, 'plugin_checker', 'PluginBase', level=2, slevel='check')
else:
msg('ERROR', 2, 'plugin_checker', 'PluginBase', level=0, slevel='check')

if hasattr(loaded_plugin, "version"): # Check versions
current = semver.parse_version_info(VERSION)
plugin = semver.parse_version_info(loaded_plugin.version)

if current.major > plugin.major: # Check X.y.z
e_reason = 'Major version change, please update plugin'
error = (e_reason, VERSION, loaded_plugin.version)
msg('FATAL', 3, 'plugin_checker', *error, level=0, slevel='check')
return None
if current.minor > plugin.minor: # Check x.Y.z
e_reason = 'New functionalities are availible, please update plugin'
error = (e_reason, VERSION, loaded_plugin.version)
msg('WARN', 1, 'plugin_checker', *error, level=1, slevel='check')
elif current.patch > plugin.patch: # Check x.y.Z
e_reason = 'Some bugs were fixed, just sayin\''
error = (e_reason, VERSION, loaded_plugin.version)
msg('WARN', 1, 'plugin_checker', *error, level=1, slevel='check')
else: # Version is ok
e_reason = 'Version ok'
error = (e_reason, loaded_plugin.version)
msg('OK', 0, 'plugin_checker', *error, level=2, slevel='check')

else:
if loaded_plugin.version != VERSION:
msg("Plugin outdated", 2, "plugin_checker", loaded_plugin.version, level=0)
return False
else:
msg("Plugin version ok", 0, "plugin_checker", loaded_plugin.version, level=2)
msg('FATAL', 3, 'plugin_checker', 'No version', level=0, slevel='check')
return None

if hasattr(loaded_plugin, "_make_layout"): # Check if make layout exists
msg('OK', 0, 'plugin_checker', '_make_layout', level=2, slevel='check')
else:
e_reason = "No _make_layout"
msg('ERROR', 2, 'plugin_checker', e_reason, level=0, slevel='check')
return None

if hasattr(loaded_plugin, "_start"): # Check if start loop exists
msg('OK', 0, 'plugin_checker', '_start', level=2, slevel='check')
else:
msg('ERROR', 2, 'plugin_checker', 'No _start', level=0, slevel='check')
return None

if hasattr(loaded_plugin, "_event_loop"): # Check if event loop exists
msg('OK', 0, 'plugin_checker', '_event_loop', level=2, slevel='check')
else:
e_reason = 'No _event_loop'
msg('ERROR', 2, 'plugin_checker', e_reason, level=0, slevel='check')
return None

return loaded_plugin


Expand All @@ -63,6 +114,10 @@ def plugin_loader(plugin, start, *args):
if loaded_plugin:
print_plugin_info(loaded_plugin)
loaded_plugin = main_plugin.Plugin(True, *args)
else:
msg('ERROR', 2, 'plugin_loader', 'failsafe plugin activation')
loaded_plugin = failsafe.Plugin(True, *args)


def print_plugin_info(plugin):
if hasattr(plugin, "name"):
Expand Down
15 changes: 8 additions & 7 deletions GLM/source/libs/pluginbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
from .screen import Screen
from .templater import Templater

VERSION = "0.9.0"

class PluginBase:
def __init__(self, start, *args):
self.name = "No name"
self.author = "No author"
self.version = "0.0.3"
if start:
self.template = "" # Template to render
self._events = Queue()
self.__event_queue = Queue()
self.__state = 1
self.__rendered_data = None
self.__pairs = {}
Expand Down Expand Up @@ -48,9 +49,9 @@ def __start(self):
self.screen.refresh()

def __event_loop(self):
self._get_events()
if not self._events.empty():
event = self._events.get()
self.__get_events()
if not self.__event_queue.empty():
event = self.__event_queue.get()
msg("Event", 0, "PluginBase", event, level=4, slevel="events")
if "LOADWEBVIEW" in event:
self.__data.send(self.get_rendered_data())
Expand All @@ -62,9 +63,9 @@ def __event_loop(self):
else:
self._event_loop(event)

def _get_events(self):
def __get_events(self):
while not self.__events.empty():
self._events.put(self.__events.get())
self.__event_queue.put(self.__events.get())

def inc_state(self):
self.__state += 1
Expand Down
2 changes: 1 addition & 1 deletion GLM/source/plugins/template_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
class Plugin(PluginBase):
def __init__(self, start, *args):
super().__init__(start, *args)
self.version = "0.9.0"

def _make_layout(self):
self.template = """\
Expand All @@ -12,7 +13,6 @@ def _make_layout(self):
{{submit;button;Button}}
"""
self.text = Text("infected")
self.screen.fps = 5
self.screen.add(self.text, refresh=False)
self.register('matrix_text', self.text.edit)

Expand Down
Empty file.
20 changes: 20 additions & 0 deletions GLM/source/specialplugins/failsafe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from ..libs.pluginbase import PluginBase
from ..libs.text import Text

class Plugin(PluginBase):
def __init__(self, start, *args):
super().__init__(start, *args)
self.version = "0.9.0"

def _make_layout(self):
self.template = """\
{% <h1 style='color:white;'>Error:</h1> %}
{{ error;label;Failed to load plugin }}"""
self.error = Text('$> error !')
self.screen.add(self.error, refresh=False, x=2, y=5)

def _start(self):
pass

def __event_loop(self):
pass

0 comments on commit ab029df

Please sign in to comment.