#!/usr/bin/env python3"""This module can do slight modifications to tidy a wiki page's source code.The changes are not supposed to change the look of the rendered wiki page.The following parameters are supported:-always Don't prompt you for each replacement. Warning (see below) has not to be confirmed. ATTENTION: Use this with care!-async Put page on queue to be saved to wiki asynchronously.-summary:XYZ Set the summary message text for the edit to XYZ, bypassing the predefined message texts with original and replacements inserted.-ignore: Ignores if an error occurred and either skips the page or only that method. It can be set to: all - does not ignore errors match - ignores ISBN related errors (default) method - ignores fixing method errors page - ignores page related errorsThe following generators and filters are supported:¶ms;&warning;For further information see pywikibot/cosmetic_changes.py"""## (C) Pywikibot team, 2006-2022## Distributed under the terms of the MIT license.#from__future__importannotationsimportpywikibotfrompywikibotimportconfig,pagegeneratorsfrompywikibot.botimportAutomaticTWSummaryBot,ExistingPageBotfrompywikibot.cosmetic_changesimportCANCEL,CosmeticChangesToolkitfrompywikibot.exceptionsimportInvalidPageErrorwarning="""ATTENTION: You can run this script as a stand-alone for testing purposes.However, the changes that are made are only minor, and other usersmight get angry if you fill the version histories and watchlists with suchirrelevant changes. Some wikis prohibit stand-alone running."""docuReplacements={'¶ms;':pagegenerators.parameterHelp,'&warning;':warning,}
[docs]deftreat_page(self)->None:"""Treat page with the cosmetic toolkit. .. versionchanged:: 7.0 skip if InvalidPageError is raised """cc_toolkit=CosmeticChangesToolkit(self.current_page,ignore=self.opt.ignore)try:old_text=self.current_page.textexceptInvalidPageErrorase:pywikibot.error(e)returnnew_text=cc_toolkit.change(old_text)ifnew_textisnotFalse:self.put_current(new_text=new_text,summary=self.opt.summary,asynchronous=self.opt['async'])
[docs]defmain(*args:str)->None:"""Process command line arguments and invoke bot. If args is an empty list, sys.argv is used. :param args: command line arguments """options={}# Process global args and prepare generator args parsergen_factory=pagegenerators.GeneratorFactory()local_args=pywikibot.handle_args(args)local_args=gen_factory.handle_args(local_args)forarginlocal_args:opt,_,value=arg.partition(':')ifopt=='-summary':options['summary']=valueelifoptin('-always','-async'):options[opt[1:]]=Trueelifopt=='-ignore':value=value.upper()try:options['ignore']=getattr(CANCEL,value)exceptAttributeError:raiseValueError(f'Unknown ignore mode {value!r}!')gen=gen_factory.getCombinedGenerator(preload=True)ifnotpywikibot.bot.suggest_help(missing_generator=notgen) \
and(options.get('always')orconfig.simulateorpywikibot.input_yn(warning+'\nDo you really want to continue?',default=False,automatic_quit=False)):bot=CosmeticChangesBot(generator=gen,**options)bot.run()