]> ToastFreeware Gitweb - philipp/winterrodeln/wrpylib.git/commitdiff
Update structure of sledrun_json_edit.
authorPhilipp Spitzer <philipp@spitzer.priv.at>
Thu, 21 Jul 2022 20:55:11 +0000 (22:55 +0200)
committerPhilipp Spitzer <philipp@spitzer.priv.at>
Thu, 21 Jul 2022 20:55:11 +0000 (22:55 +0200)
bots/sledrun_json_edit.py

index 648b1894a0e622c35b0088278c5de7f3062871fa..e98d8f589f8efd6d1ebdd14c6b3b8e6aefaaf057 100644 (file)
@@ -11,9 +11,10 @@ The following generators and filters are supported:
 """
 import json
 
+import jsonschema
 import pywikibot
 from jsonschema import validate
-from pywikibot import pagegenerators, Page
+from pywikibot import pagegenerators, Page, warning
 from pywikibot.bot import (
     AutomaticTWSummaryBot,
     ConfigParserBot,
@@ -25,7 +26,6 @@ from pywikibot.bot import (
 from wrpylib.json_tools import order_json_keys
 from wrpylib.wrmwmarkup import create_sledrun_wiki
 
-
 docuReplacements = {'&params;': pagegenerators.parameterHelp}
 
 
@@ -43,20 +43,35 @@ class SledrunFromJsonBot(
 
     def treat_page(self) -> None:
         """Load the given page, do some changes, and save it."""
-        if self.current_page.content_model != 'json':
+        if self.current_page.content_model != 'wikitext':
+            warning(f"The content model of {self.current_page.title()} is {self.current_page.content_model} "
+                    f"instead of wikitext")
+            return
+
+        sledrun_json_page = Page(self.site, self.current_page.title() + '/Rodelbahn.json')
+        if not sledrun_json_page.exists():
+            warning(f"{sledrun_json_page} does not exist.")
             return
-        content_json = json.loads(self.current_page.text)
 
-        # *here*, content_json can be processed
-        processed_json = content_json
+        if sledrun_json_page.content_model != 'json':
+            warning(f"The content model of {sledrun_json_page} is {sledrun_json_page.content_model} "
+                    f"instead of json")
+            return
+
+        sledrun_json = json.loads(sledrun_json_page.text)
+        sledrun_json_orig = json.loads(sledrun_json_page.text)
+        sledrun_json_orig_text = json.dumps(sledrun_json_orig, ensure_ascii=False, indent=4)
 
-        validate(instance=processed_json, schema=self.sledrun_schema)
-        processed_json_ordered = order_json_keys(processed_json, self.sledrun_schema)
-        assert processed_json_ordered == processed_json
-        text = json.dumps(processed_json_ordered, ensure_ascii=False, indent=4)
+        # *here*, sledrun_json can be processed
 
+        jsonschema.validate(instance=sledrun_json, schema=self.sledrun_schema)
+        sledrun_json_ordered = order_json_keys(sledrun_json, self.sledrun_schema)
+        assert sledrun_json_ordered == sledrun_json
+        if sledrun_json == sledrun_json_orig:
+            return
+        sledrun_json_text = json.dumps(sledrun_json_ordered, ensure_ascii=False, indent=4)
         summary = 'JSON Daten aktualisiert.'
-        self.put_current(text, summary=summary)
+        self.userPut(sledrun_json_page, sledrun_json_orig_text, sledrun_json_text, summary=summary, contentmodel='json')
 
 
 def main(*args: str) -> None: