From fe92a78a89950b8f2bc823f431b49aea71e187b3 Mon Sep 17 00:00:00 2001 From: Luke Rogers Date: Tue, 25 Mar 2014 14:46:34 +1300 Subject: [PATCH] test --- core/loader.py | 2 -- core/main.py | 72 +++++++++++++------------------------------- plugins/eightball.py | 4 +-- 3 files changed, 23 insertions(+), 55 deletions(-) diff --git a/core/loader.py b/core/loader.py index c934cda..2374041 100644 --- a/core/loader.py +++ b/core/loader.py @@ -39,7 +39,6 @@ class PluginLoader(object): self.observer.start() self.load_all() - pprint(bot.plugins) def stop(self): """shuts down the plugin reloader""" @@ -88,7 +87,6 @@ class PluginLoader(object): if hasattr(obj, '_hook'): # check for magic if obj._thread: self.bot.threads[obj] = main.Handler(self.bot, obj) - for plug_type, data in obj._hook: # add plugin to the plugin list self.bot.plugins[plug_type] += [data] diff --git a/core/main.py b/core/main.py index 4bdc300..3ee33a1 100644 --- a/core/main.py +++ b/core/main.py @@ -54,47 +54,27 @@ class Input(dict): def run(bot, func, input): - args = func._args - - uses_db = 'db' in args and 'db' not in input - + uses_db = True + # TODO: change to bot.get_db_session() + print(input) if 'inp' not in input: input.inp = input.paraml - if args: + if uses_db: + # create SQLAlchemy session + bot.logger.debug("Opened DB session for: {}".format(func._filename)) + input.db = input.bot.db_session() + + try: + out = func(input, input.conn) + except: + bot.logger.exception("Error in plugin {}:".format(func._filename)) + return + finally: if uses_db: - # create SQLAlchemy session - bot.logger.debug("Opened DB session for: {}".format(func._filename)) - input.db = input.bot.db_session() - if 'input' in args: - input.input = input - if 0 in args: - try: - out = func(input.inp, **input) - except: - bot.logger.exception("Error in plugin {}:".format(func._filename)) - return - finally: - if uses_db: - print("Close") - input.db.close() - else: - kw = dict((key, input[key]) for key in args if key in input) - try: - out = func(input.inp, **kw) - except: - bot.logger.exception("Error in plugin {}:".format(func._filename)) - return - finally: - if uses_db: - bot.logger.debug("Closed DB session for: {}".format(func._filename)) - input.db.close() - else: - try: - out = func(input.inp) - except: - bot.logger.exception("Error in plugin {}:".format(func._filename)) - return + bot.logger.debug("Closed DB session for: {}".format(func._filename)) + input.db.close() + if out is not None: input.reply(str(out)) @@ -124,24 +104,14 @@ class Handler(object): if input == StopIteration: break - if uses_db: - # self.bot.logger.debug("Opened ST DB session for: {}".format(self.func._filename)) - input.db = input.bot.db_session() + run(self.bot, self.func, input) - try: - run(self.bot, self.func, input) - except: - self.bot.logger.exception("Error in plugin {}:".format(self.func._filename)) - finally: - if uses_db: - # self.bot.logger.debug("Closed ST DB session for: {}".format(self.func._filename)) - input.db.close() def stop(self): self.input_queue.put(StopIteration) - def put(self, value): - self.input_queue.put(value) + def put(self, value, args): + self.input_queue.put((value, args)) def dispatch(bot, input, kind, func, args, autohelp=False): @@ -155,7 +125,7 @@ def dispatch(bot, input, kind, func, args, autohelp=False): return if func._thread: - bot.threads[func].put(input) + bot.threads[func].put(input, args) else: _thread.start_new_thread(run, (bot, func, input)) diff --git a/plugins/eightball.py b/plugins/eightball.py index 1b8f5c1..9e21ad1 100644 --- a/plugins/eightball.py +++ b/plugins/eightball.py @@ -15,9 +15,9 @@ with open("./data/8ball_responses.txt") as f: @hook.command('8ball') -def eightball(inp, action=None): +def eightball(input, conn): """8ball -- The all knowing magic eight ball, in electronic form. Ask and it shall be answered!""" magic = text.multiword_replace(random.choice(responses), color_codes) - action("shakes the magic 8 ball... {}".format(magic)) + input.action("shakes the magic 8 ball... {}".format(magic))