import json
import pywikibot
+from jsonschema import validate
from pywikibot import pagegenerators, Page
from pywikibot.bot import (
AutomaticTWSummaryBot,
SingleSiteBot,
)
+from wrpylib.json_tools import order_json_keys
from wrpylib.wrmwmarkup import create_sledrun_wiki
NoRedirectPageBot,
AutomaticTWSummaryBot,
):
+ def setup(self) -> None:
+ schema = Page(self.site, 'Winterrodeln:Datenschema/Rodelbahn/V1.json')
+ assert schema.content_model == 'json'
+ self.sledrun_schema = json.loads(schema.text)
+
def treat_page(self) -> None:
"""Load the given page, do some changes, and save it."""
if self.current_page.content_model != 'json':
return
content_json = json.loads(self.current_page.text)
+
+ # *here*, content_json can be processed
processed_json = content_json
- text = json.dumps(processed_json, 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)
+
summary = 'JSON Daten aktualisiert.'
self.put_current(text, summary=summary)