test
This commit is contained in:
parent
cbf72f9a78
commit
fe92a78a89
3 changed files with 23 additions and 55 deletions
|
@ -39,7 +39,6 @@ class PluginLoader(object):
|
||||||
self.observer.start()
|
self.observer.start()
|
||||||
|
|
||||||
self.load_all()
|
self.load_all()
|
||||||
pprint(bot.plugins)
|
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
"""shuts down the plugin reloader"""
|
"""shuts down the plugin reloader"""
|
||||||
|
@ -88,7 +87,6 @@ class PluginLoader(object):
|
||||||
if hasattr(obj, '_hook'): # check for magic
|
if hasattr(obj, '_hook'): # check for magic
|
||||||
if obj._thread:
|
if obj._thread:
|
||||||
self.bot.threads[obj] = main.Handler(self.bot, obj)
|
self.bot.threads[obj] = main.Handler(self.bot, obj)
|
||||||
|
|
||||||
for plug_type, data in obj._hook:
|
for plug_type, data in obj._hook:
|
||||||
# add plugin to the plugin list
|
# add plugin to the plugin list
|
||||||
self.bot.plugins[plug_type] += [data]
|
self.bot.plugins[plug_type] += [data]
|
||||||
|
|
50
core/main.py
50
core/main.py
|
@ -54,34 +54,19 @@ class Input(dict):
|
||||||
|
|
||||||
|
|
||||||
def run(bot, func, input):
|
def run(bot, func, input):
|
||||||
args = func._args
|
uses_db = True
|
||||||
|
# TODO: change to bot.get_db_session()
|
||||||
uses_db = 'db' in args and 'db' not in input
|
print(input)
|
||||||
|
|
||||||
if 'inp' not in input:
|
if 'inp' not in input:
|
||||||
input.inp = input.paraml
|
input.inp = input.paraml
|
||||||
|
|
||||||
if args:
|
|
||||||
if uses_db:
|
if uses_db:
|
||||||
# create SQLAlchemy session
|
# create SQLAlchemy session
|
||||||
bot.logger.debug("Opened DB session for: {}".format(func._filename))
|
bot.logger.debug("Opened DB session for: {}".format(func._filename))
|
||||||
input.db = input.bot.db_session()
|
input.db = input.bot.db_session()
|
||||||
if 'input' in args:
|
|
||||||
input.input = input
|
|
||||||
if 0 in args:
|
|
||||||
try:
|
try:
|
||||||
out = func(input.inp, **input)
|
out = func(input, input.conn)
|
||||||
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:
|
except:
|
||||||
bot.logger.exception("Error in plugin {}:".format(func._filename))
|
bot.logger.exception("Error in plugin {}:".format(func._filename))
|
||||||
return
|
return
|
||||||
|
@ -89,12 +74,7 @@ def run(bot, func, input):
|
||||||
if uses_db:
|
if uses_db:
|
||||||
bot.logger.debug("Closed DB session for: {}".format(func._filename))
|
bot.logger.debug("Closed DB session for: {}".format(func._filename))
|
||||||
input.db.close()
|
input.db.close()
|
||||||
else:
|
|
||||||
try:
|
|
||||||
out = func(input.inp)
|
|
||||||
except:
|
|
||||||
bot.logger.exception("Error in plugin {}:".format(func._filename))
|
|
||||||
return
|
|
||||||
if out is not None:
|
if out is not None:
|
||||||
input.reply(str(out))
|
input.reply(str(out))
|
||||||
|
|
||||||
|
@ -124,24 +104,14 @@ class Handler(object):
|
||||||
if input == StopIteration:
|
if input == StopIteration:
|
||||||
break
|
break
|
||||||
|
|
||||||
if uses_db:
|
|
||||||
# self.bot.logger.debug("Opened ST DB session for: {}".format(self.func._filename))
|
|
||||||
input.db = input.bot.db_session()
|
|
||||||
|
|
||||||
try:
|
|
||||||
run(self.bot, self.func, input)
|
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):
|
def stop(self):
|
||||||
self.input_queue.put(StopIteration)
|
self.input_queue.put(StopIteration)
|
||||||
|
|
||||||
def put(self, value):
|
def put(self, value, args):
|
||||||
self.input_queue.put(value)
|
self.input_queue.put((value, args))
|
||||||
|
|
||||||
|
|
||||||
def dispatch(bot, input, kind, func, args, autohelp=False):
|
def dispatch(bot, input, kind, func, args, autohelp=False):
|
||||||
|
@ -155,7 +125,7 @@ def dispatch(bot, input, kind, func, args, autohelp=False):
|
||||||
return
|
return
|
||||||
|
|
||||||
if func._thread:
|
if func._thread:
|
||||||
bot.threads[func].put(input)
|
bot.threads[func].put(input, args)
|
||||||
else:
|
else:
|
||||||
_thread.start_new_thread(run, (bot, func, input))
|
_thread.start_new_thread(run, (bot, func, input))
|
||||||
|
|
||||||
|
|
|
@ -15,9 +15,9 @@ with open("./data/8ball_responses.txt") as f:
|
||||||
|
|
||||||
|
|
||||||
@hook.command('8ball')
|
@hook.command('8ball')
|
||||||
def eightball(inp, action=None):
|
def eightball(input, conn):
|
||||||
"""8ball <question> -- The all knowing magic eight ball,
|
"""8ball <question> -- The all knowing magic eight ball,
|
||||||
in electronic form. Ask and it shall be answered!"""
|
in electronic form. Ask and it shall be answered!"""
|
||||||
|
|
||||||
magic = text.multiword_replace(random.choice(responses), color_codes)
|
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))
|
||||||
|
|
Reference in a new issue