From 36c4ba2b7cd9fbbabfa5f578a46b64b596e68dd1 Mon Sep 17 00:00:00 2001 From: MarkusRost <2701034-MarkusRost@users.noreply.gitlab.com> Date: Tue, 10 Jan 2023 13:56:18 +0100 Subject: [PATCH] migrate config from falsy values to null --- docs/API spec.md | 2 +- scripts/configbuilder.py | 4 ++-- src/discord/message.py | 4 ++-- src/migrations/__init__.py | 2 +- src/migrations/falsytypes.py | 40 ++++++++++++++++++++++++++++++++++++ src/misc.py | 2 +- src/wiki.py | 4 ++-- 7 files changed, 49 insertions(+), 9 deletions(-) create mode 100644 src/migrations/falsytypes.py diff --git a/docs/API spec.md b/docs/API spec.md index 4c0aed8..4c12129 100644 --- a/docs/API spec.md +++ b/docs/API spec.md @@ -141,7 +141,7 @@ _DiscordMessage is a class taking care of creation of Discord messages that can DiscordMessage object when created with message_type == embed will take all assignments and reads using object[key] as ones reading/setting the actual embed object. DiscordMessage consists of the following: - `__init__(message_type: str, event_type: str, webhook_url: str, content=None)` – constructor which takes message type (can be either `embed` or `compact`), event_type (for example `protect/protect`), webhook_url (full URL of webhook message is intended to be sent to), content optional parameter used in compact messages as main body -- `set_author(name, url, icon_url="")` – a method that can be used to set username, URL to their profile and optionally an icon for the embed +- `set_author(name, url, icon_url=None)` – a method that can be used to set username, URL to their profile and optionally an icon for the embed - `add_field(name, value, inline=False)` – a method to add a field with given name and value, optional inline argument can be set if field should be inline - `set_avatar(url)` – sets avatar for WEBHOOK MESSAGE (not to be confused with actual embed) - `set_name(name)` – sets name for WEBHOOK MESSAGE diff --git a/scripts/configbuilder.py b/scripts/configbuilder.py index 27ce7c8..2bcae97 100644 --- a/scripts/configbuilder.py +++ b/scripts/configbuilder.py @@ -282,7 +282,7 @@ class AdvancedSettings: if settings["show_updown_messages"]: option = default_or_custom( input("Provide a link for a custom webhook avatar when the wiki goes DOWN. (default: no avatar)\n"), - "") + None) try: if option: response = requests.head(option, timeout=10.0) @@ -295,7 +295,7 @@ class AdvancedSettings: option = default_or_custom( input( "Provide a link for a custom webhook avatar when the connection to the wiki is RESTORED. (default: no avatar)\n"), - "") + None) try: if option: response = requests.head(option, timeout=10.0) diff --git a/src/discord/message.py b/src/discord/message.py index e8872db..af89410 100644 --- a/src/discord/message.py +++ b/src/discord/message.py @@ -24,7 +24,7 @@ from src.configloader import settings class DiscordMessage: """A class defining a typical Discord JSON representation of webhook payload.""" def __init__(self, message_type: str, event_type: str, webhook_url: str, content=None): - self.webhook_object = dict(allowed_mentions={"parse": []}, avatar_url=settings["avatars"].get(message_type, "")) + self.webhook_object = dict(allowed_mentions={"parse": []}, avatar_url=settings["avatars"].get(message_type, None)) self.webhook_url = webhook_url if message_type == "embed": @@ -78,7 +78,7 @@ class DiscordMessage: if len(self.embed["title"]) > 254: self.embed["title"] = self.embed["title"][0:253] + "…" - def set_author(self, name, url, icon_url=""): + def set_author(self, name, url, icon_url=None): self.embed["author"]["name"] = name self.embed["author"]["url"] = url self.embed["author"]["icon_url"] = icon_url diff --git a/src/migrations/__init__.py b/src/migrations/__init__.py index 8e786d0..129d8a8 100644 --- a/src/migrations/__init__.py +++ b/src/migrations/__init__.py @@ -1 +1 @@ -__all__ = ["11311"] +__all__ = ["11311", "falsytypes"] diff --git a/src/migrations/falsytypes.py b/src/migrations/falsytypes.py new file mode 100644 index 0000000..521647a --- /dev/null +++ b/src/migrations/falsytypes.py @@ -0,0 +1,40 @@ +from src.configloader import settings, load_settings +import logging +import shutil +import time +import json +import sys + +logger = logging.getLogger("rcgcdw.migrations.falsytypes") +new_settings = settings.copy() + +def run(): + change = False + try: + if "avatars" in new_settings: + avatars = new_settings["avatars"] + for key, value in avatars.items(): + if not value and value is not None: + new_settings["avatars"][key] = None + change = True + if "event_appearance" in new_settings: + events = new_settings["event_appearance"] + for key, value in events.items(): + if not value.get("icon", None) and value.get("icon", None) is not None: + new_settings["event_appearance"][key]["icon"] = None + change = True + except KeyError: + logger.exception("Failed to migrate falsy types.") + sys.exit(1) + if change: + logger.info("Running migration falsytypes") + shutil.copy("settings.json", "settings.json.{}.bak".format(int(time.time()))) + with open("settings.json", "w", encoding="utf-8") as new_write: + new_write.write(json.dumps(new_settings, indent=4)) + load_settings() + logger.info("Migration falsytypes has been successful.") + else: + logger.debug("Ignoring migration falsytypes") + + +run() diff --git a/src/misc.py b/src/misc.py index 383e183..ec52e9b 100644 --- a/src/misc.py +++ b/src/misc.py @@ -80,7 +80,7 @@ class DataFile: misc_logger.info("The data file could not be found. Generating a new one...") if not command_args.nowelcome: send_simple("welcome", _("RcGcDw is now running and checking {wiki}.").format(wiki=settings["wikiname"]), - _("Welcome"), settings["avatars"].get("welcome", "")) + _("Welcome"), settings["avatars"].get("welcome", None)) return data_template def save_datafile(self): diff --git a/src/wiki.py b/src/wiki.py index 9729aff..a8892f1 100644 --- a/src/wiki.py +++ b/src/wiki.py @@ -402,7 +402,7 @@ class Wiki(object): else: if (time.time() - self.last_downtime) > 1800 and self.check_connection(): # check if last downtime happened within 30 minutes, if yes, don't send a message send_simple("down_detector", _("{wiki} seems to be down or unreachable.").format(wiki=settings["wikiname"]), - _("Connection status"), settings["avatars"]["connection_failed"]) + _("Connection status"), settings["avatars"].get("connection_failed", None)) self.last_downtime = time.time() self.streak = 0 else: @@ -414,7 +414,7 @@ class Wiki(object): self.streak = -1 send_simple("down_detector", _("Connection to {wiki} seems to be stable now.").format( wiki=settings["wikiname"]), - _("Connection status"), settings["avatars"]["connection_restored"]) + _("Connection status"), settings["avatars"].get("connection_restored", None)) def clear_cache(self): self.map_ips = {}