Skip to content

Commit

Permalink
remind, tell: Use config name in fake .db files
Browse files Browse the repository at this point in the history
Instead of using the bot's nick and configured host (to connect to),
let's use the config `basename`. This matches the default SQLite DB
filename and the names used for Sopel's log files.
  • Loading branch information
dgw committed Nov 14, 2019
1 parent a33b024 commit 0063471
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
26 changes: 25 additions & 1 deletion sopel/modules/remind.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""
remind.py - Sopel Reminder Module
Copyright 2011, Sean B. Palmer, inamidst.com
Copyright 2019, dgw, technobabbl.es
Licensed under the Eiffel Forum License 2.
https://sopel.chat
Expand All @@ -11,6 +12,7 @@
import codecs
import collections
from datetime import datetime
import logging
import os
import re
import time
Expand All @@ -21,6 +23,9 @@
from sopel.tools.time import get_timezone, format_time, validate_timezone


LOGGER = logging.getLogger(__name__)


def get_filename(bot):
"""Get the remind database's filename
Expand All @@ -32,7 +37,7 @@ def get_filename(bot):
The remind database filename is based on the bot's nick and its
configured ``core.host``, and it is located in the ``bot``'s ``homedir``.
"""
name = bot.nick + '-' + bot.config.core.host + '.reminders.db'
name = bot.config.basename + '.reminders.db'
return os.path.join(bot.config.core.homedir, name)


Expand Down Expand Up @@ -123,6 +128,25 @@ def create_reminder(bot, trigger, duration, message):
def setup(bot):
"""Load the remind database"""
bot.rfn = get_filename(bot)

# Pre-7.0 migration logic. Remove in 8.0 or 9.0.
old = bot.nick + '-' + bot.config.core.host + '.reminders.db'
old = os.path.join(bot.config.core.homedir, old)
if os.path.isfile(old):
LOGGER.info("Attempting to migrate old 'remind' database {}..."
.format(old))
try:
os.rename(old, bot.rfn)
except OSError:
LOGGER.error("Migration failed!")
LOGGER.error("Old filename: {}".format(old))
LOGGER.error("New filename: {}".format(bot.rfn))
LOGGER.error(
"See https://sopel.chat/usage/installing/upgrading-to-sopel-7/#reminder-db-migration")
else:
LOGGER.info("Migration finished!")
# End migration logic

bot.rdb = load_database(bot.rfn)


Expand Down
27 changes: 25 additions & 2 deletions sopel/modules/tell.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
"""
tell.py - Sopel Tell and Ask Module
Copyright 2008, Sean B. Palmer, inamidst.com
Copyright 2019, dgw, technobabbl.es
Licensed under the Eiffel Forum License 2.
https://sopel.chat
"""
from __future__ import unicode_literals, absolute_import, print_function, division

import io
import logging
import os
import time
import threading
Expand All @@ -20,6 +22,9 @@
from sopel.tools.time import get_timezone, format_time


LOGGER = logging.getLogger(__name__)


class TellSection(StaticSection):
use_private_reminder = ValidatedAttribute(
'use_private_reminder', parse=bool, default=False)
Expand Down Expand Up @@ -51,7 +56,7 @@ def load_reminders(filename):
"""Load tell/ask reminders from a ``filename``.
:param str filename: path to the tell/ask reminders file
:return: a dict with the tell/asl reminders
:return: a dict with the tell/ask reminders
:rtype: dict
"""
result = defaultdict(list)
Expand Down Expand Up @@ -84,9 +89,27 @@ def dump_reminders(filename, data):

def setup(bot):
bot.config.define_section('tell', TellSection)
fn = bot.nick + '-' + bot.config.core.host + '.tell.db'
fn = bot.config.basename + '.tell.db'
bot.tell_filename = os.path.join(bot.config.core.homedir, fn)

# Pre-7.0 migration logic. Remove in 8.0 or 9.0.
old = bot.nick + '-' + bot.config.core.host + '.tell.db'
old = os.path.join(bot.config.core.homedir, old)
if os.path.isfile(old):
LOGGER.info("Attempting to migrate old 'tell' database {}..."
.format(old))
try:
os.rename(old, bot.tell_filename)
except OSError:
LOGGER.error("Migration failed!")
LOGGER.error("Old filename: {}".format(old))
LOGGER.error("New filename: {}".format(bot.tell_filename))
LOGGER.error(
"See https://sopel.chat/usage/installing/upgrading-to-sopel-7/#reminder-db-migration")
else:
LOGGER.info("Migration finished!")
# End migration logic

if not os.path.exists(bot.tell_filename):
with io.open(bot.tell_filename, 'w', encoding='utf-8') as fd:
# if we can't open/write into the file, the tell plugin can't work
Expand Down
3 changes: 2 additions & 1 deletion test/modules/test_modules_remind.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
@pytest.fixture
def sopel():
bot = test_tools.MockSopel('Sopel')
bot.config.basename = 'default'
bot.config.core.owner = 'Admin'
bot.config.core.host = 'chat.freenode.net'
return bot
Expand Down Expand Up @@ -316,7 +317,7 @@ def test_get_filename(sopel):
filename = remind.get_filename(sopel)
assert filename == os.path.join(
sopel.config.core.homedir,
'Sopel-chat.freenode.net.reminders.db')
'default.reminders.db')


def test_load_database_empty(tmpdir):
Expand Down

0 comments on commit 0063471

Please sign in to comment.