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:
16 from jsonschema import validate
17 from pywikibot import pagegenerators, Page, warning
18 from pywikibot.bot import (
19 AutomaticTWSummaryBot,
26 from wrpylib.json_tools import order_json_keys
27 from wrpylib.wrmwmarkup import create_sledrun_wiki
29 docuReplacements = {'¶ms;': pagegenerators.parameterHelp}
32 class SledrunFromJsonBot(
37 AutomaticTWSummaryBot,
39 def setup(self) -> None:
40 schema = Page(self.site, 'Winterrodeln:Datenschema/Rodelbahn/V1.json')
41 assert schema.content_model == 'json'
42 self.sledrun_schema = json.loads(schema.text)
44 def treat_page(self) -> None:
45 """Load the given page, do some changes, and save it."""
46 if self.current_page.content_model != 'wikitext':
47 warning(f"The content model of {self.current_page.title()} is {self.current_page.content_model} "
48 f"instead of wikitext")
51 sledrun_json_page = Page(self.site, self.current_page.title() + '/Rodelbahn.json')
52 if not sledrun_json_page.exists():
53 warning(f"{sledrun_json_page} does not exist.")
56 if sledrun_json_page.content_model != 'json':
57 warning(f"The content model of {sledrun_json_page} is {sledrun_json_page.content_model} "
61 sledrun_json = json.loads(sledrun_json_page.text)
62 sledrun_json_orig = json.loads(sledrun_json_page.text)
63 sledrun_json_orig_text = json.dumps(sledrun_json_orig, ensure_ascii=False, indent=4)
65 # *here*, sledrun_json can be processed
67 jsonschema.validate(instance=sledrun_json, schema=self.sledrun_schema)
68 sledrun_json_ordered = order_json_keys(sledrun_json, self.sledrun_schema)
69 assert sledrun_json_ordered == sledrun_json
70 if sledrun_json == sledrun_json_orig:
72 sledrun_json_text = json.dumps(sledrun_json_ordered, ensure_ascii=False, indent=4)
73 summary = 'JSON Daten aktualisiert.'
74 self.userPut(sledrun_json_page, sledrun_json_orig_text, sledrun_json_text, summary=summary, contentmodel='json')
77 def main(*args: str) -> None:
78 local_args = pywikibot.handle_args(args)
79 gen_factory = pagegenerators.GeneratorFactory()
80 gen_factory.handle_args(local_args)
81 gen = gen_factory.getCombinedGenerator(preload=True)
83 bot = SledrunFromJsonBot(generator=gen)
86 pywikibot.bot.suggest_help(missing_generator=True)
89 if __name__ == '__main__':