Skip to content

Commit

Permalink
Use async_call in regular plugins for Python 3.7+ compat
Browse files Browse the repository at this point in the history
  • Loading branch information
daboross committed Dec 10, 2018
1 parent d237805 commit d059783
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 38 deletions.
34 changes: 17 additions & 17 deletions plugins/factoids.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ def _load_cache_db(db):

@asyncio.coroutine
@hook.on_start()
def load_cache(async, db):
def load_cache(async_call, db):
"""
:type db: sqlalchemy.orm.Session
"""
global factoid_cache
factoid_cache = {}
for word, data in (yield from async(_load_cache_db, db)):
for word, data in (yield from async_call(_load_cache_db, db)):
# nick = row["nick"]
factoid_cache[word] = data # we might want (data, nick) sometime later


@asyncio.coroutine
def add_factoid(async, db, word, data, nick):
def add_factoid(async_call, db, word, data, nick):
"""
:type db: sqlalchemy.orm.Session
:type word: str
Expand All @@ -50,28 +50,28 @@ def add_factoid(async, db, word, data, nick):
"""
if word in factoid_cache:
# if we have a set value, update
yield from async(db.execute, table.update().values(data=data, nick=nick).where(table.c.word == word))
yield from async_call(db.execute, table.update().values(data=data, nick=nick).where(table.c.word == word))
else:
# otherwise, insert
yield from async(db.execute, table.insert().values(word=word, data=data, nick=nick))
yield from async(db.commit)
yield from load_cache(async, db)
yield from async_call(db.execute, table.insert().values(word=word, data=data, nick=nick))
yield from async_call(db.commit)
yield from load_cache(async_call, db)


@asyncio.coroutine
def del_factoid(async, db, word):
def del_factoid(async_call, db, word):
"""
:type db: sqlalchemy.orm.Session
:type word: str
"""
yield from async(db.execute, table.delete().where(table.c.word == word))
yield from async(db.commit)
yield from load_cache(async, db)
yield from async_call(db.execute, table.delete().where(table.c.word == word))
yield from async_call(db.commit)
yield from load_cache(async_call, db)


@asyncio.coroutine
@hook.command("r", "remember", permissions=["addfactoid"])
def remember(text, nick, db, notice, async):
def remember(text, nick, db, notice, async_call):
"""<word> [+]<data> - remembers <data> with <word> - add + to <data> to append"""

try:
Expand All @@ -97,18 +97,18 @@ def remember(text, nick, db, notice, async):
if old_data:
notice('Previous data was \x02{}\x02'.format(old_data))

yield from add_factoid(async, db, word, data, nick)
yield from add_factoid(async_call, db, word, data, nick)


@asyncio.coroutine
@hook.command("f", "forget", permissions=["delfactoid"])
def forget(text, db, async, notice):
def forget(text, db, async_call, notice):
"""<word> - forgets previously remembered <word>"""

data = factoid_cache.get(text.lower())

if data:
yield from del_factoid(async, db, text)
yield from del_factoid(async_call, db, text)
notice('"{}" has been forgotten.'.format(data.replace('`', "'")))
return
else:
Expand All @@ -134,7 +134,7 @@ def info(text, notice):

@asyncio.coroutine
@hook.regex(factoid_re)
def factoid(match, async, event, message, action):
def factoid(match, async_call, event, message, action):
"""<word> - shows what data is associated with <word>"""

# split up the input
Expand All @@ -154,7 +154,7 @@ def factoid(match, async, event, message, action):
variables = 'input="""{}"""; nick="{}"; chan="{}"; bot_nick="{}";'.format(arguments.replace('"', '\\"'),
event.nick, event.chan,
event.conn.nick)
result = yield from async(web.pyeval, variables + code)
result = yield from async_call(web.pyeval, variables + code)
else:
result = data

Expand Down
42 changes: 21 additions & 21 deletions plugins/remind.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,26 @@


@asyncio.coroutine
def delete_reminder(async, db, network, remind_time, user):
def delete_reminder(async_call, db, network, remind_time, user):
query = table.delete() \
.where(table.c.network == network.lower()) \
.where(table.c.remind_time == remind_time) \
.where(table.c.added_user == user.lower())
yield from async(db.execute, query)
yield from async(db.commit)
yield from async_call(db.execute, query)
yield from async_call(db.commit)


@asyncio.coroutine
def delete_all(async, db, network, user):
def delete_all(async_call, db, network, user):
query = table.delete() \
.where(table.c.network == network.lower()) \
.where(table.c.added_user == user.lower())
yield from async(db.execute, query)
yield from async(db.commit)
yield from async_call(db.execute, query)
yield from async_call(db.commit)


@asyncio.coroutine
def add_reminder(async, db, network, added_user, added_chan, message, remind_time, added_time):
def add_reminder(async_call, db, network, added_user, added_chan, message, remind_time, added_time):
query = table.insert().values(
network=network.lower(),
added_user=added_user.lower(),
Expand All @@ -68,17 +68,17 @@ def add_reminder(async, db, network, added_user, added_chan, message, remind_tim
message=message,
remind_time=remind_time
)
yield from async(db.execute, query)
yield from async(db.commit)
yield from async_call(db.execute, query)
yield from async_call(db.commit)


@asyncio.coroutine
@hook.on_start()
def load_cache(async, db):
def load_cache(async_call, db):
global reminder_cache
reminder_cache = []

for network, remind_time, added_time, user, message in (yield from async(_load_cache_db, db)):
for network, remind_time, added_time, user, message in (yield from async_call(_load_cache_db, db)):
reminder_cache.append((network, remind_time, added_time, user, message))


Expand All @@ -89,16 +89,16 @@ def _load_cache_db(db):

@asyncio.coroutine
@hook.periodic(30, initial_interval=30)
def check_reminders(bot, async, db):
def check_reminders(bot, async_call, db):
current_time = datetime.now()

for reminder in reminder_cache:
network, remind_time, added_time, user, message = reminder
if remind_time <= current_time:
if network not in bot.connections:
# connection is invalid
yield from add_reminder(async, db, network, remind_time, user)
yield from load_cache(async, db)
yield from add_reminder(async_call, db, network, remind_time, user)
yield from load_cache(async_call, db)
continue

conn = bot.connections[network]
Expand All @@ -119,13 +119,13 @@ def check_reminders(bot, async, db):
" it seems I was unable to deliver it on time)".format(late_time)
conn.message(user, colors.parse(late))

yield from delete_reminder(async, db, network, remind_time, user)
yield from load_cache(async, db)
yield from delete_reminder(async_call, db, network, remind_time, user)
yield from load_cache(async_call, db)


@asyncio.coroutine
@hook.command('remind', 'reminder')
def remind(text, nick, chan, db, conn, notice, async):
def remind(text, nick, chan, db, conn, notice, async_call):
"""<1 minute, 30 seconds>: <do task> -- reminds you to <do task> in <1 minute, 30 seconds>"""

count = len([x for x in reminder_cache if x[0] == conn.name and x[3] == nick.lower()])
Expand All @@ -134,8 +134,8 @@ def remind(text, nick, chan, db, conn, notice, async):
if count == 0:
return "You have no reminders to delete."

yield from delete_all(async, db, conn.name, nick)
yield from load_cache(async, db)
yield from delete_all(async_call, db, conn.name, nick)
yield from load_cache(async_call, db)
return "Deleted all ({}) reminders for {}!".format(count, nick)

# split the input on the first ":"
Expand Down Expand Up @@ -171,8 +171,8 @@ def remind(text, nick, chan, db, conn, notice, async):
return "I can't remind you in the past!"

# finally, add the reminder and send a confirmation message
yield from add_reminder(async, db, conn.name, nick, chan, message, remind_time, current_time)
yield from load_cache(async, db)
yield from add_reminder(async_call, db, conn.name, nick, chan, message, remind_time, current_time)
yield from load_cache(async_call, db)

remind_text = format_time(seconds, count=2)
output = "Alright, I'll remind you \"{}\" in $(b){}$(clear)!".format(message, remind_text)
Expand Down

0 comments on commit d059783

Please sign in to comment.