even more close connections from get_db()
This commit is contained in:
parent
097077174b
commit
1e87fe59d8
@ -87,11 +87,13 @@ class Markov(Module):
|
||||
sql = 'INSERT INTO drbotzo_modules VALUES (?,?)'
|
||||
db.execute(sql, (self.__class__.__name__, 1))
|
||||
db.commit()
|
||||
db.close()
|
||||
version = 1
|
||||
|
||||
self._learn_line('')
|
||||
except sqlite3.Error as e:
|
||||
db.rollback()
|
||||
db.close()
|
||||
print("sqlite error: " + str(e))
|
||||
raise
|
||||
if (version < 2):
|
||||
@ -115,9 +117,11 @@ class Markov(Module):
|
||||
db.execute('UPDATE drbotzo_modules SET version = ? WHERE module = ?',
|
||||
(2, self.__class__.__name__))
|
||||
db.commit()
|
||||
db.close()
|
||||
version = 2
|
||||
except sqlite3.Error as e:
|
||||
db.rollback()
|
||||
db.close()
|
||||
print('sqlite error: ' + str(e))
|
||||
raise
|
||||
if (version < 3):
|
||||
@ -129,9 +133,11 @@ class Markov(Module):
|
||||
db.execute('UPDATE drbotzo_modules SET version = ? WHERE module = ?',
|
||||
(3, self.__class__.__name__))
|
||||
db.commit()
|
||||
db.close()
|
||||
version = 3
|
||||
except sqlite3.Error as e:
|
||||
db.rollback()
|
||||
db.close()
|
||||
print('sqlite error: ' + str(e))
|
||||
raise
|
||||
if (version < 4):
|
||||
@ -140,9 +146,11 @@ class Markov(Module):
|
||||
db.execute('UPDATE drbotzo_modules SET version = ? WHERE module = ?',
|
||||
(4, self.__class__.__name__))
|
||||
db.commit()
|
||||
db.close()
|
||||
version = 4
|
||||
except sqlite3.Error as e:
|
||||
db.rollback()
|
||||
db.close()
|
||||
print('sqlite error: ' + str(e))
|
||||
raise
|
||||
if (version < 5):
|
||||
@ -155,9 +163,11 @@ class Markov(Module):
|
||||
target TEXT NOT NULL
|
||||
)''')
|
||||
db.commit()
|
||||
db.close()
|
||||
self.db_register_module_version(self.__class__.__name__, version)
|
||||
except sqlite3.Error as e:
|
||||
db.rollback()
|
||||
db.close()
|
||||
print('sqlite error: ' + str(e))
|
||||
raise
|
||||
if (version < 6):
|
||||
@ -168,9 +178,11 @@ class Markov(Module):
|
||||
CREATE INDEX markov_chain_keys_and_context_index
|
||||
ON markov_chain (k1, k2, context)''')
|
||||
db.commit()
|
||||
db.close()
|
||||
self.db_register_module_version(self.__class__.__name__, version)
|
||||
except sqlite3.Error as e:
|
||||
db.rollback()
|
||||
db.close()
|
||||
print('sqlite error: ' + str(e))
|
||||
raise
|
||||
|
||||
@ -336,8 +348,10 @@ class Markov(Module):
|
||||
cur.execute(statement, (k1.decode('utf-8', 'replace').lower(), k2.decode('utf-8', 'replace').lower(), self.stop, context))
|
||||
|
||||
db.commit()
|
||||
db.close()
|
||||
except sqlite3.Error as e:
|
||||
db.rollback()
|
||||
db.close()
|
||||
print("sqlite error: " + str(e))
|
||||
raise
|
||||
|
||||
@ -410,8 +424,10 @@ class Markov(Module):
|
||||
for result in results:
|
||||
values.append(result['v'])
|
||||
|
||||
db.close()
|
||||
return values
|
||||
except sqlite3.Error as e:
|
||||
db.close()
|
||||
print('sqlite error: ' + str(e))
|
||||
raise
|
||||
|
||||
@ -433,6 +449,7 @@ class Markov(Module):
|
||||
|
||||
return values
|
||||
except sqlite3.Error as e:
|
||||
db.close()
|
||||
print('sqlite error: ' + str(e))
|
||||
raise
|
||||
|
||||
@ -453,11 +470,13 @@ class Markov(Module):
|
||||
'''
|
||||
cursor = db.execute(query)
|
||||
result = cursor.fetchone()
|
||||
db.close()
|
||||
if result:
|
||||
return result['id']
|
||||
else:
|
||||
return None
|
||||
except sqlite3.Error as e:
|
||||
db.close()
|
||||
print('sqlite error: ' + str(e))
|
||||
raise
|
||||
|
||||
|
@ -62,8 +62,10 @@ class Pi(Module):
|
||||
''')
|
||||
db.execute('INSERT INTO drbotzo_modules VALUES (?,?)', (self.__class__.__name__, 1))
|
||||
db.commit()
|
||||
db.close()
|
||||
except sqlite3.Error as e:
|
||||
db.rollback()
|
||||
db.close()
|
||||
print("sqlite error: " + str(e))
|
||||
raise
|
||||
|
||||
@ -96,8 +98,10 @@ class Pi(Module):
|
||||
|
||||
cur.execute('INSERT INTO pi_log (count_inside, count_total) VALUES (?,?)', (count_inside, count))
|
||||
db.commit()
|
||||
db.close()
|
||||
except sqlite3.Error as e:
|
||||
db.rollback()
|
||||
db.close()
|
||||
return self.reply(connection, event, "sqlite error: " + str(e))
|
||||
|
||||
return self.reply(connection, event, "({0:.10f}, {1:.10f}) is {2}within the circle. pi is {5:.10f}. (i:{3:d} p:{4:d})".format(x, y, "" if inside else "not ", count_inside, count, pi))
|
||||
|
@ -46,9 +46,11 @@ class Seen(Module):
|
||||
sql = 'INSERT INTO drbotzo_modules VALUES (?,?)'
|
||||
db.execute(sql, (self.__class__.__name__, 1))
|
||||
db.commit()
|
||||
db.close()
|
||||
version = 1
|
||||
except sqlite3.Error as e:
|
||||
db.rollback()
|
||||
db.close()
|
||||
print("sqlite error: " + str(e))
|
||||
raise
|
||||
if version < 2:
|
||||
@ -68,9 +70,11 @@ class Seen(Module):
|
||||
CREATE UNIQUE INDEX seen_nicks_nick_and_location_index
|
||||
ON seen_nicks (nick, location)''')
|
||||
db.commit()
|
||||
db.close()
|
||||
self.db_register_module_version(self.__class__.__name__, version)
|
||||
except sqlite3.Error as e:
|
||||
db.rollback()
|
||||
db.close()
|
||||
print("sqlite error: " + str(e))
|
||||
raise
|
||||
|
||||
@ -88,8 +92,10 @@ class Seen(Module):
|
||||
statement = 'REPLACE INTO seen_nicks (nick, location, host, what) VALUES (?, ?, ?, ?)'
|
||||
cur.execute(statement, (nick, where, userhost, what.decode('utf-8', 'replace')))
|
||||
db.commit()
|
||||
db.close()
|
||||
except sqlite3.Error as e:
|
||||
db.rollback()
|
||||
db.close()
|
||||
print("sqlite error: " + str(e))
|
||||
raise
|
||||
|
||||
@ -101,12 +107,14 @@ class Seen(Module):
|
||||
query = 'SELECT * FROM seen_nicks WHERE nick = ? AND location = ?'
|
||||
cursor = db.execute(query, (nick,where))
|
||||
result = cursor.fetchone()
|
||||
db.close()
|
||||
if result:
|
||||
seentime = datetime.strptime(result['time'], '%Y-%m-%d %H:%M:%S').replace(tzinfo=tzutc())
|
||||
replystr = 'last saw {0:s} in {3:s} at {1:s} saying \'{2:s}\''.format(result['nick'], seentime.astimezone(tzlocal()).strftime('%Y/%m/%d %H:%M:%S %Z'), result['what'].encode('utf-8', 'ignore'), result['location'].encode('utf-8', 'ignore'))
|
||||
return self.reply(connection, event, replystr)
|
||||
except sqlite3.Error as e:
|
||||
db.rollback()
|
||||
db.close()
|
||||
print("sqlite error: " + str(e))
|
||||
raise
|
||||
|
||||
|
@ -154,9 +154,11 @@ class Storycraft(Module):
|
||||
sql = 'INSERT INTO drbotzo_modules VALUES (?,?)'
|
||||
db.execute(sql, (self.__class__.__name__, 1))
|
||||
db.commit()
|
||||
db.close()
|
||||
version = 1
|
||||
except sqlite3.Error as e:
|
||||
db.rollback()
|
||||
db.close()
|
||||
print("sqlite error: " + str(e))
|
||||
raise
|
||||
|
||||
@ -534,6 +536,7 @@ class Storycraft(Module):
|
||||
'''
|
||||
cursor = db.execute(query, (game_id,))
|
||||
result = cursor.fetchone()
|
||||
db.close()
|
||||
if result:
|
||||
game = self.StorycraftGame()
|
||||
|
||||
@ -551,6 +554,7 @@ class Storycraft(Module):
|
||||
|
||||
return game
|
||||
except sqlite3.Error as e:
|
||||
db.close()
|
||||
print('sqlite error: ' + str(e))
|
||||
raise
|
||||
|
||||
@ -608,8 +612,10 @@ class Storycraft(Module):
|
||||
'''
|
||||
cur.execute(statement, (round_mode, game_length, line_length, random_method, lines_per_turn, 'OPEN', nick, userhost))
|
||||
db.commit()
|
||||
db.close()
|
||||
return cur.lastrowid
|
||||
except sqlite3.Error as e:
|
||||
db.close()
|
||||
print('sqlite error: ' + str(e))
|
||||
raise
|
||||
|
||||
@ -623,6 +629,7 @@ class Storycraft(Module):
|
||||
query = 'SELECT id, game_id, nick, userhost FROM storycraft_player WHERE game_id = ?'
|
||||
cursor = db.execute(query, (game_id,))
|
||||
results = cursor.fetchall()
|
||||
db.close()
|
||||
for result in results:
|
||||
player = self.StorycraftPlayer()
|
||||
|
||||
@ -635,6 +642,7 @@ class Storycraft(Module):
|
||||
|
||||
return players
|
||||
except sqlite3.Error as e:
|
||||
db.close()
|
||||
print('sqlite error: ' + str(e))
|
||||
raise
|
||||
|
||||
@ -654,8 +662,10 @@ class Storycraft(Module):
|
||||
'''
|
||||
cur.execute(statement, (game_id,))
|
||||
db.commit()
|
||||
db.close()
|
||||
return game_id
|
||||
except sqlite3.Error as e:
|
||||
db.close()
|
||||
print('sqlite error: ' + str(e))
|
||||
raise
|
||||
|
||||
@ -716,8 +726,10 @@ class Storycraft(Module):
|
||||
'''
|
||||
cur.execute(statement, (game_id, player_id, ''))
|
||||
db.commit()
|
||||
db.close()
|
||||
return cur.lastrowid
|
||||
except sqlite3.Error as e:
|
||||
db.close()
|
||||
print('sqlite error: ' + str(e))
|
||||
raise
|
||||
|
||||
@ -733,8 +745,10 @@ class Storycraft(Module):
|
||||
'''
|
||||
cur.execute(statement, (game_id,))
|
||||
db.commit()
|
||||
db.close()
|
||||
return game_id
|
||||
except sqlite3.Error as e:
|
||||
db.close()
|
||||
print('sqlite error: ' + str(e))
|
||||
raise
|
||||
|
||||
@ -747,6 +761,7 @@ class Storycraft(Module):
|
||||
query = 'SELECT id, game_id, nick, userhost FROM storycraft_player WHERE id = ?'
|
||||
cursor = db.execute(query, (player_id,))
|
||||
result = cursor.fetchone()
|
||||
db.close()
|
||||
if result:
|
||||
player = self.StorycraftPlayer()
|
||||
|
||||
@ -757,6 +772,7 @@ class Storycraft(Module):
|
||||
|
||||
return player
|
||||
except sqlite3.Error as e:
|
||||
db.close()
|
||||
print('sqlite error: ' + str(e))
|
||||
raise
|
||||
|
||||
@ -769,6 +785,7 @@ class Storycraft(Module):
|
||||
query = 'SELECT id, game_id, nick, userhost FROM storycraft_player WHERE game_id = ? AND userhost = ?'
|
||||
cursor = db.execute(query, (game_id,userhost))
|
||||
result = cursor.fetchone()
|
||||
db.close()
|
||||
if result:
|
||||
player = self.StorycraftPlayer()
|
||||
|
||||
@ -779,6 +796,7 @@ class Storycraft(Module):
|
||||
|
||||
return player
|
||||
except sqlite3.Error as e:
|
||||
db.close()
|
||||
print('sqlite error: ' + str(e))
|
||||
raise
|
||||
|
||||
@ -814,6 +832,7 @@ class Storycraft(Module):
|
||||
'''
|
||||
cursor = db.execute(query, (player_nick,))
|
||||
results = cursor.fetchall()
|
||||
db.close()
|
||||
for result in results:
|
||||
game = self.StorycraftGame()
|
||||
|
||||
@ -833,6 +852,7 @@ class Storycraft(Module):
|
||||
|
||||
return games
|
||||
except sqlite3.Error as e:
|
||||
db.close()
|
||||
print('sqlite error: ' + str(e))
|
||||
raise
|
||||
|
||||
@ -864,6 +884,7 @@ class Storycraft(Module):
|
||||
'''
|
||||
cursor = db.execute(query, (player_nick, player_nick))
|
||||
results = cursor.fetchall()
|
||||
db.close()
|
||||
for result in results:
|
||||
game = self.StorycraftGame()
|
||||
|
||||
@ -883,6 +904,7 @@ class Storycraft(Module):
|
||||
|
||||
return games
|
||||
except sqlite3.Error as e:
|
||||
db.close()
|
||||
print('sqlite error: ' + str(e))
|
||||
raise
|
||||
|
||||
@ -901,6 +923,7 @@ class Storycraft(Module):
|
||||
'''
|
||||
cursor = db.execute(query, (game_type,))
|
||||
results = cursor.fetchall()
|
||||
db.close()
|
||||
for result in results:
|
||||
game = self.StorycraftGame()
|
||||
|
||||
@ -920,6 +943,7 @@ class Storycraft(Module):
|
||||
|
||||
return games
|
||||
except sqlite3.Error as e:
|
||||
db.close()
|
||||
print('sqlite error: ' + str(e))
|
||||
raise
|
||||
|
||||
@ -935,8 +959,10 @@ class Storycraft(Module):
|
||||
'''
|
||||
cur.execute(statement, (game_id, nick, userhost))
|
||||
db.commit()
|
||||
db.close()
|
||||
return cur.lastrowid
|
||||
except sqlite3.Error as e:
|
||||
db.close()
|
||||
print('sqlite error: ' + str(e))
|
||||
raise
|
||||
|
||||
@ -978,9 +1004,11 @@ class Storycraft(Module):
|
||||
query = 'SELECT COUNT(*) FROM storycraft_game WHERE status = ?'
|
||||
cursor = db.execute(query, (game_type,))
|
||||
result = cursor.fetchone()
|
||||
db.close()
|
||||
if result:
|
||||
count = result['COUNT(*)']
|
||||
except sqlite3.Error as e:
|
||||
db.close()
|
||||
print('sqlite error: ' + str(e))
|
||||
raise
|
||||
|
||||
@ -996,9 +1024,11 @@ class Storycraft(Module):
|
||||
query = 'SELECT concurrent_games FROM storycraft_config'
|
||||
cursor = db.execute(query)
|
||||
result = cursor.fetchone()
|
||||
db.close()
|
||||
if result:
|
||||
concurrency = result['concurrent_games']
|
||||
except sqlite3.Error as e:
|
||||
db.close()
|
||||
print('sqlite error: ' + str(e))
|
||||
raise
|
||||
|
||||
@ -1018,6 +1048,7 @@ class Storycraft(Module):
|
||||
'''
|
||||
cursor = db.execute(query, (game_id,))
|
||||
results = cursor.fetchall()
|
||||
db.close()
|
||||
for result in results:
|
||||
line = self.StorycraftLine()
|
||||
|
||||
@ -1031,6 +1062,7 @@ class Storycraft(Module):
|
||||
|
||||
return lines
|
||||
except sqlite3.Error as e:
|
||||
db.close()
|
||||
print('sqlite error: ' + str(e))
|
||||
raise
|
||||
|
||||
@ -1045,8 +1077,10 @@ class Storycraft(Module):
|
||||
'''
|
||||
cur.execute(statement, (input_line.decode('utf-8', 'replace'), line_id))
|
||||
db.commit()
|
||||
db.close()
|
||||
return line_id
|
||||
except sqlite3.Error as e:
|
||||
db.close()
|
||||
print('sqlite error: ' + str(e))
|
||||
raise
|
||||
|
||||
@ -1095,6 +1129,7 @@ class Storycraft(Module):
|
||||
'''
|
||||
cursor = db.execute(query)
|
||||
result = cursor.fetchone()
|
||||
db.close()
|
||||
if result:
|
||||
settings = self.StorycraftSettings()
|
||||
|
||||
@ -1108,6 +1143,7 @@ class Storycraft(Module):
|
||||
|
||||
return settings
|
||||
except sqlite3.Error as e:
|
||||
db.close()
|
||||
print('sqlite error: ' + str(e))
|
||||
raise
|
||||
|
||||
|
@ -88,8 +88,10 @@ class Twitter(Module):
|
||||
db.execute('''INSERT INTO twitter_settings (since_id, output_channel) VALUES (0, '#drbotzo')''')
|
||||
db.execute('INSERT INTO drbotzo_modules VALUES (?,?)', (self.__class__.__name__, 1))
|
||||
db.commit()
|
||||
db.close()
|
||||
except sqlite3.Error as e:
|
||||
db.rollback()
|
||||
db.close()
|
||||
print("sqlite error: " + str(e))
|
||||
raise
|
||||
|
||||
@ -301,17 +303,15 @@ class Twitter(Module):
|
||||
"""Get the since_id out of the database."""
|
||||
|
||||
try:
|
||||
# need to create our own db object, since this is likely going
|
||||
# to be called in a new thread
|
||||
dbfile = self.config.get('dr.botzo', 'database')
|
||||
db = sqlite3.connect(dbfile)
|
||||
db.row_factory = sqlite3.Row
|
||||
db = self.get_db()
|
||||
query = 'SELECT since_id FROM twitter_settings'
|
||||
cursor = db.execute(query)
|
||||
result = cursor.fetchone()
|
||||
db.close()
|
||||
if result:
|
||||
return result['since_id']
|
||||
except sqlite3.Error as e:
|
||||
db.close()
|
||||
print("sqlite error: " + str(e))
|
||||
raise
|
||||
|
||||
@ -319,18 +319,15 @@ class Twitter(Module):
|
||||
"""Get the output_channel out of the database."""
|
||||
|
||||
try:
|
||||
# need to create our own db object, since this is likely going
|
||||
# to be called in a new thread
|
||||
dbfile = self.config.get('dr.botzo', 'database')
|
||||
self.conn = sqlite3.connect(dbfile)
|
||||
self.conn.row_factory = sqlite3.Row
|
||||
db = self.conn
|
||||
db = self.get_db()
|
||||
query = 'SELECT output_channel FROM twitter_settings'
|
||||
cursor = db.execute(query)
|
||||
result = cursor.fetchone()
|
||||
db.close()
|
||||
if result:
|
||||
return result['output_channel']
|
||||
except sqlite3.Error as e:
|
||||
db.close()
|
||||
print("sqlite error: " + str(e))
|
||||
raise
|
||||
|
||||
@ -338,17 +335,14 @@ class Twitter(Module):
|
||||
"""Set the since_id."""
|
||||
|
||||
try:
|
||||
# need to create our own db object, since this is likely going
|
||||
# to be called in a new thread
|
||||
dbfile = self.config.get('dr.botzo', 'database')
|
||||
self.conn = sqlite3.connect(dbfile)
|
||||
self.conn.row_factory = sqlite3.Row
|
||||
db = self.conn
|
||||
db = self.get_db()
|
||||
cur = db.cursor()
|
||||
statement = 'UPDATE twitter_settings SET since_id = ?'
|
||||
cur.execute(statement, (since_id,))
|
||||
db.commit()
|
||||
db.close()
|
||||
except sqlite3.Error as e:
|
||||
db.close()
|
||||
print("sqlite error: " + str(e))
|
||||
raise
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user