diff --git a/.github/workflows/pythonapp.yml b/.github/workflows/pythonapp.yml index dcaefd1d..82ca287a 100644 --- a/.github/workflows/pythonapp.yml +++ b/.github/workflows/pythonapp.yml @@ -45,6 +45,7 @@ jobs: env: PYTHONDEVMODE: 1 PYTHONPATH: . + SQLALCHEMY_WARN_20: 1 - uses: codecov/codecov-action@v4 with: diff --git a/cloudbot/bot.py b/cloudbot/bot.py index 8587d579..7189f1b0 100644 --- a/cloudbot/bot.py +++ b/cloudbot/bot.py @@ -486,7 +486,7 @@ def migrate_db(self) -> None: if not inspector.has_table(table.name): continue - old_data = old_session.execute(table.select()).fetchall() + old_data = old_session.execute(table.select()).mappings().fetchall() if not old_data: continue diff --git a/cloudbot/util/database.py b/cloudbot/util/database.py index be5dfa77..7c6eb347 100644 --- a/cloudbot/util/database.py +++ b/cloudbot/util/database.py @@ -4,8 +4,12 @@ from sqlalchemy import MetaData from sqlalchemy.engine import Engine -from sqlalchemy.ext.declarative import declarative_base -from sqlalchemy.orm import close_all_sessions, scoped_session, sessionmaker +from sqlalchemy.orm import ( + close_all_sessions, + declarative_base, + scoped_session, + sessionmaker, +) __all__ = ("metadata", "base", "Base", "Session", "configure") @@ -13,7 +17,7 @@ Base = declarative_base() base = Base metadata: MetaData = Base.metadata -Session = scoped_session(sessionmaker()) +Session = scoped_session(sessionmaker(future=True)) def configure(bind: Engine = None) -> None: diff --git a/plugins/badwords.py b/plugins/badwords.py index 6f3f2f48..21c929a1 100644 --- a/plugins/badwords.py +++ b/plugins/badwords.py @@ -33,7 +33,7 @@ def load_bad(db): """- Should run on start of bot to load the existing words into the regex""" words = [] new_cache = defaultdict(list) - for chan, word in db.execute(select([table.c.chan, table.c.word])): + for chan, word in db.execute(select(table.c.chan, table.c.word)): new_cache[chan.casefold()].append(word) words.append(word) diff --git a/plugins/chain.py b/plugins/chain.py index 3bf21d77..1fe3c058 100644 --- a/plugins/chain.py +++ b/plugins/chain.py @@ -23,7 +23,7 @@ def load_cache(db): new_cache = {} for row in db.execute(commands.select()): - new_cache[row["hook"]] = row["allowed"] + new_cache[row.hook] = row.allowed allow_cache.clear() allow_cache.update(new_cache) diff --git a/plugins/core/autojoin.py b/plugins/core/autojoin.py index 9b4f8c64..8a02096e 100644 --- a/plugins/core/autojoin.py +++ b/plugins/core/autojoin.py @@ -26,7 +26,7 @@ def load_cache(db): new_cache = defaultdict(set) for row in db.execute(table.select()): - new_cache[row["conn"]].add(row["chan"]) + new_cache[row.conn].add(row.chan) with db_lock: chan_cache.clear() diff --git a/plugins/core/chan_key_db.py b/plugins/core/chan_key_db.py index 051e651e..bf3f1f3c 100644 --- a/plugins/core/chan_key_db.py +++ b/plugins/core/chan_key_db.py @@ -35,12 +35,12 @@ def load_keys(conn: IrcClient, db) -> None: """ Load channel keys to the client """ - query = select( - [table.c.chan, table.c.key], table.c.conn == conn.name.lower() + query = select(table.c.chan, table.c.key).where( + table.c.conn == conn.name.lower() ) conn.clear_channel_keys() for row in db.execute(query): - conn.set_channel_key(row["chan"], row["key"]) + conn.set_channel_key(row.chan, row.key) @hook.irc_raw("MODE") diff --git a/plugins/core/ignore.py b/plugins/core/ignore.py index 7e7b2e83..c794ee35 100644 --- a/plugins/core/ignore.py +++ b/plugins/core/ignore.py @@ -34,9 +34,9 @@ def load_cache(db): new_cache = [] for row in db.execute(table.select()): - conn = row["connection"] - chan = row["channel"] - mask = row["mask"] + conn = row.connection + chan = row.channel + mask = row.mask new_cache.append((conn, chan, mask)) ignore_cache.clear() @@ -182,8 +182,7 @@ def listignores(db, conn, chan): """- List all active ignores for the current channel""" rows = db.execute( - select( - [table.c.mask], + select(table.c.mask).where( and_( table.c.connection == conn.name.lower(), table.c.channel == chan.lower(), @@ -191,7 +190,7 @@ def listignores(db, conn, chan): ) ).fetchall() - out = "\n".join(row["mask"] for row in rows) + "\n" + out = "\n".join(row.mask for row in rows) + "\n" return web.paste(out) @@ -245,13 +244,13 @@ def list_all_ignores(db, conn, text): whereclause = and_(whereclause, table.c.channel == text.lower()) rows = db.execute( - select([table.c.channel, table.c.mask], whereclause) + select(table.c.channel, table.c.mask).where(whereclause) ).fetchall() ignores: Dict[str, List[str]] = OrderedDict() for row in rows: - ignores.setdefault(row["channel"], []).append(row["mask"]) + ignores.setdefault(row.channel, []).append(row.mask) out = "" for chan, masks in ignores.items(): diff --git a/plugins/core/optout.py b/plugins/core/optout.py index 7c9de0e0..d51aeb06 100644 --- a/plugins/core/optout.py +++ b/plugins/core/optout.py @@ -175,9 +175,7 @@ def clear_optout(db, conn, chan=None): def load_cache(db): new_cache = defaultdict(list) for row in db.execute(optout_table.select()): - new_cache[row["network"]].append( - OptOut(row["chan"], row["hook"], row["allow"]) - ) + new_cache[row.network].append(OptOut(row.chan, row.hook, row.allow)) for opts in new_cache.values(): opts.sort(reverse=True) diff --git a/plugins/core/regex_chans.py b/plugins/core/regex_chans.py index 96865a62..528d4bce 100644 --- a/plugins/core/regex_chans.py +++ b/plugins/core/regex_chans.py @@ -36,9 +36,9 @@ def load_cache(db): new_cache = {} for row in db.execute(table.select()): - conn = row["connection"] - chan = row["channel"] - status = row["status"] + conn = row.connection + chan = row.channel + status = row.status if status == ENABLED: value = True elif status == DISABLED: diff --git a/plugins/duckhunt.py b/plugins/duckhunt.py index 00d7e1fe..ece7f726 100644 --- a/plugins/duckhunt.py +++ b/plugins/duckhunt.py @@ -15,6 +15,7 @@ and_, desc, ) +from sqlalchemy.orm import Session from sqlalchemy.sql import select from cloudbot import hook @@ -136,14 +137,13 @@ def get_config(conn, field, default): @hook.on_start() -def load_optout(db): +def load_optout(db: Session): """load a list of channels duckhunt should be off in. Right now I am being lazy and not differentiating between networks this should be cleaned up later.""" new_data = defaultdict(list) chans = db.execute(optout.select()) for row in chans: - chan = row["chan"] - new_data[row["network"].casefold()].append(chan.casefold()) + new_data[row.network.casefold()].append(row.chan.casefold()) opt_out.clear() opt_out.update(new_data) @@ -157,11 +157,11 @@ def is_opt_out(network, chan): def load_status(db): rows = db.execute(status_table.select()) for row in rows: - net = row["network"] - chan = row["chan"] + net = row.network + chan = row.chan status = get_state_table(net, chan) - status.game_on = row["active"] - status.no_duck_kick = row["duck_kick"] + status.game_on = row.active + status.no_duck_kick = row.duck_kick if status.game_on: set_ducktime(chan, net) @@ -404,7 +404,7 @@ def dbupdate(nick, chan, db, conn, shoot, friend): def update_score(nick, chan, db, conn, shoot=0, friend=0): score = db.execute( - select([table.c.shot, table.c.befriend]) + select(table.c.shot, table.c.befriend) .where(table.c.network == conn.name) .where(table.c.chan == chan.lower()) .where(table.c.name == nick.lower()) @@ -544,8 +544,10 @@ def get_scores(db, score_type, network, chan=None): if chan is not None: clause = and_(clause, table.c.chan == chan.lower()) - query = select([table.c.name, table.c[score_type]], clause).order_by( - desc(table.c[score_type]) + query = ( + select(table.c.name, table.c[score_type]) + .where(clause) + .order_by(desc(table.c[score_type])) ) scores = db.execute(query).fetchall() @@ -728,8 +730,7 @@ def hunt_opt_out(text, chan, db, conn): if not is_opt_out(conn.name, channel): return f"Duck hunt is already enabled in {channel}." - delete = optout.delete(optout.c.chan == channel.lower()) - db.execute(delete) + db.execute(optout.delete().where(optout.c.chan == channel.lower())) db.commit() load_optout(db) @@ -746,13 +747,13 @@ def duck_merge(text, conn, db, message): return "Please specify two nicks for this command." oldnickscore = db.execute( - select([table.c.name, table.c.chan, table.c.shot, table.c.befriend]) + select(table.c.name, table.c.chan, table.c.shot, table.c.befriend) .where(table.c.network == conn.name) .where(table.c.name == oldnick) ).fetchall() newnickscore = db.execute( - select([table.c.name, table.c.chan, table.c.shot, table.c.befriend]) + select(table.c.name, table.c.chan, table.c.shot, table.c.befriend) .where(table.c.network == conn.name) .where(table.c.name == newnick) ).fetchall() @@ -767,16 +768,16 @@ def duck_merge(text, conn, db, message): new_chans = [] for row in newnickscore: - new_chans.append(row["chan"]) - chan_data = duckmerge[row["chan"]] - chan_data["shot"] = row["shot"] - chan_data["befriend"] = row["befriend"] + new_chans.append(row.chan) + chan_data = duckmerge[row.chan] + chan_data["shot"] = row.shot + chan_data["befriend"] = row.befriend for row in oldnickscore: - chan_name = row["chan"] + chan_name = row.chan chan_data1 = duckmerge[chan_name] - shot: int = row["shot"] - _friends: int = row["befriend"] + shot: int = row.shot + _friends: int = row.befriend chan_data1["shot"] += shot chan_data1["befriend"] += _friends total_kills += shot @@ -833,7 +834,8 @@ def ducks_user(text, nick, chan, conn, db, message): ducks: Dict[str, int] = defaultdict(int) scores = db.execute( select( - [table.c.name, table.c.chan, table.c.shot, table.c.befriend], + table.c.name, table.c.chan, table.c.shot, table.c.befriend + ).where( and_( table.c.network == conn.name, table.c.name == name, @@ -849,13 +851,13 @@ def ducks_user(text, nick, chan, conn, db, message): if scores: has_hunted_in_chan = False for row in scores: - if row["chan"].lower() == chan.lower(): + if row.chan.lower() == chan.lower(): has_hunted_in_chan = True - ducks["chankilled"] += row["shot"] - ducks["chanfriends"] += row["befriend"] + ducks["chankilled"] += row.shot + ducks["chanfriends"] += row.befriend - ducks["killed"] += row["shot"] - ducks["friend"] += row["befriend"] + ducks["killed"] += row.shot + ducks["friend"] += row.befriend ducks["chans"] += 1 # Check if the user has only participated in the hunt in this channel @@ -897,7 +899,8 @@ def duck_stats(chan, conn, db, message): """- Prints duck statistics for the entire channel and totals for the network.""" scores = db.execute( select( - [table.c.name, table.c.chan, table.c.shot, table.c.befriend], + table.c.name, table.c.chan, table.c.shot, table.c.befriend + ).where( table.c.network == conn.name, ) ).fetchall() diff --git a/plugins/factoids.py b/plugins/factoids.py index 4bea5518..9763a410 100644 --- a/plugins/factoids.py +++ b/plugins/factoids.py @@ -33,9 +33,9 @@ def load_cache(db): new_cache.clear() for row in db.execute(table.select()): # assign variables - chan = row["chan"] - word = row["word"] - data = row["data"] + chan = row.chan + word = row.word + data = row.data new_cache[chan][word] = data factoid_cache.clear() diff --git a/plugins/grab.py b/plugins/grab.py index 4e645498..5255ce8f 100644 --- a/plugins/grab.py +++ b/plugins/grab.py @@ -35,9 +35,9 @@ def load_cache(db): new_cache = grab_cache.copy() new_cache.clear() for row in db.execute(table.select().order_by(table.c.time)): - name = row["name"].lower() - quote = row["quote"] - chan = row["chan"] + name = row.name.lower() + quote = row.quote + chan = row.chan new_cache.setdefault(chan, {}).setdefault(name, []).append(quote) with cache_lock: diff --git a/plugins/herald.py b/plugins/herald.py index 1a5f6356..ee480f66 100644 --- a/plugins/herald.py +++ b/plugins/herald.py @@ -54,7 +54,7 @@ def load_cache(db): new_cache = herald_cache.copy() new_cache.clear() for row in db.execute(table.select()): - new_cache[row["chan"]][row["name"]] = row["quote"] + new_cache[row.chan][row.name] = row.quote herald_cache.clear() herald_cache.update(new_cache) diff --git a/plugins/hookup.py b/plugins/hookup.py index 889b507f..38c0ec34 100644 --- a/plugins/hookup.py +++ b/plugins/hookup.py @@ -36,10 +36,11 @@ def hookup(db, chan): times = time.time() - 86400 results = db.execute( - select( - [seen_table.c.name], + select(seen_table.c.name) + .where( and_(seen_table.c.chan == chan, seen_table.c.time > times), - ).order_by(seen_table.c.time) + ) + .order_by(seen_table.c.time) ).fetchall() if not results or len(results) < 2: diff --git a/plugins/horoscope.py b/plugins/horoscope.py index af08d46c..a8e4be5c 100644 --- a/plugins/horoscope.py +++ b/plugins/horoscope.py @@ -35,7 +35,7 @@ def get_sign(db, nick): row = db.execute( - select([table.c.sign]).where(table.c.nick == nick.lower()) + select(table.c.sign).where(table.c.nick == nick.lower()) ).fetchone() if not row: return None diff --git a/plugins/karma.py b/plugins/karma.py index 929477ac..78929d80 100644 --- a/plugins/karma.py +++ b/plugins/karma.py @@ -54,10 +54,10 @@ def update_score(nick, chan, thing, score, db): karma_table.c.chan == chan, karma_table.c.thing == thing.lower(), ) - karma = db.execute(select([karma_table.c.score]).where(clause)).fetchone() + karma = db.execute(select(karma_table.c.score).where(clause)).fetchone() query: Executable if karma: - score += karma["score"] + score += karma.score query = karma_table.update().values(score=score).where(clause) else: query = karma_table.insert().values( @@ -102,10 +102,11 @@ def pluspts(nick, chan, db): karma_table.c.score >= 0, ) query = ( - select([karma_table.c.thing, karma_table.c.score]) + select(karma_table.c.thing, karma_table.c.score) .where(clause) .order_by(karma_table.c.score.desc()) ) + likes = db.execute(query).fetchall() for like in likes: @@ -124,7 +125,7 @@ def minuspts(nick, chan, db): karma_table.c.score <= 0, ) query = ( - select([karma_table.c.thing, karma_table.c.score]) + select(karma_table.c.thing, karma_table.c.score) .where(clause) .order_by(karma_table.c.score) ) @@ -155,13 +156,13 @@ def points_cmd(text, chan, db): thing = "" if text.endswith(("-global", " global")): thing = text[:-7].strip() - query = select([karma_table.c.score]).where( + query = select(karma_table.c.score).where( karma_table.c.thing == thing.lower() ) else: text = text.strip() query = ( - select([karma_table.c.score]) + select(karma_table.c.score) .where(karma_table.c.thing == text.lower()) .where(karma_table.c.chan == chan) ) @@ -190,12 +191,12 @@ def points_cmd(text, chan, db): def parse_lookup(text, db, chan, name): if text in ("global", "-global"): items = db.execute( - select([karma_table.c.thing, karma_table.c.score]) + select(karma_table.c.thing, karma_table.c.score) ).fetchall() out = f"The {{}} most {name} things in all channels are: " else: items = db.execute( - select([karma_table.c.thing, karma_table.c.score]).where( + select(karma_table.c.thing, karma_table.c.score).where( karma_table.c.chan == chan ) ).fetchall() diff --git a/plugins/lastfm.py b/plugins/lastfm.py index 86d82b23..c12e184c 100644 --- a/plugins/lastfm.py +++ b/plugins/lastfm.py @@ -76,9 +76,7 @@ def filter_tags(tags, artist, limit=4): def load_cache(db): new_cache = {} for row in db.execute(table.select()): - nick = row["nick"] - account = row["acc"] - new_cache[nick] = account + new_cache[row.nick] = row.acc last_cache.clear() last_cache.update(new_cache) diff --git a/plugins/librefm.py b/plugins/librefm.py index 7c573913..b0a1377e 100644 --- a/plugins/librefm.py +++ b/plugins/librefm.py @@ -3,6 +3,7 @@ import requests from sqlalchemy import Column, PrimaryKeyConstraint, String, Table +from sqlalchemy.orm import Session from cloudbot import hook from cloudbot.util import database, timeformat, web @@ -35,12 +36,10 @@ def api_request(method, **params): @hook.on_start() -def load_cache(db): +def load_cache(db: Session): new_cache = [] for row in db.execute(table.select()): - nick = row["nick"] - account = row["acc"] - new_cache.append((nick, account)) + new_cache.append((row.nick, row.acc)) last_cache.clear() last_cache.extend(new_cache) diff --git a/plugins/notes.py b/plugins/notes.py index b00ec938..13009462 100644 --- a/plugins/notes.py +++ b/plugins/notes.py @@ -44,7 +44,7 @@ def where_user(server, user): def read_all_notes(db, server, user, show_deleted=False): - query = select([table.c.note_id, table.c.text, table.c.added]).where( + query = select(table.c.note_id, table.c.text, table.c.added).where( where_user(server, user) ) @@ -64,7 +64,7 @@ def delete_all_notes(db, server, user): def read_note(db, server, user, note_id): query = ( - select([table.c.note_id, table.c.text, table.c.added]) + select(table.c.note_id, table.c.text, table.c.added) .where(where_user(server, user)) .where(table.c.note_id == note_id) ) @@ -83,7 +83,7 @@ def delete_note(db, server, user, note_id): def add_note(db, server, user, text): - id_query = select([func.max(table.c.note_id).label("maxid")]).where( + id_query = select(func.max(table.c.note_id).label("maxid")).where( where_user(server, user) ) max_id = db.execute(id_query).scalar() diff --git a/plugins/profile.py b/plugins/profile.py index 46597b58..e91d5344 100644 --- a/plugins/profile.py +++ b/plugins/profile.py @@ -34,10 +34,10 @@ def load_cache(db): new_cache = profile_cache.copy() new_cache.clear() for row in db.execute(table.select().order_by(table.c.category)): - nick = row["nick"].lower() - cat = row["category"] - text = row["text"] - chan = row["chan"] + nick = row.nick.lower() + cat = row.category + text = row.text + chan = row.chan new_cache.setdefault(chan, {}).setdefault(nick, {})[cat] = text profile_cache.clear() diff --git a/plugins/quote.py b/plugins/quote.py index 32c4f58f..d5c21d27 100644 --- a/plugins/quote.py +++ b/plugins/quote.py @@ -14,6 +14,7 @@ select, ) from sqlalchemy.exc import IntegrityError +from sqlalchemy.orm import Session from sqlalchemy.types import REAL from cloudbot import hook @@ -33,7 +34,7 @@ @hook.on_start() -def migrate_table(db, logger): +def migrate_table(db: Session, logger): old_table = Table( "quote", database.metadata, @@ -57,19 +58,19 @@ def migrate_table(db, logger): return logger.info("Migrating quotes table") - qtable.drop(checkfirst=True) - qtable.create(checkfirst=True) + qtable.drop(checkfirst=True, bind=db.bind) + qtable.create(checkfirst=True, bind=db.bind) db.execute( qtable.insert().values( [ { - "chan": row["chan"], - "nick": row["nick"], - "add_nick": row["add_nick"], - "msg": row["msg"], - "time": row["time"], - "deleted": row["deleted"] in (1, "1", True), + "chan": row.chan, + "nick": row.nick, + "add_nick": row.add_nick, + "msg": row.msg, + "time": row.time, + "deleted": row.deleted in (1, "1", True), } for row in old_quotes ] @@ -79,7 +80,7 @@ def migrate_table(db, logger): logger.info("Migrated all quotes") db.commit() - old_table.drop() + old_table.drop(bind=db.bind) database.metadata.remove(old_table) @@ -133,7 +134,7 @@ def get_quote_by_nick(db, nick, num=False): """Returns a formatted quote from a nick, random or selected by number""" count_query = ( - select([func.count(qtable.c.msg)]) + select(func.count(qtable.c.msg)) .where(not_(qtable.c.deleted)) .where(qtable.c.nick == nick.lower()) ) @@ -145,7 +146,7 @@ def get_quote_by_nick(db, nick, num=False): return error_message query = ( - select([qtable.c.time, qtable.c.nick, qtable.c.msg]) + select(qtable.c.time, qtable.c.nick, qtable.c.msg) .where(not_(qtable.c.deleted)) .where(qtable.c.nick == nick.lower()) .order_by(qtable.c.time) @@ -159,7 +160,7 @@ def get_quote_by_nick(db, nick, num=False): def get_quote_by_nick_chan(db, chan, nick, num=False): """Returns a formatted quote from a nick in a channel, random or selected by number""" count_query = ( - select([func.count(qtable.c.msg)]) + select(func.count(qtable.c.msg)) .where(not_(qtable.c.deleted)) .where(qtable.c.chan == chan) .where(qtable.c.nick == nick.lower()) @@ -172,7 +173,7 @@ def get_quote_by_nick_chan(db, chan, nick, num=False): return error_message query = ( - select([qtable.c.time, qtable.c.nick, qtable.c.msg]) + select(qtable.c.time, qtable.c.nick, qtable.c.msg) .where(not_(qtable.c.deleted)) .where(qtable.c.chan == chan) .where(qtable.c.nick == nick.lower()) @@ -187,7 +188,7 @@ def get_quote_by_nick_chan(db, chan, nick, num=False): def get_quote_by_chan(db, chan, num=False): """Returns a formatted quote from a channel, random or selected by number""" count_query = ( - select([func.count(qtable.c.msg)]) + select(func.count(qtable.c.msg)) .where(not_(qtable.c.deleted)) .where(qtable.c.chan == chan) ) @@ -199,7 +200,7 @@ def get_quote_by_chan(db, chan, num=False): return error_message query = ( - select([qtable.c.time, qtable.c.nick, qtable.c.msg]) + select(qtable.c.time, qtable.c.nick, qtable.c.msg) .where(not_(qtable.c.deleted)) .where(qtable.c.chan == chan) .order_by(qtable.c.time) diff --git a/plugins/remind.py b/plugins/remind.py index b35e7590..c218d5be 100644 --- a/plugins/remind.py +++ b/plugins/remind.py @@ -99,11 +99,11 @@ def _load_cache_db(db): query = db.execute(table.select()) return [ ( - row["network"], - row["remind_time"], - row["added_time"], - row["added_user"], - row["message"], + row.network, + row.remind_time, + row.added_time, + row.added_user, + row.message, ) for row in query ] diff --git a/plugins/seen.py b/plugins/seen.py index 8281fffc..eeec0add 100644 --- a/plugins/seen.py +++ b/plugins/seen.py @@ -80,7 +80,7 @@ def seen(text, nick, chan, db, event): return "I can't look up that name, its impossible to use!" last_seen = db.execute( - select([table.c.name, table.c.time, table.c.quote]).where( + select(table.c.name, table.c.time, table.c.quote).where( and_(table.c.name == text.lower(), table.c.chan == chan) ) ).fetchone() diff --git a/plugins/tell.py b/plugins/tell.py index aa5c2fcf..370b5ec7 100644 --- a/plugins/tell.py +++ b/plugins/tell.py @@ -96,7 +96,7 @@ def migrate_tables(db): f"Can't migrate table {table.name} to {TellMessage.__tablename__}, destination already exists" ) - data = [dict(row) for row in db.execute(table.select())] + data = [dict(row) for row in db.execute(table.select()).mappings()] for item in data: item["conn"] = item.pop("connection") @@ -110,8 +110,8 @@ def migrate_tables(db): def load_cache(db): new_cache = [] for conn, target in db.execute( - select( - [TellMessage.conn, TellMessage.target], not_(TellMessage.is_read) + select(TellMessage.conn, TellMessage.target).where( + not_(TellMessage.is_read) ) ): new_cache.append((conn, target)) @@ -124,7 +124,7 @@ def load_cache(db): def load_disabled(db): new_cache = defaultdict(set) for row in db.execute(disable_table.select()): - new_cache[row["conn"]].add(row["target"].lower()) + new_cache[row.conn].add(row.target.lower()) disable_cache.clear() disable_cache.update(new_cache) @@ -135,7 +135,7 @@ def load_ignores(db): new_cache = ignore_cache.copy() new_cache.clear() for row in db.execute(ignore_table.select()): - new_cache[row["conn"].lower()][row["nick"].lower()].append(row["mask"]) + new_cache[row.conn.lower()][row.nick.lower()].append(row.mask) ignore_cache.clear() ignore_cache.update(new_cache) @@ -193,7 +193,7 @@ def list_disabled(db, conn): for row in db.execute( disable_table.select().where(disable_table.c.conn == conn.name.lower()) ): - yield (row["conn"], row["target"], row["setter"], row["set_at"].ctime()) + yield (row.conn, row.target, row.setter, row.set_at.ctime()) def add_ignore(db, conn, nick, mask, now=None): diff --git a/plugins/weather.py b/plugins/weather.py index 3fb472c6..bbfc279a 100644 --- a/plugins/weather.py +++ b/plugins/weather.py @@ -133,9 +133,7 @@ def add_location(nick, location, db): def load_cache(db): new_cache = [] for row in db.execute(table.select()): - nick = row["nick"] - location = row["loc"] - new_cache.append((nick, location)) + new_cache.append((row.nick, row.loc)) location_cache.clear() location_cache.extend(new_cache) diff --git a/tests/util/mock_db.py b/tests/util/mock_db.py index b15abeda..3c7a664c 100644 --- a/tests/util/mock_db.py +++ b/tests/util/mock_db.py @@ -10,7 +10,9 @@ class MockDB: def __init__(self, path="sqlite:///:memory:", force_session=False): self.engine = create_engine(path) if force_session: - self.session = scoped_session(sessionmaker(bind=self.engine)) + self.session = scoped_session( + sessionmaker(bind=self.engine, future=True) + ) else: self.session = Session