From 16ddabfe2a4e75de1b024e74d3a1376eb58e5604 Mon Sep 17 00:00:00 2001 From: Luke Rogers Date: Sat, 15 Feb 2014 18:32:51 +1300 Subject: [PATCH] db stuff --- core/bot.py | 3 +- core/irc.py | 2 +- core/main.py | 12 ++-- plugins/dbtest.py | 14 +---- plugins/regex_chans.py | 128 ----------------------------------------- 5 files changed, 12 insertions(+), 147 deletions(-) delete mode 100644 plugins/regex_chans.py diff --git a/core/bot.py b/core/bot.py index ba233d1..acddec2 100644 --- a/core/bot.py +++ b/core/bot.py @@ -45,6 +45,7 @@ def get_logger(): logger.addHandler(sh) return logger + class Bot(threading.Thread): def __init__(self): # basic variables @@ -163,4 +164,4 @@ class Bot(threading.Thread): def restart(self, reason=None): """shuts the bot down and restarts it""" self.do_restart = True - self.stop(reason) + self.stop(reason) \ No newline at end of file diff --git a/core/irc.py b/core/irc.py index 6e15237..f0a06db 100644 --- a/core/irc.py +++ b/core/irc.py @@ -106,7 +106,7 @@ class SendThread(threading.Thread): def run(self): while not self.shutdown: line = self.output_queue.get().splitlines()[0][:500] - #print u"{}> {}".format(self.conn_name, line) + print u"{}> {}".format(self.conn_name, line) self.output_buffer += line.encode('utf-8', 'replace') + '\r\n' while self.output_buffer: sent = self.socket.send(self.output_buffer) diff --git a/core/main.py b/core/main.py index a394816..955418f 100644 --- a/core/main.py +++ b/core/main.py @@ -73,6 +73,10 @@ def run(bot, func, 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: @@ -80,6 +84,10 @@ def run(bot, func, input): except: bot.logger.exception("Error in plugin {}:".format(func._filename)) return + finally: + if uses_db: + print "Close" + input.db.close() else: try: out = func(input.inp) @@ -89,10 +97,6 @@ def run(bot, func, input): if out is not None: input.reply(unicode(out)) - if uses_db: - # close SQLAlchemy session - input.db.close() - def do_sieve(sieve, bot, input, func, type, args): try: diff --git a/plugins/dbtest.py b/plugins/dbtest.py index e4d47e0..3f48fc7 100644 --- a/plugins/dbtest.py +++ b/plugins/dbtest.py @@ -3,18 +3,6 @@ from util import hook from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey from sqlalchemy.orm import scoped_session, sessionmaker -def create_tables(metadata): - users_table = Table('users', metadata, - Column('id', Integer, primary_key=True), - Column('name', String), - Column('fullname', String), - Column('password', String) - ) - users_table.create() - - @hook.command def dbtest(inp, db=None): - metadata = MetaData(db) - create_tables(metadata) - print metadata \ No newline at end of file + print db \ No newline at end of file diff --git a/plugins/regex_chans.py b/plugins/regex_chans.py deleted file mode 100644 index f49411a..0000000 --- a/plugins/regex_chans.py +++ /dev/null @@ -1,128 +0,0 @@ -from util import hook - - -# Default value. -# If True, all channels without a setting will have regex enabled -# If False, all channels without a setting will have regex disabled -default_enabled = True - -db_already_initiated = False - - -def db_init(db): - global db_already_initiated - if not db_already_initiated: - db_already_initiated = True - db.execute("CREATE TABLE IF NOT EXISTS regexchans(channel PRIMARY KEY, status)") - db.commit() - - -def get_status(db, channel): - row = db.execute("SELECT status FROM regexchans WHERE channel = ?", [channel]).fetchone() - if row: - return row[0] - else: - return None - - -def set_status(db, channel, status): - row = db.execute("REPLACE INTO regexchans (channel, status) VALUES(?, ?)", [channel, status]) - db.commit() - - -def delete_status(db, channel): - row = db.execute("DELETE FROM regexchans WHERE channel = ?", [channel]) - db.commit() - - -def list_status(db): - row = db.execute("SELECT * FROM regexchans").fetchall() - result = None - for values in row: - if result: - result += u", {}: {}".format(values[0], values[1]) - else: - result = u"{}: {}".format(values[0], values[1]) - return result - - -@hook.sieve -def sieve_regex(bot, inp, func, kind, args): - db = bot.get_db_connection(inp.conn) - db_init(db) - if kind == 'regex' and inp.chan.startswith("#") and func.__name__ != 'factoid': - chanstatus = get_status(db, inp.chan) - if chanstatus != "ENABLED" and (chanstatus == "DISABLED" or not default_enabled): - print u"Denying input.raw={}, kind={}, args={} from {}".format(inp.raw, kind, args, inp.chan) - return None - print u"Allowing input.raw={}, kind={}, args={} from {}".format(inp.raw, kind, args, inp.chan) - - return inp - - -@hook.command(permissions=["botcontrol"]) -def enableregex(inp, db=None, message=None, notice=None, chan=None, nick=None): - db_init(db) - inp = inp.strip().lower() - if not inp: - channel = chan - elif inp.startswith("#"): - channel = inp - else: - channel = u"#{}".format(inp) - - message(u"Enabling regex matching (youtube, etc) (issued by {})".format(nick), target=channel) - notice(u"Enabling regex matching (youtube, etc) in channel {}".format(channel)) - set_status(db, channel, "ENABLED") - - -@hook.command(permissions=["botcontrol"]) -def disableregex(inp, db=None, message=None, notice=None, chan=None, nick=None): - db_init(db) - inp = inp.strip().lower() - if not inp: - channel = chan - elif inp.startswith("#"): - channel = inp - else: - channel = u"#{}".format(inp) - - message(u"Disabling regex matching (youtube, etc) (issued by {})".format(nick), target=channel) - notice(u"Disabling regex matching (youtube, etc) in channel {}".format(channel)) - set_status(db, channel, "DISABLED") - - -@hook.command(permissions=["botcontrol"]) -def resetregex(inp, db=None, message=None, notice=None, chan=None, nick=None): - db_init(db) - inp = inp.strip().lower() - if not inp: - channel = chan - elif inp.startswith("#"): - channel = inp - else: - channel = u"#{}".format(inp) - - message(u"Resetting regex matching setting (youtube, etc) (issued by {})".format(nick), target=channel) - notice(u"Resetting regex matching setting (youtube, etc) in channel {}".format(channel)) - delete_status(db, channel) - - -@hook.command(permissions=["botcontrol"]) -def regexstatus(inp, db=None, chan=None): - db_init(db) - inp = inp.strip().lower() - if not inp: - channel = chan - elif inp.startswith("#"): - channel = inp - else: - channel = u"#{}".format(inp) - - return u"Regex status for {}: {}".format(channel, get_status(db, channel)) - - -@hook.command(permissions=["botcontrol"]) -def listregex(inp, db=None): - db_init(db) - return list_status(db)