From f1cbe5edba78fd8ddb5d877305d4de5f113c0195 Mon Sep 17 00:00:00 2001 From: deathbybandaid Date: Thu, 20 Jun 2019 13:33:16 -0400 Subject: [PATCH] ready for live testing maybe --- sopel_modules/SpiceBot/Commands.py | 34 ++++++------ sopel_modules/SpiceBot/DictComs.py | 38 +------------- sopel_modules/SpiceBot/Read.py | 83 ++++++++++++++++++++++++++++++ sopel_modules/SpiceBot/__init__.py | 2 +- 4 files changed, 103 insertions(+), 54 deletions(-) diff --git a/sopel_modules/SpiceBot/Commands.py b/sopel_modules/SpiceBot/Commands.py index 5194f62..092f526 100644 --- a/sopel_modules/SpiceBot/Commands.py +++ b/sopel_modules/SpiceBot/Commands.py @@ -13,10 +13,12 @@ import os import threading import spicemanip +import sopel_modules from .Logs import logs from .Config import config as botconfig from .Database import db as botdb +from .Read import read as botread class SpiceBot_Commands_MainSection(StaticSection): @@ -29,7 +31,6 @@ class BotCommands(): self.setup_commands() self.lock = threading.Lock() self.dict = { - "counts": 0, 'nickrules': [], 'nickaiml': [], "commands": { @@ -145,14 +146,10 @@ class BotCommands(): filepathlisting.append(home_modules_dir) # pypi installed - try: - import sopel_modules - for plugin_dir in set(sopel_modules.__path__): - for pathname in os.listdir(plugin_dir): - pypi_modules_dir = os.path.join(plugin_dir, pathname) - filepathlisting.append(pypi_modules_dir) - except Exception as e: - logs.log('SpiceBOT_COMMANDS', "sopel_modules not loaded :" + str(e)) + for plugin_dir in set(sopel_modules.__path__): + for pathname in os.listdir(plugin_dir): + pypi_modules_dir = os.path.join(plugin_dir, pathname) + filepathlisting.append(pypi_modules_dir) # Extra directories for directory in botconfig.extra: @@ -198,6 +195,15 @@ class BotCommands(): folderpath = str(modulefile).split("/" + filename)[0] foldername = str(folderpath).split("/")[-1] + # check for json reference file + validcomdict = botread.module_json_to_dict(str(modulefile)) + + # replace json defaults + validcomdict["filepath"] = str(modulefile) + validcomdict["filename"] = str(filename_base) + validcomdict["folderpath"] = str(folderpath) + validcomdict["foldername"] = str(foldername) + detected_lines = [] for line in module_file_lines: @@ -249,14 +255,8 @@ class BotCommands(): validcoms.remove(regexcom) if len(validcoms): - validcomdict = { - "comtype": comtype, - "validcoms": validcoms, - "filepath": str(modulefile), - "filename": str(filename_base), - "folderpath": str(folderpath), - "foldername": str(foldername), - } + validcomdict["comtype"] = comtype + validcomdict["validcoms"] = validcoms filelinelist.append(validcomdict) currentsuccesslines += 1 except Exception as e: diff --git a/sopel_modules/SpiceBot/DictComs.py b/sopel_modules/SpiceBot/DictComs.py index 098c20f..833010d 100644 --- a/sopel_modules/SpiceBot/DictComs.py +++ b/sopel_modules/SpiceBot/DictComs.py @@ -63,47 +63,13 @@ class BotJSONCommands(): dict_from_file[var] = tuple_contents_bak # check that type is set, use cases will inherit this if not set - if "type" not in list(dict_from_file.keys()): - dict_from_file["type"] = dict_from_file["foldername"] - if dict_from_file["type"] not in self.valid_com_types: + if "type" not in list(dict_from_file.keys()) or dict_from_file["type"] not in self.valid_com_types: dict_from_file["type"] = 'simple' # Don't process these. keysprocessed = [] keysprocessed.extend(["validcoms", "filepath", "filename"]) - - # the command must have an author - if "author" not in list(dict_from_file.keys()): - dict_from_file["author"] = "deathbybandaid" - keysprocessed.append("author") - - # the command must have a contributors list - if "contributors" not in list(dict_from_file.keys()): - dict_from_file["contributors"] = [] - if not isinstance(dict_from_file["contributors"], list): - dict_from_file["contributors"] = [dict_from_file["contributors"]] - if "deathbybandaid" not in dict_from_file["contributors"]: - dict_from_file["contributors"].append("deathbybandaid") - if dict_from_file["author"] not in dict_from_file["contributors"]: - dict_from_file["contributors"].append(dict_from_file["author"]) - keysprocessed.append("contributors") - - if "hardcoded_channel_block" not in list(dict_from_file.keys()): - dict_from_file["hardcoded_channel_block"] = [] - keysprocessed.append("hardcoded_channel_block") - - if "example" not in list(dict_from_file.keys()): - dict_from_file["example"] = str(botconfig.core.prefix_list[0] + maincom) - - if "exampleresponse" not in list(dict_from_file.keys()): - dict_from_file["exampleresponse"] = None - - if "description" not in list(dict_from_file.keys()): - dict_from_file["description"] = None - - if "privs" not in list(dict_from_file.keys()): - dict_from_file["privs"] = [] - + keysprocessed.append("author", "contributors") keysprocessed.extend(["validcoms", "filepath", "filename", "description", "exampleresponse", "example", "privs"]) # handle basic required dict handling diff --git a/sopel_modules/SpiceBot/Read.py b/sopel_modules/SpiceBot/Read.py index 95473e3..0aecded 100644 --- a/sopel_modules/SpiceBot/Read.py +++ b/sopel_modules/SpiceBot/Read.py @@ -8,6 +8,7 @@ import os import codecs from .Logs import logs +from .Config import config as botconfig class BotRead(): @@ -84,6 +85,7 @@ class BotRead(): dict_from_file["filename"] = str(filename_base) dict_from_file["folderpath"] = dict_from_file["filepath"].split("/" + dict_from_file["filename"])[0] dict_from_file["foldername"] = str(dict_from_file["folderpath"]).split("/")[-1] + dict_from_file = self.command_defaults(dict_from_file) configs_dict[filename_base] = dict_from_file if filecount: @@ -96,5 +98,86 @@ class BotRead(): return configs_dict + def module_json_to_dict(self, filepath, logging=True): + + # gather file stats + slashsplit = str(filepath).split("/") + filename = slashsplit[-1] + filename_base = os.path.basename(filename).rsplit('.', 1)[0] + folderpath = str(filepath).split("/" + filename)[0] + + jsonpath = os.path.join(folderpath, filename_base + ".json") + if os.path.exists(jsonpath) and os.path.isfile(jsonpath): + + # Read dictionary from file, if not, enable an empty dict + filereadgood = True + inf = codecs.open(filepath, "r", encoding='utf-8') + infread = inf.read() + try: + dict_from_file = eval(infread) + except Exception as e: + filereadgood = False + if logging: + logs.log("SpiceBot_Modules", "Error loading %s: %s (%s)" % ("Module Commands", e, filepath)) + dict_from_file = dict() + # Close File + inf.close() + + # gather file stats + slashsplit = str(filepath).split("/") + filename = slashsplit[-1] + filename_base = os.path.basename(filename).rsplit('.', 1)[0] + + if not filereadgood or not isinstance(dict_from_file, dict): + dict_from_file = {} + else: + dict_from_file["filepath"] = str(filepath) + dict_from_file["filename"] = str(filename_base) + dict_from_file["folderpath"] = dict_from_file["filepath"].split("/" + dict_from_file["filename"])[0] + dict_from_file["foldername"] = str(dict_from_file["folderpath"]).split("/")[-1] + dict_from_file = self.command_defaults(dict_from_file) + else: + dict_from_file = {} + + dict_from_file = self.command_defaults(dict_from_file) + return dict_from_file + + def command_defaults(self, dict_from_file): + + # the command must have an author + if "author" not in list(dict_from_file.keys()): + dict_from_file["author"] = "deathbybandaid" + + # the command must have a contributors list + if "contributors" not in list(dict_from_file.keys()): + dict_from_file["contributors"] = [] + if not isinstance(dict_from_file["contributors"], list): + dict_from_file["contributors"] = [dict_from_file["contributors"]] + if "deathbybandaid" not in dict_from_file["contributors"]: + dict_from_file["contributors"].append("deathbybandaid") + if dict_from_file["author"] not in dict_from_file["contributors"]: + dict_from_file["contributors"].append(dict_from_file["author"]) + + if "example" not in list(dict_from_file.keys()): + dict_from_file["example"] = "$maincom" + # TODO + #if dict_from_file["comtype"] == "nickname": + # dict_from_file["example"] = str(botconfig.nick + " $maincom") + # else: + # dict_from_file["example"] = str(botconfig.core.prefix_list[0] + "$maincom") + if not dict_from_file["example"].startswith("$maincom"): + dict_from_file["example"] = "$maincom " + dict_from_file["example"] + + if "exampleresponse" not in list(dict_from_file.keys()): + dict_from_file["exampleresponse"] = None + + if "description" not in list(dict_from_file.keys()): + dict_from_file["description"] = None + + if "privs" not in list(dict_from_file.keys()): + dict_from_file["privs"] = [] + + return dict_from_file + read = BotRead() diff --git a/sopel_modules/SpiceBot/__init__.py b/sopel_modules/SpiceBot/__init__.py index f051b6c..31db45c 100644 --- a/sopel_modules/SpiceBot/__init__.py +++ b/sopel_modules/SpiceBot/__init__.py @@ -26,7 +26,7 @@ from .Google import * from .Gif import * from .Sherlock import * from .Reddit import * -# from .DictComs import * +from .DictComs import * from .Read import *