From 152f059b4610ccd46e41a987b6ef806e3ee9a832 Mon Sep 17 00:00:00 2001 From: deathbybandaid Date: Sun, 5 May 2019 22:21:03 -0400 Subject: [PATCH] hopefully last test of event system --- .../SpiceBot_CommandsQuery/__init__.py | 2 +- sopel_modules/SpiceBot_Docs/Docs.py | 4 +- sopel_modules/SpiceBot_GifSearch/gif.py | 3 +- sopel_modules/SpiceBot_GifSearch/gifapi.py | 3 +- sopel_modules/SpiceBot_GifSearch/gifsearch.py | 42 ++++-------- sopel_modules/SpiceBot_Logs/Logs.py | 59 +++++++---------- sopel_modules/SpiceBot_Logs/Logs_Command.py | 38 +++++++++++ sopel_modules/SpiceBot_Logs/__init__.py | 1 + sopel_modules/SpiceBot_SBTools/__init__.py | 19 +++--- .../StartupMonologue.py | 64 ++++++++++--------- 10 files changed, 121 insertions(+), 114 deletions(-) create mode 100644 sopel_modules/SpiceBot_Logs/Logs_Command.py diff --git a/sopel_modules/SpiceBot_CommandsQuery/__init__.py b/sopel_modules/SpiceBot_CommandsQuery/__init__.py index c439020..55dadea 100644 --- a/sopel_modules/SpiceBot_CommandsQuery/__init__.py +++ b/sopel_modules/SpiceBot_CommandsQuery/__init__.py @@ -6,7 +6,7 @@ Sopel Commands Query responds to ? to display commands matching patterns from __future__ import unicode_literals, absolute_import, division, print_function from .CommandsQuery import * -from . QueryCommand import * +from .QueryCommand import * __author__ = 'Sam Zick' __email__ = 'sam@deathbybandaid.net' diff --git a/sopel_modules/SpiceBot_Docs/Docs.py b/sopel_modules/SpiceBot_Docs/Docs.py index e1daf4a..c8e496a 100644 --- a/sopel_modules/SpiceBot_Docs/Docs.py +++ b/sopel_modules/SpiceBot_Docs/Docs.py @@ -4,9 +4,7 @@ from __future__ import unicode_literals, absolute_import, division, print_functi import sopel.module -import spicemanip - -from sopel_modules.SpiceBot_SBTools import sopel_triggerargs, bot_privs, github_dict +from sopel_modules.SpiceBot_SBTools import github_dict @sopel.module.nickname_commands('help', 'docs', 'wiki') diff --git a/sopel_modules/SpiceBot_GifSearch/gif.py b/sopel_modules/SpiceBot_GifSearch/gif.py index a90f2d4..9a93975 100644 --- a/sopel_modules/SpiceBot_GifSearch/gif.py +++ b/sopel_modules/SpiceBot_GifSearch/gif.py @@ -7,6 +7,7 @@ from sopel import module from .gifsearch import getGif from sopel_modules.SpiceBot_SBTools import sopel_triggerargs +from sopel_modules.SpiceBot_Events.System import bot_events_check import spicemanip @@ -14,7 +15,7 @@ import spicemanip @module.commands('gif') def gif_trigger(bot, trigger): - while "SpiceBot_GifSearch" not in bot.memory: + while not bot_events_check(bot, '2003'): pass triggerargs, triggercommand = sopel_triggerargs(bot, trigger) diff --git a/sopel_modules/SpiceBot_GifSearch/gifapi.py b/sopel_modules/SpiceBot_GifSearch/gifapi.py index 9442793..6748cae 100644 --- a/sopel_modules/SpiceBot_GifSearch/gifapi.py +++ b/sopel_modules/SpiceBot_GifSearch/gifapi.py @@ -9,12 +9,13 @@ import sopel.module import spicemanip from sopel_modules.SpiceBot_SBTools import sopel_triggerargs +from sopel_modules.SpiceBot_Events.System import bot_events_check @sopel.module.commands('(.*)') def gifapi_triggers(bot, trigger): - while "SpiceBot_GifSearch" not in bot.memory: + while not bot_events_check(bot, '2003'): pass triggerargs, triggercommand = sopel_triggerargs(bot, trigger, 'prefix_command') diff --git a/sopel_modules/SpiceBot_GifSearch/gifsearch.py b/sopel_modules/SpiceBot_GifSearch/gifsearch.py index 52ff480..a75b087 100644 --- a/sopel_modules/SpiceBot_GifSearch/gifsearch.py +++ b/sopel_modules/SpiceBot_GifSearch/gifsearch.py @@ -6,10 +6,11 @@ import sopel.module from sopel.config.types import StaticSection, ValidatedAttribute from sopel_modules.SpiceBot_LoadOrder.LoadOrder import set_bot_event, startup_bot_event +from sopel_modules.SpiceBot_Events.System import bot_events_startup_register, bot_events_recieved, bot_events_trigger from sopel_modules.SpiceBot_CommandsQuery.CommandsQuery import commandsquery_register -from sopel_modules.SpiceBot_SBTools import read_directory_json_to_dict, sopel_triggerargs, bot_logging +from sopel_modules.SpiceBot_SBTools import read_directory_json_to_dict, bot_logging import spicemanip @@ -19,7 +20,6 @@ import requests import json from fake_useragent import UserAgent import random -import threading # user agent and header ua = UserAgent() @@ -51,18 +51,8 @@ def configure(config): def setup(bot): - bot_logging(bot, 'SpiceBot_GifSearch', "Starting Setup Procedure") - - threading.Thread(target=setup_thread, args=(bot,)).start() - - -def shutdown(bot): - if "SpiceBot_GifSearch" in bot.memory: - del bot.memory["SpiceBot_GifSearch"] - - -def setup_thread(bot): + bot_events_startup_register(bot, ['2003']) startup_bot_event(bot, "SpiceBot_GifSearch") @@ -94,24 +84,12 @@ def setup_thread(bot): commandsquery_register(bot, "prefix", prefixcommand) set_bot_event(bot, "SpiceBot_GifSearch") + bot_events_trigger(bot, 2003, "SpiceBot_GifSearch") -@sopel.module.commands('gif') -def gif_trigger(bot, trigger): - triggerargs, triggercommand = sopel_triggerargs(bot, trigger) - if triggerargs == []: - return bot.osd("Please present a query to search.") - - query = spicemanip.main(trigger.args[1], 0) - searchapis = bot.memory["SpiceBot_GifSearch"]['valid_gif_api_dict'].keys() - searchdict = {"query": query, "gifsearch": searchapis} - - gifdict = getGif(bot, searchdict) - - if gifdict["error"]: - bot.osd(gifdict["error"]) - else: - bot.osd(str(gifdict['gifapi'].title() + " Result (" + str(query) + " #" + str(gifdict["returnnum"]) + "): " + str(gifdict["returnurl"]))) +def shutdown(bot): + if "SpiceBot_GifSearch" in bot.memory: + del bot.memory["SpiceBot_GifSearch"] def getGif(bot, searchdict): @@ -273,3 +251,9 @@ def getGif(bot, searchdict): # return dict gifdict['error'] = None return gifdict + + +@sopel.module.event('2003') +@sopel.module.rule('.*') +def bot_events_setup(bot, trigger): + bot_events_recieved(bot, trigger.event) diff --git a/sopel_modules/SpiceBot_Logs/Logs.py b/sopel_modules/SpiceBot_Logs/Logs.py index 85593d5..93310d2 100644 --- a/sopel_modules/SpiceBot_Logs/Logs.py +++ b/sopel_modules/SpiceBot_Logs/Logs.py @@ -3,12 +3,13 @@ from __future__ import unicode_literals, absolute_import, division, print_function import sopel.module -from sopel.tools import stderr from sopel.config.types import StaticSection, ValidatedAttribute import spicemanip from sopel_modules.SpiceBot_SBTools import sopel_triggerargs, command_permissions_check, inlist, inlist_match, bot_logging +from sopel_modules.SpiceBot_Events.System import bot_events_startup_register, bot_events_recieved, bot_events_trigger +from sopel_modules.SpiceBot_LoadOrder.LoadOrder import set_bot_event, startup_bot_event def configure(config): @@ -23,21 +24,24 @@ class SpiceBot_Logs_MainSection(StaticSection): def setup(bot): bot_logging(bot, 'SpiceBot_Logs', "Starting Setup Procedure") bot.config.define_section("SpiceBot_Logs", SpiceBot_Logs_MainSection, validate=False) - if 'SpiceBot_Logs' not in bot.memory: - bot.memory['SpiceBot_Logs'] = {} - if 'SpiceBot_Logs_queue' not in bot.memory: - bot.memory['SpiceBot_Logs_queue'] = [] + bot_events_startup_register(bot, ['2004']) + + startup_bot_event(bot, "SpiceBot_CommandsQuery") + + if 'SpiceBot_Logs' not in bot.memory: + bot.memory['SpiceBot_Logs'] = {"logs": {}, "queue": []} + + set_bot_event(bot, "SpiceBot_Logs") + bot_events_trigger(bot, 2004, "SpiceBot_Logs") def shutdown(bot): if "SpiceBot_Logs" in bot.memory: del bot.memory["SpiceBot_Logs"] - if "SpiceBot_Logs_queue" in bot.memory: - del bot.memory["SpiceBot_Logs_queue"] -@sopel.module.event('001') +@sopel.module.event('1004') @sopel.module.rule('.*') def join_log_channel(bot, trigger): if bot.config.SpiceBot_Logs.logging_channel: @@ -47,35 +51,16 @@ def join_log_channel(bot, trigger): if channel not in bot.channels.keys() and bot.config.SpiceBot_Channels.operadmin: bot.write(('SAJOIN', bot.nick, channel)) - if 'SpiceBot_Logs_queue' not in bot.memory: - bot.memory['SpiceBot_Logs_queue'] = [] + if 'SpiceBot_Logs' not in bot.memory: + bot.memory['SpiceBot_Logs'] = {"logs": {}, "queue": []} - while 'SpiceBot_Logs_queue' in bot.memory: - if len(bot.memory['SpiceBot_Logs_queue']): - bot.say(str(bot.memory['SpiceBot_Logs_queue'][0]), channel) - del bot.memory['SpiceBot_Logs_queue'][0] + while 'SpiceBot_Logs' in bot.memory: + if len(bot.memory['SpiceBot_Logs']["queue"]): + bot.say(str(bot.memory['SpiceBot_Logs']["queue"][0]), channel) + del bot.memory['SpiceBot_Logs']["queue"][0] -@sopel.module.nickname_commands('logs') -def bot_command_action(bot, trigger): - - if not command_permissions_check(bot, trigger, ['admins', 'owner']): - bot.say("I was unable to process this Bot Nick command due to privilege issues.") - return - - triggerargs, triggercommand = sopel_triggerargs(bot, trigger, 'nickname_command') - - logtype = spicemanip.main(triggerargs, 1) or None - if not logtype or not inlist(bot, logtype, bot.memory['SpiceBot_Logs'].keys()): - bot.osd("Current valid log(s) include: " + spicemanip.main(bot.memory['SpiceBot_Logs'].keys(), 'andlist'), trigger.sender, 'action') - return - - logtype = inlist_match(bot, logtype, bot.memory['SpiceBot_Logs'].keys()) - - if len(bot.memory['SpiceBot_Logs'][logtype]) == 0: - bot.osd("No logs found for " + str(logtype) + ".") - return - - bot.osd("Is Examining " + str(logtype) + " log(s).") - for line in bot.memory['SpiceBot_Logs'][logtype]: - bot.osd(" " + str(line)) +@sopel.module.event('2004') +@sopel.module.rule('.*') +def bot_events_setup(bot, trigger): + bot_events_recieved(bot, trigger.event) diff --git a/sopel_modules/SpiceBot_Logs/Logs_Command.py b/sopel_modules/SpiceBot_Logs/Logs_Command.py new file mode 100644 index 0000000..c201128 --- /dev/null +++ b/sopel_modules/SpiceBot_Logs/Logs_Command.py @@ -0,0 +1,38 @@ +# coding=utf-8 + +from __future__ import unicode_literals, absolute_import, division, print_function + +import sopel.module + +import spicemanip + +from sopel_modules.SpiceBot_SBTools import sopel_triggerargs, command_permissions_check, inlist, inlist_match +from sopel_modules.SpiceBot_Events.System import bot_events_check + + +@sopel.module.nickname_commands('logs') +def bot_command_action(bot, trigger): + + while not bot_events_check(bot, '2004'): + pass + + if not command_permissions_check(bot, trigger, ['admins', 'owner']): + bot.say("I was unable to process this Bot Nick command due to privilege issues.") + return + + triggerargs, triggercommand = sopel_triggerargs(bot, trigger, 'nickname_command') + + logtype = spicemanip.main(triggerargs, 1) or None + if not logtype or not inlist(bot, logtype, bot.memory['SpiceBot_Logs']["logs"].keys()): + bot.osd("Current valid log(s) include: " + spicemanip.main(bot.memory['SpiceBot_Logs']["logs"].keys(), 'andlist'), trigger.sender, 'action') + return + + logtype = inlist_match(bot, logtype, bot.memory['SpiceBot_Logs'].keys()) + + if len(bot.memory['SpiceBot_Logs']["logs"][logtype]) == 0: + bot.osd("No logs found for " + str(logtype) + ".") + return + + bot.osd("Is Examining " + str(logtype) + " log(s).") + for line in bot.memory['SpiceBot_Logs']["logs"][logtype]: + bot.osd(" " + str(line)) diff --git a/sopel_modules/SpiceBot_Logs/__init__.py b/sopel_modules/SpiceBot_Logs/__init__.py index f9d038a..4c90034 100644 --- a/sopel_modules/SpiceBot_Logs/__init__.py +++ b/sopel_modules/SpiceBot_Logs/__init__.py @@ -2,6 +2,7 @@ from __future__ import unicode_literals, absolute_import, division, print_function from .Logs import * +from .Logs_Command import * __author__ = 'Sam Zick' __email__ = 'sam@deathbybandaid.net' diff --git a/sopel_modules/SpiceBot_SBTools/__init__.py b/sopel_modules/SpiceBot_SBTools/__init__.py index 757018e..a4b67d0 100644 --- a/sopel_modules/SpiceBot_SBTools/__init__.py +++ b/sopel_modules/SpiceBot_SBTools/__init__.py @@ -95,24 +95,21 @@ def command_permissions_check(bot, trigger, privslist): def bot_logging(bot, logtype, logentry): - if 'SpiceBot_Logs_queue' not in bot.memory: - bot.memory['SpiceBot_Logs_queue'] = [] + if 'SpiceBot_Logs' not in bot.memory: + bot.memory['SpiceBot_Logs'] = {"logs": {}, "queue": []} logmessage = "[" + logtype + "] " + logentry + "" - bot.memory['SpiceBot_Logs_queue'].append(logmessage) + bot.memory['SpiceBot_Logs']["queue"].append(logmessage) stderr("\n" + logmessage + "\n") - if 'SpiceBot_Logs' not in bot.memory: - bot.memory['SpiceBot_Logs'] = {} + if logtype not in bot.memory['SpiceBot_Logs']["logs"].keys(): + bot.memory['SpiceBot_Logs']["logs"][logtype] = [] - if logtype not in bot.memory['SpiceBot_Logs'].keys(): - bot.memory['SpiceBot_Logs'][logtype] = [] - - bot.memory['SpiceBot_Logs'][logtype].append(logentry) - if len(bot.memory['SpiceBot_Logs'][logtype]) > 10: - del bot.memory['SpiceBot_Logs'][logtype][0] + bot.memory['SpiceBot_Logs']["logs"][logtype].append(logentry) + if len(bot.memory['SpiceBot_Logs']["logs"][logtype]) > 10: + del bot.memory['SpiceBot_Logs']["logs"][logtype][0] """ diff --git a/sopel_modules/SpiceBot_StartupMonologue/StartupMonologue.py b/sopel_modules/SpiceBot_StartupMonologue/StartupMonologue.py index 02143f7..f0fe416 100644 --- a/sopel_modules/SpiceBot_StartupMonologue/StartupMonologue.py +++ b/sopel_modules/SpiceBot_StartupMonologue/StartupMonologue.py @@ -2,38 +2,36 @@ from __future__ import unicode_literals, absolute_import, division, print_function -from sopel import module - -from sopel_modules.SpiceBot_LoadOrder.LoadOrder import set_bot_event, check_bot_events +import sopel.module +from sopel_modules.SpiceBot_Events.System import bot_events_startup_register, bot_events_recieved, bot_events_trigger +from sopel_modules.SpiceBot_LoadOrder.LoadOrder import set_bot_event from sopel_modules.SpiceBot_SBTools import humanized_time, bot_logging import time -@module.event('001') -@module.rule('.*') -@module.thread(True) -def bot_startup_monologue(bot, trigger): +def setup(bot): + bot_logging(bot, 'SpiceBot_StartupMonologue', "Starting setup procedure") + bot_events_startup_register(bot, ['2005']) - while not check_bot_events(bot, ["connected"]): - pass - - if check_bot_events(bot, ["SpiceBot_StartupMonologue"]): - startup_reconnect(bot, trigger) - else: - startup_fresh(bot, trigger) + if 'SpiceBot_StartupMonologue' not in bot.memory: + bot.memory['SpiceBot_StartupMonologue'] = [] -def startup_fresh(bot, trigger): +@sopel.module.event('1003') +@sopel.module.rule('.*') +def bot_startup_monologue_start(bot, trigger): # Startup bot_logging(bot, 'SpiceBot_StartupMonologue', bot.nick + " is now starting. Please wait while I load my configuration") bot.osd(" is now starting. Please wait while I load my configuration.", bot.channels.keys(), 'ACTION') - startupcomplete = [bot.nick + " startup complete"] + bot.memory['SpiceBot_StartupMonologue'].append(bot.nick + " startup complete") - while not check_bot_events(bot, ["SpiceBot_CommandsQuery"]): - pass + +@sopel.module.event('2002') +@sopel.module.rule('.*') +def bot_startup_monologue_commands(bot, trigger): availablecomsnum, availablecomsfiles = 0, 0 for commandstype in bot.memory['SpiceBot_CommandsQuery']['commands'].keys(): @@ -41,31 +39,35 @@ def startup_fresh(bot, trigger): for commandstype in bot.memory['SpiceBot_CommandsQuery']['counts'].keys(): availablecomsfiles += bot.memory['SpiceBot_CommandsQuery']['counts'][commandstype] - startupcomplete.append("There are " + str(availablecomsnum) + " commands available in " + str(availablecomsfiles) + " files.") + bot.memory['SpiceBot_StartupMonologue'].append("There are " + str(availablecomsnum) + " commands available in " + str(availablecomsfiles) + " files.") bot_logging(bot, 'SpiceBot_StartupMonologue', "There are " + str(availablecomsnum) + " commands available in " + str(availablecomsfiles) + " files.") - while not check_bot_events(bot, ["SpiceBot_Channels"]): - pass + +@sopel.module.event('2001') +@sopel.module.rule('.*') +def bot_startup_monologue_channels(bot, trigger): botcount = len(bot.channels.keys()) servercount = len(bot.memory['SpiceBot_Channels']['channels'].keys()) - startupcomplete.append("I am in " + str(botcount) + " of " + str(servercount) + " channel(s) available on this server.") + bot.memory['SpiceBot_StartupMonologue'].append("I am in " + str(botcount) + " of " + str(servercount) + " channel(s) available on this server.") - while not check_bot_events(bot, ["startup_complete"]): - pass + +@sopel.module.event('1004') +@sopel.module.rule('.*') +def bot_startup_monologue_display(bot, trigger): timesince = humanized_time(time.time() - bot.memory["SpiceBot_Uptime"]) - startupcomplete.append("Startup took " + timesince) + bot.memory['SpiceBot_StartupMonologue'].append("Startup took " + timesince) # Announce to chan, then handle some closing stuff bot_logging(bot, 'SpiceBot_StartupMonologue', bot.nick + " startup complete") - bot.osd(startupcomplete, bot.channels.keys()) + bot.osd(bot.memory['SpiceBot_StartupMonologue'], bot.channels.keys()) set_bot_event(bot, "SpiceBot_StartupMonologue") + bot_events_trigger(bot, 2005, "SpiceBot_StartupMonologue") -def startup_reconnect(bot, trigger): - - # Startup - bot_logging(bot, 'SpiceBot_StartupMonologue', bot.nick + " has reconnected.") - bot.osd(" has reconnected.", bot.channels.keys(), 'ACTION') +@sopel.module.event('2005') +@sopel.module.rule('.*') +def bot_events_setup(bot, trigger): + bot_events_recieved(bot, trigger.event)