diff --git a/opt/ahenk/base/plugin/Plugin.py b/opt/ahenk/base/plugin/Plugin.py index ecf3742..52a5f64 100644 --- a/opt/ahenk/base/plugin/Plugin.py +++ b/opt/ahenk/base/plugin/Plugin.py @@ -24,6 +24,9 @@ class Context(object): def get(self, var_name): return self.data[var_name] + def get_username(self): + return self.data['username'] + def empty_data(self): self.data = {} @@ -151,17 +154,26 @@ class Plugin(threading.Thread): elif obj_name == "KILL_SIGNAL": self.keep_run = False self.logger.debug('[Plugin] Killing queue ! Plugin Name: {}'.format(str(self.name))) - elif obj_name == "SAFE_MODE": - username = item_obj.username - safe_mode_module = Scope.getInstance().getPluginManager().find_safe_mode_module(self.name) - safe_mode_module.handle_safe_mode(username, self.context) + elif 'MODE' in obj_name: + module = Scope.getInstance().getPluginManager().find_module(obj_name, self.name) + if module is not None: + if item_obj.obj_name in ('LOGIN_MODE', 'LOGOUT_MODE', 'SAFE_MODE'): + self.context.put('username', item_obj.username) + try: + self.logger.debug('[Plugin] {0} is running on {1} plugin'.format(str(item_obj.obj_name), str(self.name))) + module.handle_mode(self.context) + except Exception as e: + self.logger.error('[Plugin] A problem occurred while running {0} on {1} plugin. Error Message: {2}'.format(str(obj_name), str(self.name), str(e))) + + if item_obj.obj_name is 'SHUTDOWN_MODE': + self.logger.debug('[Plugin] {0} plugin is stopping...'.format(str(self.name))) + self.keep_run = False else: self.logger.warning("[Plugin] Not supported object type: {}".format(str(obj_name))) - # Empty context for next use self.context.empty_data() except Exception as e: - self.logger.error("[Plugin] Plugin running exception. Exception Message: {} ".format(str(e))) + self.logger.error("[Plugin] Plugin running exception about {0}. Exception Message: {1} ".format(obj_name, str(e))) def get_execution_id(self, profile_id): try: diff --git a/opt/ahenk/base/plugin/plugin_manager.py b/opt/ahenk/base/plugin/plugin_manager.py index 9bffe83..ad8ddc5 100644 --- a/opt/ahenk/base/plugin/plugin_manager.py +++ b/opt/ahenk/base/plugin/plugin_manager.py @@ -6,11 +6,15 @@ import imp import os from base.Scope import Scope +from base.model.PluginBean import PluginBean +from base.model.PluginKillSignal import PluginKillSignal +from base.model.modes.init_mode import InitMode +from base.model.modes.login_mode import LoginMode +from base.model.modes.logout_mode import LogoutMode +from base.model.modes.safe_mode import SafeMode +from base.model.modes.shutdown_mode import ShutdownMode from base.plugin.Plugin import Plugin from base.plugin.PluginQueue import PluginQueue -from base.model.PluginKillSignal import PluginKillSignal -from base.model.PluginBean import PluginBean -from base.model.safe_mode import SafeMode # TODO create base abstract class @@ -173,22 +177,39 @@ class PluginManager(object): else: return True - def find_safe_mode_module(self, plugin_name): + def process_mode(self, mode_type, username=None): + mode = None + if mode_type == 'init': + mode = InitMode() + elif mode_type == 'shutdown': + mode = ShutdownMode() + elif mode_type == 'login': + mode = LoginMode(username) + elif mode_type == 'logout': + mode = LogoutMode(username) + elif mode_type == 'safe': + mode = SafeMode(username) + else: + self.logger.error('[PluginManager] Unknown mode type: {}'.format(mode_type)) + + if mode is not None: + for plugin_name in self.pluginQueueDict: + try: + self.pluginQueueDict[plugin_name].put(mode, 1) + except Exception as e: + self.logger.error('[PluginManager] Exception occurred while switching safe mode. Error Message : {}'.format(str(e))) + + def find_module(self, mode, plugin_name): + mode = mode.lower().replace('_mode', '') location = os.path.join(self.configManager.get("PLUGIN", "pluginFolderPath"), plugin_name) - if os.path.isdir(location) and "safe.py" in os.listdir(location): - info = imp.find_module("safe", [location]) - return imp.load_module("safe", *info) + + if os.path.isdir(location) and (mode + ".py") in os.listdir(location): + info = imp.find_module(mode, [location]) + return imp.load_module(mode, *info) else: self.logger.warning('[PluginManager] safe.py not found Plugin Name : ' + str(plugin_name)) return None - def process_safe_mode(self, username): - for plugin_name in self.pluginQueueDict: - try: - self.pluginQueueDict[plugin_name].put(SafeMode(username), 1) - except Exception as e: - self.logger.error('Exception occured when switching safe mode ' + str(e)) - def reloadSinglePlugin(self, pluginName): # Not implemented yet