From d241ceb787cb1f3379e6a6e2a0ceaf954bb67dc2 Mon Sep 17 00:00:00 2001 From: deathbybandaid Date: Wed, 5 Jun 2019 10:04:07 -0400 Subject: [PATCH] update setup procedures for configuration --- sopel_modules/SpiceBot/AI.py | 8 +- sopel_modules/SpiceBot/Channels.py | 9 +-- sopel_modules/SpiceBot/Commands.py | 13 ++++ sopel_modules/SpiceBot/Config.py | 17 ++--- sopel_modules/SpiceBot/Logs.py | 16 +++- sopel_modules/SpiceBot/setup.py | 116 ++++++++++++++++++++--------- 6 files changed, 122 insertions(+), 57 deletions(-) diff --git a/sopel_modules/SpiceBot/AI.py b/sopel_modules/SpiceBot/AI.py index 2201ee3..3485414 100644 --- a/sopel_modules/SpiceBot/AI.py +++ b/sopel_modules/SpiceBot/AI.py @@ -23,9 +23,7 @@ class SpiceBot_AI_MainSection(StaticSection): class SpiceBot_AI(): def __init__(self): - - botconfig.define_section("SpiceBot_AI", SpiceBot_AI_MainSection, validate=False) - + self.setup_ai() self.braindirs = [] self.dict = { "counts": 0, @@ -52,6 +50,10 @@ class SpiceBot_AI(): # Load bot values self.load_bot_values() + def setup_ai(self): + botconfig.define_section("SpiceBot_AI", SpiceBot_AI_MainSection, validate=False) + botconfig.aibrain = os.path.join(botconfig.homedir, botconfig.basename + '.aibrain.brn') + def load_saved_brain(self): if os.path.isfile(botconfig.config.aibrain): self.aiml_kernel.bootstrap(brainFile=botconfig.config.aibrain) diff --git a/sopel_modules/SpiceBot/Channels.py b/sopel_modules/SpiceBot/Channels.py index bf35781..79eeda4 100644 --- a/sopel_modules/SpiceBot/Channels.py +++ b/sopel_modules/SpiceBot/Channels.py @@ -10,7 +10,6 @@ import re import threading from .Config import config as botconfig -from .Logs import logs class SpiceBot_Channels_MainSection(StaticSection): @@ -23,10 +22,7 @@ class SpiceBot_Channels_MainSection(StaticSection): class BotChannels(): """This Logs all channels known to the server""" def __init__(self): - - # SpiceBot - logs.log('SpiceBot_Channels', "Setting Up BotChannels class") - botconfig.define_section("SpiceBot_Channels", SpiceBot_Channels_MainSection, validate=False) + self.setup_channels() self.lock = threading.Lock() self.channel_lock = False @@ -35,6 +31,9 @@ class BotChannels(): "InitialProcess": False } + def setup_channels(self): + botconfig.define_section("SpiceBot_Channels", SpiceBot_Channels_MainSection, validate=False) + def channel_list_request(self, bot): bot.write(['LIST']) diff --git a/sopel_modules/SpiceBot/Commands.py b/sopel_modules/SpiceBot/Commands.py index df42c1f..e495b21 100644 --- a/sopel_modules/SpiceBot/Commands.py +++ b/sopel_modules/SpiceBot/Commands.py @@ -5,7 +5,10 @@ This is the SpiceBot Commands system. This Class stores commands in an easy to access manner """ + import sopel +from sopel.config.types import StaticSection, ValidatedAttribute + import os import threading @@ -16,9 +19,14 @@ from .Config import config as botconfig from .Database import db as botdb +class SpiceBot_Commands_MainSection(StaticSection): + query_prefix = ValidatedAttribute('query_prefix', default="?") + + class BotCommands(): """This Logs all commands known to the bot""" def __init__(self): + self.setup_commands() self.lock = threading.Lock() self.dict = { "counts": 0, @@ -36,6 +44,11 @@ class BotCommands(): for comtype in ['module', 'nickname', 'rule']: logs.log('SpiceBot_Commands', "Found " + str(len(self.dict['commands'][comtype].keys())) + " " + comtype + " commands.", True) + def setup_commands(self): + botconfig.define_section("SpiceBot_Commands", SpiceBot_Commands_MainSection, validate=False) + botconfig.core.query_list = str(botconfig.SpiceBot_Commands.query_prefix).replace("\\", '').split("|") + botconfig.core.prefix_list.extend(botconfig.SpiceBot_Commands.query_prefix) + def nickrules(self): for command in self.dict['commands']['rule'].keys(): if command.startswith("$nickname"): diff --git a/sopel_modules/SpiceBot/Config.py b/sopel_modules/SpiceBot/Config.py index 4d535d8..983ea1f 100644 --- a/sopel_modules/SpiceBot/Config.py +++ b/sopel_modules/SpiceBot/Config.py @@ -24,18 +24,7 @@ class BotConfig(): opts = parser.parse_args(argv) self.config = get_configuration(opts) - self.config.core.prefix_list = str(self.config.core.prefix).replace("\\", '').split("|") - self.config.core.prefix_list.append("?") - - self.config.basename = os.path.basename(self.config.filename).rsplit('.', 1)[0] - self.config.core.logs_stdio = os.path.join(self.config.core.logdir, 'stdio.log') - # self.config.core.logs_stdio = os.path.os.path.join(self.config.core.logdir, self.config.basename + '.stdio.log') - self.config.core.logs_exceptions = os.path.join(self.config.core.logdir, 'exceptions.log') - # self.config.core.logs_exceptions = os.path.os.path.join(self.config.core.logdir, self.config.basename + '.exceptions.log') - self.config.core.logs_raw = os.path.join(self.config.core.logdir, 'raw.log') - # self.config.core.logs_raw = os.path.os.path.join(self.config.core.logdir, self.config.basename + '.raw.log') - - self.config.aibrain = os.path.join(self.config.homedir, self.config.basename + '.aibrain.brn') + self.setup_config() # load as dict config = configparser.ConfigParser() @@ -47,6 +36,10 @@ class BotConfig(): if each_key.lower() not in self.dict[each_section.lower()].keys(): self.dict[each_section.lower()][each_key.lower()] = each_val + def setup_config(self): + self.config.core.basename = os.path.basename(self.config.filename).rsplit('.', 1)[0] + self.config.core.prefix_list = str(self.config.core.prefix).replace("\\", '').split("|") + def define_section(self, name, cls_, validate=True): return self.config.define_section(name, cls_, validate) diff --git a/sopel_modules/SpiceBot/Logs.py b/sopel_modules/SpiceBot/Logs.py index 4e422fd..1226d98 100644 --- a/sopel_modules/SpiceBot/Logs.py +++ b/sopel_modules/SpiceBot/Logs.py @@ -6,9 +6,9 @@ This is the SpiceBot Logs system. This Class stores logs in an easy to access manner """ -import sopel import threading import os +import sys import spicemanip from .Config import config as botconfig @@ -19,12 +19,24 @@ from .Config import config as botconfig class BotLogs(): """This Is a contained source of log information""" def __init__(self): + self.setup_logs() self.lock = threading.Lock() self.dict = { "list": {"Sopel_systemd": [], "Sopel_stdio": []}, "queue": [] } + def setup_logs(self): + botconfig.core.logs_stdio = os.path.join(botconfig.core.logdir, 'stdio.log') + # botconfig.core.logs_stdio = os.path.os.path.join(botconfig.core.logdir, botconfig.basename + '.stdio.log') + botconfig.core.logs_exceptions = os.path.join(botconfig.core.logdir, 'exceptions.log') + # botconfig.core.logs_exceptions = os.path.os.path.join(botconfig.core.logdir, botconfig.basename + '.exceptions.log') + botconfig.core.logs_raw = os.path.join(botconfig.core.logdir, 'raw.log') + # botconfig.core.logs_raw = os.path.os.path.join(botconfig.core.logdir, botconfig.basename + '.raw.log') + + def botstderr(self, logmessage): + sys.stderr.write(logmessage) + def log(self, logtype, logentry, stdio=False): self.lock.acquire() @@ -36,7 +48,7 @@ class BotLogs(): self.dict["queue"].append(logmessage) if stdio: - sopel.tools.stderr(logmessage) + self.botstderr(logmessage) if logtype not in self.dict["list"].keys(): self.dict["list"][logtype] = [] diff --git a/sopel_modules/SpiceBot/setup.py b/sopel_modules/SpiceBot/setup.py index e49425a..72da720 100644 --- a/sopel_modules/SpiceBot/setup.py +++ b/sopel_modules/SpiceBot/setup.py @@ -12,6 +12,7 @@ from .Logs import logs from .Events import events from .Channels import SpiceBot_Channels_MainSection +from .Commands import SpiceBot_Commands_MainSection from .Update import SpiceBot_Update_MainSection from .osd import SopelWrapperOSD, ToolsOSD, SopelOSD, SpiceBot_OSD from .Kick import SopelWrapperKICK, SopelKICK, SpiceBot_Kick @@ -20,10 +21,88 @@ from .AI import SpiceBot_AI_MainSection def setup(bot): + # basic config settings + setup_config(bot) + + # Logs + setup_logs(bot) + + # Commands + setup_commands(bot) + + # OSD + setup_osd(bot) + + # kick + setup_kick(bot) + + # update + setup_update(bot) + + # channels + setup_channels(bot) + + # AI + setup_ai(bot) + + # startupmonologue + setup_startupmonologue(bot) + + +def setup_config(bot): + logs.log('SpiceBot_Config', "Setting Up Configuration") + bot.config.core.basename = os.path.basename(bot.config.filename).rsplit('.', 1)[0] + bot.config.core.prefix_list = str(bot.config.core.prefix).replace("\\", '').split("|") + + +def setup_logs(bot): logs.log('SpiceBot_Logs', "Starting Setup Procedure") events.startup_add([events.BOT_LOGS]) + bot.config.core.logs_stdio = os.path.join(bot.config.core.logdir, 'stdio.log') + # bot.config.core.logs_stdio = os.path.os.path.join(bot.config.core.logdir, bot.config.basename + '.stdio.log') + bot.config.core.logs_exceptions = os.path.join(bot.config.core.logdir, 'exceptions.log') + # bot.config.core.logs_exceptions = os.path.os.path.join(bot.config.core.logdir, bot.config.basename + '.exceptions.log') + bot.config.core.logs_raw = os.path.join(bot.config.core.logdir, 'raw.log') + # bot.config.core.logs_raw = os.path.os.path.join(bot.config.core.logdir, bot.config.basename + '.raw.log') events.trigger(bot, events.BOT_LOGS, "SpiceBot_Logs") + +def setup_commands(bot): + logs.log('SpiceBot_Commands', "Setting Up Configuration") + events.startup_add([events.BOT_COMMANDS]) + bot.config.define_section("SpiceBot_Commands", SpiceBot_Commands_MainSection, validate=False) + bot.config.core.query_list = str(bot.config.SpiceBot_Commands.query_prefix).replace("\\", '').split("|") + bot.config.core.prefix_list.extend(bot.config.SpiceBot_Commands.query_prefix) + + +def setup_startupmonologue(bot): + logs.log('SpiceBot_StartupMonologue', "Initial Setup processing") + events.startup_add([ + events.BOT_STARTUPMONOLOGUE_CONNECTED, + events.BOT_STARTUPMONOLOGUE_CHANNELS, + events.BOT_STARTUPMONOLOGUE_COMMANDS + ]) + + +def setup_channels(bot): + logs.log('SpiceBot_Channels', "Setting Up Configuration") + events.startup_add([events.BOT_CHANNELS]) + bot.config.define_section("SpiceBot_Channels", SpiceBot_Channels_MainSection, validate=False) + + +def setup_update(bot): + logs.log('SpiceBot_Update', "Initial Setup processing") + bot.config.define_section("SpiceBot_Update", SpiceBot_Update_MainSection, validate=False) + botconfig.define_section("SpiceBot_Update", SpiceBot_Update_MainSection, validate=False) + + +def setup_ai(bot): + logs.log('SpiceBot_AI', "Setting Up Configuration") + bot.config.define_section("SpiceBot_AI", SpiceBot_AI_MainSection, validate=False) + bot.config.aibrain = os.path.join(bot.config.homedir, bot.config.basename + '.aibrain.brn') + + +def setup_osd(bot): # Inject OSD logs.log('SpiceBot_OSD', "Implanting OSD function into bot") bot.osd = SopelOSD.osd @@ -37,6 +116,8 @@ def setup(bot): bot.config.define_section("SpiceBot_OSD", SpiceBot_OSD, validate=False) botconfig.define_section("SpiceBot_OSD", SpiceBot_OSD, validate=False) + +def setup_kick(bot): # Inject KICK logs.log('SpiceBot_Kick', "Implanting Kick function into bot") bot.kick = SopelKICK.kick @@ -46,38 +127,3 @@ def setup(bot): logs.log('SpiceBot_Kick', "Checking for config settings") bot.config.define_section("SpiceBot_Kick", SpiceBot_Kick, validate=False) botconfig.define_section("SpiceBot_Kick", SpiceBot_Kick, validate=False) - - bot.config.core.prefix_list = str(bot.config.core.prefix).replace("\\", '').split("|") - bot.config.core.prefix_list.append("?") - - logs.log('SpiceBot_Config', "Setting Up Configuration") - bot.config.core.basename = os.path.basename(bot.config.filename).rsplit('.', 1)[0] - bot.config.core.logs_stdio = os.path.join(bot.config.core.logdir, 'stdio.log') - # bot.config.core.logs_stdio = os.path.os.path.join(bot.config.core.logdir, bot.config.basename + '.stdio.log') - bot.config.core.logs_exceptions = os.path.join(bot.config.core.logdir, 'exceptions.log') - # bot.config.core.logs_exceptions = os.path.os.path.join(bot.config.core.logdir, bot.config.basename + '.exceptions.log') - bot.config.core.logs_raw = os.path.join(bot.config.core.logdir, 'raw.log') - # bot.config.core.logs_raw = os.path.os.path.join(bot.config.core.logdir, bot.config.basename + '.raw.log') - - bot.config.aibrain = os.path.join(bot.config.homedir, bot.config.basename + '.aibrain.brn') - - logs.log('SpiceBot_Channels', "Setting Up Configuration") - events.startup_add([events.BOT_CHANNELS]) - bot.config.define_section("SpiceBot_Channels", SpiceBot_Channels_MainSection, validate=False) - - logs.log('SpiceBot_Commands', "Setting Up Configuration") - events.startup_add([events.BOT_COMMANDS]) - - logs.log('SpiceBot_Update', "Initial Setup processing") - bot.config.define_section("SpiceBot_Update", SpiceBot_Update_MainSection, validate=False) - botconfig.define_section("SpiceBot_Update", SpiceBot_Update_MainSection, validate=False) - - logs.log('SpiceBot_StartupMonologue', "Initial Setup processing") - events.startup_add([ - events.BOT_STARTUPMONOLOGUE_CONNECTED, - events.BOT_STARTUPMONOLOGUE_CHANNELS, - events.BOT_STARTUPMONOLOGUE_COMMANDS - ]) - - logs.log('SpiceBot_AI', "Setting Up Configuration") - bot.config.define_section("SpiceBot_AI", SpiceBot_AI_MainSection, validate=False)