From 293a1b05d24af0e4aa371964ba4147a52c1493b6 Mon Sep 17 00:00:00 2001 From: Kovachev <86124720+KovachevBot@users.noreply.github.com> Date: Mon, 29 May 2023 16:47:28 +0100 Subject: [PATCH] Add backup functionality --- ja-yomi/ja-yomi-remove.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/ja-yomi/ja-yomi-remove.py b/ja-yomi/ja-yomi-remove.py index b089b0a..35277e2 100644 --- a/ja-yomi/ja-yomi-remove.py +++ b/ja-yomi/ja-yomi-remove.py @@ -1,12 +1,13 @@ from typing import Generator import pywikibot import regex as re +import os JA_YOMI_TRACKING_PAGE = "tracking/ja-pron/yomi" REMOVE_YOMI_PATTERN = re.compile(r"({{ja-pron(?:\|[^\|]+?=[^\|]+?|\|[^\|]+)*?)\|(?:y|yomi)=(?:o|on|go|goon|ko|kan|kanon|so|soon|to|toon|ky|kanyo|kanyoon|k|kun|j|ju|y|yu|i|irr|irreg|irregular)((?:\|[^\|]+?=[^\|]+?|\|[^\|]+)*}})") JA_PRON_PATTERN = re.compile(r"{{ja-pron(?:\|[^\|]+?=[^\|]+?|\|[^\|]+)*}}") - +BACKUP_PATH = "ja-yomi-backup" def get_yomi_pages() -> Generator[pywikibot.Page, None, None]: SITE = pywikibot.Site("en", "wiktionary") @@ -30,6 +31,16 @@ def remove_yomi_from_page(page: pywikibot.Page) -> None: page.text = new_text +def backup_page(page: pywikibot.Page) -> None: + """ + Copy the contents of the page to local storage for backup in case there is a problem + with the script later; this will allow the error to be automatically corrected at that time. + """ + os.makedirs(BACKUP_PATH, exist_ok=True) + with open(os.path.join(BACKUP_PATH, page.title()), mode="w", encoding="utf-8") as f: + f.write(page.text) + + def template_argument_counts_accord(previous_text: str, current_text: str) -> bool: """ Gets the previous and current renditions of the wikitext, @@ -62,10 +73,12 @@ def main(): return original_text = page.text - print(f"Removing yomi from {page.title()}") + print(f"Backing up {page.title()}...") + backup_page(page) + print(f"Removing yomi from {page.title()}...") remove_yomi_from_page(page) assert template_argument_counts_accord(original_text, page.text) - page.save("Removed deprecated yomi/y parameters from {{ja-pron}} (automated task)", minor=True) + page.save("Removed deprecated yomi/y parameters from {{ja-pron}} (automated task)", minor=True, botflag=True) if __name__ == "__main__":