jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/511692 )
Change subject: [IMPR] add a new ConfigParserBot class ......................................................................
[IMPR] add a new ConfigParserBot class
ConfigParserBot enables to set options from the scripts.ini setting file. The setting section for the script is identified by the module name. All settings in the settings file must be listed in the availabelOptions dictionary of the bot. The type of the value is identified by the availabelOptions default.
The script's settings is like this:
[add_text] summary = Bot: Aggiungo template Categorizzare
[shell] always=true
Bug: T223778 Change-Id: I601a5bc055239a7848c897b4651ed22f80af2ff0 --- M pywikibot/bot.py 1 file changed, 56 insertions(+), 1 deletion(-)
Approvals: D3r1ck01: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/bot.py b/pywikibot/bot.py index 954d07f..b7f906b 100644 --- a/pywikibot/bot.py +++ b/pywikibot/bot.py @@ -71,7 +71,7 @@ 'showHelp', 'suggest_help', 'writeToCommandLogFile', 'open_webbrowser', 'OptionHandler', - 'BaseBot', 'Bot', 'SingleSiteBot', 'MultipleSitesBot', + 'BaseBot', 'Bot', 'ConfigParserBot', 'SingleSiteBot', 'MultipleSitesBot', 'CurrentPageBot', 'AutomaticTWSummaryBot', 'ExistingPageBot', 'FollowRedirectPageBot', 'CreatingPageBot', 'RedirectPageBot', 'NoRedirectPageBot', @@ -92,6 +92,11 @@ from warnings import warn import webbrowser
+try: + import configparser +except ImportError: # PY2 + import ConfigParser as configparser # noqa: N813 + from textwrap import fill
import pywikibot @@ -1715,6 +1720,56 @@ return page
+class ConfigParserBot(BaseBot): + + """A bot class that can read options from scripts.ini file. + + All options must be predefined in availableOptions dictionary. The type + of these options is responsible for the correct interpretation of the + options type given by the .ini file. They can be interpreted as bool, + int, float or str (default). The settings file may be like: + + [add_text] + # edit summary for the bot. + summary = Bot: Aggiungo template Categorizzare + + [shell] ; Shell options + always: true + + The option values are interpreted in this order:: + + - availableOptions default setting + - script.ini options settings + - command line arguments + """ + + INI = 'scripts.ini' + + def setOptions(self, **kwargs): + """Read settings from scripts.ini file.""" + conf = configparser.ConfigParser() + section = calledModuleName() + + if (conf.read(self.INI) == [self.INI] and conf.has_section(section)): + pywikibot.output('Reading settings from {} file.'.format(self.INI)) + args = {} + for option, value in self.availableOptions.items(): + if not conf.has_option(section, option): + continue + # use a convenience parser method, default to get() + method = getattr(conf, 'get' + type(value).__name__, + getattr(conf, 'get')) + args[option] = method(section, option) + for opt in set(conf.options(section)) - set(args): + pywikibot.warning( + opt + ' is not a valid option. It was ignored.') + args.update(kwargs) + else: + args = kwargs + + super(ConfigParserBot, self).setOptions(**args) + + class CurrentPageBot(BaseBot):
"""
pywikibot-commits@lists.wikimedia.org