#!/usr/bin/python """ User script for pywikibot (https://gerrit.wikimedia.org/r/pywikibot/core.git), tested with version 6.6.1. Put it in directory scripts/userscripts. Edit JSON associated with sledruns. The following generators and filters are supported: ¶ms; """ import json import jsonschema from jsonschema import validate import pywikibot from pywikibot import pagegenerators, Page from pywikibot.bot import ( AutomaticTWSummaryBot, ConfigParserBot, ExistingPageBot, NoRedirectPageBot, SingleSiteBot, ) from wrpylib.json_tools import order_json_keys docuReplacements = {'¶ms;': pagegenerators.parameterHelp} class SledrunFromJsonBot( SingleSiteBot, ConfigParserBot, ExistingPageBot, 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.""" content_json = json.loads(self.current_page.text) try: validate(instance=content_json, schema=self.sledrun_schema) content_json_ordered = order_json_keys(content_json, self.sledrun_schema) assert content_json_ordered == content_json text = json.dumps(content_json_ordered, ensure_ascii=False, indent=4) except jsonschema.exceptions.ValidationError as e: text = str(e) summary = 'JSON Daten aktualisiert.' self.put_current(text, summary=summary) def main(*args: str) -> None: local_args = pywikibot.handle_args(args) gen_factory = pagegenerators.GeneratorFactory() gen_factory.handle_args(local_args) gen = gen_factory.getCombinedGenerator(preload=True) if gen: bot = SledrunFromJsonBot(generator=gen) bot.run() else: pywikibot.bot.suggest_help(missing_generator=True) if __name__ == '__main__': main()