From 378c4d9bba14e92d4978a34f9630a1e66385a788 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Fri, 23 Feb 2018 23:26:21 +0000 Subject: [PATCH] Implement loading new modules at runtime --- commands/loadmod.py | 23 +++++++++++++++++++++++ conf/help.json | 5 +++-- core/parser.py | 2 +- utils/loaders/command_loader.py | 10 +++++----- utils/loaders/single_loader.py | 17 +++++++++++++++++ 5 files changed, 49 insertions(+), 8 deletions(-) create mode 100644 commands/loadmod.py create mode 100644 utils/loaders/single_loader.py diff --git a/commands/loadmod.py b/commands/loadmod.py new file mode 100644 index 0000000..9fb3697 --- /dev/null +++ b/commands/loadmod.py @@ -0,0 +1,23 @@ +from core.main import * +from utils.loaders.single_loader import loadSingle + +class Loadmod: + def __init__(self, register): + register("loadmod", self.loadmod) + + def loadmod(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): + if authed: + if length == 2: + rtrn = loadSingle(spl[1], register) + if rtrn == True: + success("Loaded module %s" % spl[1]) + return + else: + failure("Error loading module %s: %s" % (spl[1], rtrn)) + return + else: + incUsage("loadmod") + return + else: + incUsage(None) + return diff --git a/conf/help.json b/conf/help.json index b505b6c..dba2f80 100644 --- a/conf/help.json +++ b/conf/help.json @@ -15,5 +15,6 @@ "stats": "stats []", "save": "save ", "load": "load ", - "dist": "dist" -} \ No newline at end of file + "dist": "dist", + "loadmod": "loadmod " +} diff --git a/core/parser.py b/core/parser.py index 2845829..ecfd18a 100644 --- a/core/parser.py +++ b/core/parser.py @@ -23,7 +23,7 @@ def parseCommand(addr, authed, data): failure("No text was sent") return for i in CommandMap.keys(): - if data.startswith(i): + if spl[0] == i: CommandMap[i](addr, authed, data, obj, spl, success, failure, info, incUsage, length) return incUsage(None) diff --git a/utils/loaders/command_loader.py b/utils/loaders/command_loader.py index ce65a9c..61eb93e 100644 --- a/utils/loaders/command_loader.py +++ b/utils/loaders/command_loader.py @@ -8,8 +8,8 @@ def loadCommands(func): if filename.endswith('.py') and filename != "__init__.py": commandName = filename[0:-3] className = commandName.capitalize() - #try: - __import__('commands.%s' % commandName) - eval('commands.%s.%s(func)' % (commandName, className)) - #except Exception as err: - # error("Exception while loading command %s:\n%s" % (commandName, err)) + try: + __import__('commands.%s' % commandName) + eval('commands.%s.%s(func)' % (commandName, className)) + except Exception as err: + error("Exception while loading command %s:\n%s" % (commandName, err)) diff --git a/utils/loaders/single_loader.py b/utils/loaders/single_loader.py new file mode 100644 index 0000000..11de8ef --- /dev/null +++ b/utils/loaders/single_loader.py @@ -0,0 +1,17 @@ +from os import listdir +from core.main import * +from utils.logging.log import * +import commands + +def loadSingle(command, func): + if command+".py" in listdir("commands"): + try: + if command in CommandMap.keys(): + return "Cannot reload modules" + else: + className = command.capitalize() + __import__("commands.%s" % command) + eval("commands.%s.%s(func)" % (command, className)) + return True + except Exception as err: + return err