From b6558ccdd53e75ea9045202bd99be1b576d2f957 Mon Sep 17 00:00:00 2001 From: Luke Rogers Date: Fri, 14 Feb 2014 19:43:36 +1300 Subject: [PATCH] I've been doing this all wrong for a long time --- plugins/encrypt.py | 13 +++++++------ plugins/factoids.py | 11 ++++++++--- plugins/horoscope.py | 12 ++++++------ plugins/notes.py | 8 ++++---- plugins/regex_chans.py | 8 ++++---- plugins/seen.py | 10 ++++++---- plugins/tell.py | 12 ++++++++---- 7 files changed, 43 insertions(+), 31 deletions(-) diff --git a/plugins/encrypt.py b/plugins/encrypt.py index 23d6a6f..e391a04 100644 --- a/plugins/encrypt.py +++ b/plugins/encrypt.py @@ -25,10 +25,12 @@ db_ready = False def db_init(db): """check to see that our db has the the encryption table.""" - db.execute("create table if not exists encryption(encrypted, iv, " - "primary key(encrypted))") - db.commit() - db_ready = True + global db_ready + if not db_ready: + db.execute("create table if not exists encryption(encrypted, iv, " + "primary key(encrypted))") + db.commit() + db_ready = True def get_salt(bot): @@ -42,8 +44,7 @@ def get_salt(bot): @hook.command def encrypt(inp, bot=None, db=None, notice=None): """encrypt -- Encrypts with . ( can only be decrypted using this bot)""" - if not db_ready: - db_init(db) + db_init(db) split = inp.split(" ") diff --git a/plugins/factoids.py b/plugins/factoids.py index 7468b66..403e6f5 100644 --- a/plugins/factoids.py +++ b/plugins/factoids.py @@ -7,6 +7,8 @@ from util import hook, http, text, pyexec re_lineends = re.compile(r'[\r\n]*') +db_ready = False + # some simple "shortcodes" for formatting purposes shortcodes = { '[b]': '\x02', @@ -18,9 +20,12 @@ shortcodes = { def db_init(db): - db.execute("create table if not exists mem(word, data, nick," - " primary key(word))") - db.commit() + global db_ready + if not db_ready: + db.execute("create table if not exists mem(word, data, nick," + " primary key(word))") + db.commit() + db_ready = True def get_memory(db, word): diff --git a/plugins/horoscope.py b/plugins/horoscope.py index 74b12e0..7145711 100644 --- a/plugins/horoscope.py +++ b/plugins/horoscope.py @@ -7,17 +7,17 @@ db_ready = False def db_init(db): """check to see that our db has the horoscope table and return a connection.""" - db.execute("create table if not exists horoscope(nick primary key, sign)") - db.commit() - db_ready = True + global db_ready + if not db_ready: + db.execute("create table if not exists horoscope(nick primary key, sign)") + db.commit() + db_ready = True @hook.command(autohelp=False) def horoscope(inp, db=None, notice=None, nick=None): """horoscope -- Get your horoscope.""" - - if not db_ready: - db_init(db) + db_init(db) # check if the user asked us not to save his details dontsave = inp.endswith(" dontsave") diff --git a/plugins/notes.py b/plugins/notes.py index 129b7d5..070a9fb 100644 --- a/plugins/notes.py +++ b/plugins/notes.py @@ -3,7 +3,7 @@ import re from util import hook -db_inited = False +db_ready = False def clean_sql(sql): @@ -11,8 +11,8 @@ def clean_sql(sql): def db_init(db): - global db_inited - if db_inited: + global db_ready + if db_ready: return exists = db.execute(""" @@ -32,7 +32,7 @@ def db_init(db): db.commit() - db_inited = True + db_ready = True def db_getall(db, nick, limit=-1): diff --git a/plugins/regex_chans.py b/plugins/regex_chans.py index f49411a..c16c250 100644 --- a/plugins/regex_chans.py +++ b/plugins/regex_chans.py @@ -6,15 +6,15 @@ from util import hook # If False, all channels without a setting will have regex disabled default_enabled = True -db_already_initiated = False +db_ready = False def db_init(db): - global db_already_initiated - if not db_already_initiated: - db_already_initiated = True + global db_ready + if not db_ready: db.execute("CREATE TABLE IF NOT EXISTS regexchans(channel PRIMARY KEY, status)") db.commit() + db_ready = True def get_status(db, channel): diff --git a/plugins/seen.py b/plugins/seen.py index 85d433a..65bfecb 100644 --- a/plugins/seen.py +++ b/plugins/seen.py @@ -11,10 +11,12 @@ db_ready = False def db_init(db): """check to see that our db has the the seen table and return a connection.""" - db.execute("create table if not exists seen_user(name, time, quote, chan, host, " - "primary key(name, chan))") - db.commit() - db_ready = True + global db_ready + if not db_ready: + db.execute("create table if not exists seen_user(name, time, quote, chan, host, " + "primary key(name, chan))") + db.commit() + db_ready = True @hook.singlethread diff --git a/plugins/tell.py b/plugins/tell.py index 67f7ec3..b29cf5b 100644 --- a/plugins/tell.py +++ b/plugins/tell.py @@ -6,13 +6,17 @@ import re from util import hook, timesince +db_ready = False def db_init(db): """check to see that our db has the tell table and return a dbection.""" - db.execute("create table if not exists tell" - "(user_to, user_from, message, chan, time," - "primary key(user_to, message))") - db.commit() + global db_ready + if not db_ready: + db.execute("create table if not exists tell" + "(user_to, user_from, message, chan, time," + "primary key(user_to, message))") + db.commit() + db_ready = True return db