jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/272316 )
Change subject: [IMPR] Provide options by a separate handler class ......................................................................
[IMPR] Provide options by a separate handler class
- provide an option handler class - derive BaseBot from OptionHandler class
There are som generator classes who could derive from OptionHandler: - redirect.RedirectGenerator - pagefromfile.PageFromFileReader Will be done in a separate patch set. - rewrite help message for redirect.py
Change-Id: Ib281147d38f3d6eaf34f1a1ad0c7c920513f0d79 --- M pywikibot/bot.py 1 file changed, 48 insertions(+), 27 deletions(-)
Approvals: Dalba: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/bot.py b/pywikibot/bot.py index 4785117..4cef28a 100644 --- a/pywikibot/bot.py +++ b/pywikibot/bot.py @@ -1131,31 +1131,16 @@ i18n.input('pywikibot-enter-finished-browser')
-class BaseBot(object): +class OptionHandler(object):
- """ - Generic Bot to be subclassed. + """Class to get and set options."""
- This class provides a run() method for basic processing of a - generator one page at a time. - - If the subclass places a page generator in self.generator, - Bot will process each page in the generator, invoking the method treat() - which must then be implemented by subclasses. - - If the subclass does not set a generator, or does not override - treat() or run(), NotImplementedError is raised. - """ - - # Bot configuration. + # Handler configuration. # Only the keys of the dict can be passed as init options # The values are the default values - # Extend this in subclasses! - availableOptions = { - 'always': False, # By default ask for confirmation when putting a page - } + # Overwrite this in subclasses!
- _current_page = None + availableOptions = {}
def __init__(self, **kwargs): """ @@ -1164,13 +1149,7 @@ @param kwargs: bot options @type kwargs: dict """ - if 'generator' in kwargs: - self.generator = kwargs.pop('generator') - self.setOptions(**kwargs) - - self._treat_counter = 0 - self._save_counter = 0
def setOptions(self, **kwargs): """ @@ -1196,13 +1175,55 @@ """ Get the current value of an option.
- @param option: key defined in Bot.availableOptions + @param option: key defined in OptionHandler.availableOptions """ try: return self.options.get(option, self.availableOptions[option]) except KeyError: raise pywikibot.Error(u'%s is not a valid bot option.' % option)
+ +class BaseBot(OptionHandler): + + """ + Generic Bot to be subclassed. + + This class provides a run() method for basic processing of a + generator one page at a time. + + If the subclass places a page generator in self.generator, + Bot will process each page in the generator, invoking the method treat() + which must then be implemented by subclasses. + + If the subclass does not set a generator, or does not override + treat() or run(), NotImplementedError is raised. + """ + + # Handler configuration. + # The values are the default values + # Extend this in subclasses! + + availableOptions = { + 'always': False, # By default ask for confirmation when putting a page + } + + _current_page = None + + def __init__(self, **kwargs): + """ + Only accept options defined in availableOptions. + + @param kwargs: bot options + @type kwargs: dict + """ + if 'generator' in kwargs: + self.generator = kwargs.pop('generator') + + super(BaseBot, self).__init__(**kwargs) + + self._treat_counter = 0 + self._save_counter = 0 + @property def current_page(self): """Return the current working page as a property."""