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)
50 validate(instance=content_json, schema=self.sledrun_schema)
52 content_json_ordered = order_json_keys(content_json, self.sledrun_schema)
53 assert content_json_ordered == content_json
54 text = json.dumps(content_json_ordered, ensure_ascii=False, indent=4)
56 except jsonschema.exceptions.ValidationError as e:
59 summary = 'JSON Daten aktualisiert.'
60 self.userPut(sledrun_json_page, sledrun_json_page.text, text, summary=summary, contentmodel='json')
63 def main(*args: str) -> None:
64 local_args = pywikibot.handle_args(args)
65 gen_factory = pagegenerators.GeneratorFactory()
66 gen_factory.handle_args(local_args)
67 gen = gen_factory.getCombinedGenerator(preload=True)
69 bot = SledrunFromJsonBot(generator=gen)
72 pywikibot.bot.suggest_help(missing_generator=True)
75 if __name__ == '__main__':