diff options
author | 2020-11-14 22:56:36 +0100 | |
---|---|---|
committer | 2020-11-14 22:56:36 +0100 | |
commit | 4ed015aae21045876761284902c337da724b81eb (patch) | |
tree | 9bcc70435c2554c96de0c48376fae3af6b9816a7 | |
parent | Fix typo in GitHub workflow (diff) | |
parent | Remove make_persistent import from bot extensions (diff) |
Merge pull request #515 from python-discord/feature/non-persistence
Remove persistence requirements from Seasonalbot
-rw-r--r-- | bot/exts/evergreen/branding.py | 29 | ||||
-rw-r--r-- | bot/exts/halloween/candy_collection.py | 5 | ||||
-rw-r--r-- | bot/exts/halloween/hacktoberstats.py | 5 | ||||
-rw-r--r-- | bot/exts/halloween/monstersurvey.py | 1 | ||||
-rw-r--r-- | bot/utils/persist.py | 69 |
5 files changed, 19 insertions, 90 deletions
diff --git a/bot/exts/evergreen/branding.py b/bot/exts/evergreen/branding.py index 7e531011..b55c9b14 100644 --- a/bot/exts/evergreen/branding.py +++ b/bot/exts/evergreen/branding.py @@ -18,7 +18,8 @@ from bot.seasons import SeasonBase, get_all_seasons, get_current_season, get_sea from bot.utils import human_months from bot.utils.decorators import with_role from bot.utils.exceptions import BrandingError -from bot.utils.persist import make_persistent +# TODO: Implement substitute for current volume persistence requirements +# from bot.utils.persist import make_persistent log = logging.getLogger(__name__) @@ -156,13 +157,14 @@ class BrandingManager(commands.Cog): self.days_since_cycle = itertools.cycle([None]) - self.config_file = make_persistent(Path("bot", "resources", "evergreen", "branding.json")) - should_run = self._read_config()["daemon_active"] + # self.config_file = make_persistent(Path("bot", "resources", "evergreen", "branding.json")) - if should_run: - self.daemon = self.bot.loop.create_task(self._daemon_func()) - else: - self.daemon = None + # should_run = self._read_config()["daemon_active"] + + # if should_run: + # self.daemon = self.bot.loop.create_task(self._daemon_func()) + # else: + self.daemon = None @property def _daemon_running(self) -> bool: @@ -171,16 +173,11 @@ class BrandingManager(commands.Cog): def _read_config(self) -> t.Dict[str, bool]: """Read and return persistent config file.""" - with self.config_file.open("r", encoding="utf8") as persistent_file: - return json.load(persistent_file) + raise NotImplementedError("read_config functionality requires mounting a persistent volume.") def _write_config(self, key: str, value: bool) -> None: """Write a `key`, `value` pair to persistent config file.""" - current_config = self._read_config() - current_config[key] = value - - with self.config_file.open("w", encoding="utf8") as persistent_file: - json.dump(current_config, persistent_file) + raise NotImplementedError("write_config functionality requires mounting a persistent volume.") async def _daemon_func(self) -> None: """ @@ -513,7 +510,7 @@ class BrandingManager(commands.Cog): await ctx.send(embed=response) - @daemon_group.command(name="start") + @daemon_group.command(name="start", enabled=False) async def daemon_start(self, ctx: commands.Context) -> None: """If the daemon isn't running, start it.""" if self._daemon_running: @@ -525,7 +522,7 @@ class BrandingManager(commands.Cog): response = discord.Embed(description=f"Daemon started {Emojis.ok_hand}", colour=Colours.soft_green) await ctx.send(embed=response) - @daemon_group.command(name="stop") + @daemon_group.command(name="stop", enabled=False) async def daemon_stop(self, ctx: commands.Context) -> None: """If the daemon is running, stop it.""" if not self._daemon_running: diff --git a/bot/exts/halloween/candy_collection.py b/bot/exts/halloween/candy_collection.py index a862e1af..7bdc6ef0 100644 --- a/bot/exts/halloween/candy_collection.py +++ b/bot/exts/halloween/candy_collection.py @@ -9,7 +9,9 @@ from discord.ext import commands from bot.constants import Channels, Month from bot.utils.decorators import in_month -from bot.utils.persist import make_persistent + +# TODO: Implement substitutes for volume-persistent methods. +# from bot.utils.persist import make_persistent log = logging.getLogger(__name__) @@ -197,4 +199,3 @@ class CandyCollection(commands.Cog): def setup(bot: commands.Bot) -> None: """Candy Collection game Cog load.""" - bot.add_cog(CandyCollection(bot)) diff --git a/bot/exts/halloween/hacktoberstats.py b/bot/exts/halloween/hacktoberstats.py index d2762513..beff86e3 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -12,7 +12,9 @@ from discord.ext import commands from bot.constants import Channels, Month, Tokens, WHITELISTED_CHANNELS from bot.utils.decorators import in_month, override_in_channel -from bot.utils.persist import make_persistent + +# TODO: Implement substitutes for volume-persistent methods. +# from bot.utils.persist import make_persistent log = logging.getLogger(__name__) @@ -489,4 +491,3 @@ class HacktoberStats(commands.Cog): def setup(bot: commands.Bot) -> None: """Hacktoberstats Cog load.""" - bot.add_cog(HacktoberStats(bot)) diff --git a/bot/exts/halloween/monstersurvey.py b/bot/exts/halloween/monstersurvey.py index 7b1a1e84..80196825 100644 --- a/bot/exts/halloween/monstersurvey.py +++ b/bot/exts/halloween/monstersurvey.py @@ -202,4 +202,3 @@ class MonsterSurvey(Cog): def setup(bot: Bot) -> None: """Monster survey Cog load.""" - bot.add_cog(MonsterSurvey(bot)) diff --git a/bot/utils/persist.py b/bot/utils/persist.py deleted file mode 100644 index 1e178569..00000000 --- a/bot/utils/persist.py +++ /dev/null @@ -1,69 +0,0 @@ -import sqlite3 -from pathlib import Path -from shutil import copyfile - -from bot.exts import get_package_names - -DIRECTORY = Path("data") # directory that has a persistent volume mapped to it - - -def make_persistent(file_path: Path) -> Path: - """ - Copy datafile at the provided file_path to the persistent data directory. - - A persistent data file is needed by some features in order to not lose data - after bot rebuilds. - - This function will ensure that a clean data file with default schema, - structure or data is copied over to the persistent volume before returning - the path to this new persistent version of the file. - - If the persistent file already exists, it won't be overwritten with the - clean default file, just returning the Path instead to the existing file. - - Note: Avoid using the same file name as other features in the same seasons - as otherwise only one datafile can be persistent and will be returned for - both cases. - - Ensure that all open files are using explicit appropriate encoding to avoid - encoding errors from diffent OS systems. - - Example Usage: - >>> import json - >>> template_datafile = Path("bot", "resources", "evergreen", "myfile.json") - >>> path_to_persistent_file = make_persistent(template_datafile) - >>> print(path_to_persistent_file) - data/evergreen/myfile.json - >>> with path_to_persistent_file.open("w+", encoding="utf8") as f: - >>> data = json.load(f) - """ - # ensure the persistent data directory exists - DIRECTORY.mkdir(exist_ok=True) - - if not file_path.is_file(): - raise OSError(f"File not found at {file_path}.") - - # detect season in datafile path for assigning to subdirectory - season = next((s for s in get_package_names() if s in file_path.parts), None) - - if season: - # make sure subdirectory exists first - subdirectory = Path(DIRECTORY, season) - subdirectory.mkdir(exist_ok=True) - - persistent_path = Path(subdirectory, file_path.name) - - else: - persistent_path = Path(DIRECTORY, file_path.name) - - # copy base/template datafile to persistent directory - if not persistent_path.exists(): - copyfile(file_path, persistent_path) - - return persistent_path - - -def sqlite(db_path: Path) -> sqlite3.Connection: - """Copy sqlite file to the persistent data directory and return an open connection.""" - persistent_path = make_persistent(db_path) - return sqlite3.connect(persistent_path) |