diff options
-rw-r--r-- | bot/seasons/halloween/hacktoberstats.py | 4 | ||||
-rw-r--r-- | bot/utils/persist.py | 24 |
2 files changed, 27 insertions, 1 deletions
diff --git a/bot/seasons/halloween/hacktoberstats.py b/bot/seasons/halloween/hacktoberstats.py index 0f513953..9dfb20bd 100644 --- a/bot/seasons/halloween/hacktoberstats.py +++ b/bot/seasons/halloween/hacktoberstats.py @@ -10,6 +10,8 @@ import aiohttp import discord from discord.ext import commands +from bot.utils.persist import datafile + log = logging.getLogger(__name__) CURRENT_YEAR = datetime.now().year # Used to construct GH API query @@ -21,7 +23,7 @@ class HacktoberStats(commands.Cog): def __init__(self, bot: commands.Bot): self.bot = bot - self.link_json = Path("bot/resources/github_links.json") + self.link_json = datafile(Path("bot", "resources", "halloween", "github_links.json")) self.linked_accounts = self.load_linked_users() @commands.group(name="hacktoberstats", aliases=("hackstats",), invoke_without_command=True) diff --git a/bot/utils/persist.py b/bot/utils/persist.py new file mode 100644 index 00000000..ec6f306a --- /dev/null +++ b/bot/utils/persist.py @@ -0,0 +1,24 @@ +import sqlite3 +from pathlib import Path +from shutil import copyfile + +DIRECTORY = Path("data") # directory that has a persistent volume mapped to it + + +def datafile(file_path: Path) -> Path: + """Copy datafile at the provided file_path to the persistent data directory.""" + if not file_path.exists(): + raise OSError(f"File not found at {file_path}.") + + persistant_path = Path(DIRECTORY, file_path.name) + + if not persistant_path.exists(): + copyfile(file_path, persistant_path) + + return persistant_path + + +def sqlite(db_path: Path) -> sqlite3.Connection: + """Copy sqlite file to the persistent data directory and return an open connection.""" + persistant_path = datafile(db_path) + return sqlite3.connect(persistant_path) |