3 User script for pywikibot (https://gerrit.wikimedia.org/r/pywikibot/core.git), tested with version 6.6.1.
4 Put it in directory scripts/userscripts.
6 Edit JSON associated with sledruns.
8 The following generators and filters are supported:
14 from jsonschema import validate
17 from pywikibot import pagegenerators, Page
18 from pywikibot.bot import (
19 AutomaticTWSummaryBot,
26 from wrpylib.json_tools import order_json_keys
28 docuReplacements = {'¶ms;': pagegenerators.parameterHelp}
31 class SledrunFromJsonBot(
36 AutomaticTWSummaryBot,
38 def setup(self) -> None:
39 schema = Page(self.site, 'Winterrodeln:Datenschema/Rodelbahn/V1.json')
40 assert schema.content_model == 'json'
41 self.sledrun_schema = json.loads(schema.text)
43 def treat_page(self) -> None:
44 """Load the given page, do some changes, and save it."""
45 sledrun_json_page = Page(self.site, self.current_page.title() + '/Rodelbahn.json')
46 if not sledrun_json_page.exists():
48 content_json = json.loads(sledrun_json_page.text)
49 text_orig = json.dumps(content_json, ensure_ascii=False, indent=4)
51 validate(instance=content_json, schema=self.sledrun_schema)
53 content_json_ordered = order_json_keys(content_json, self.sledrun_schema)
54 assert content_json_ordered == content_json
55 text = json.dumps(content_json_ordered, ensure_ascii=False, indent=4)
57 except jsonschema.exceptions.ValidationError as e:
60 summary = 'JSON Daten aktualisiert.'
61 self.userPut(sledrun_json_page, text_orig, text, summary=summary, contentmodel='json')
64 def main(*args: str) -> None:
65 local_args = pywikibot.handle_args(args)
66 gen_factory = pagegenerators.GeneratorFactory()
67 gen_factory.handle_args(local_args)
68 gen = gen_factory.getCombinedGenerator(preload=True)
70 bot = SledrunFromJsonBot(generator=gen)
73 pywikibot.bot.suggest_help(missing_generator=True)
76 if __name__ == '__main__':