jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/359985 )
Change subject: [IMPR] Move unlink.BaseUnlinkBot to specialbots.py ......................................................................
[IMPR] Move unlink.BaseUnlinkBot to specialbots.py
Bug: T168336 Change-Id: I6d9c6ff6b07fe873ecc6b90416cfe489c155974d --- M pywikibot/specialbots.py M scripts/selflink.py M scripts/unlink.py 3 files changed, 83 insertions(+), 81 deletions(-)
Approvals: Dalba: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/specialbots.py b/pywikibot/specialbots.py index 68b8f70..0a1ad68 100644 --- a/pywikibot/specialbots.py +++ b/pywikibot/specialbots.py @@ -21,7 +21,13 @@
from pywikibot import config
-from pywikibot.bot import BaseBot, QuitKeyboardInterrupt +from pywikibot.bot import ( + BaseBot, ExistingPageBot, NoRedirectPageBot, AutomaticTWSummaryBot, + InteractiveReplace, ChoiceException, UnhandledAnswer, AlwaysChoice, + QuitKeyboardInterrupt, +) +from pywikibot.editor import TextEditor +from pywikibot.textlib import replace_links from pywikibot.tools import PY2, deprecated, deprecated_args from pywikibot.tools.formatter import color_format
@@ -476,3 +482,76 @@ self.__class__.__name__) finally: self.exit() + + +class EditReplacement(ChoiceException, UnhandledAnswer): + + """The text should be edited and replacement should be restarted.""" + + def __init__(self): + """Constructor.""" + super(EditReplacement, self).__init__('edit', 'e') + self.stop = True + + +class InteractiveUnlink(InteractiveReplace): + + """An implementation which just allows unlinking.""" + + def __init__(self, bot): + """Create default settings.""" + super(InteractiveUnlink, self).__init__( + old_link=bot.pageToUnlink, new_link=False, default='u') + self._always = AlwaysChoice(self, 'unlink all pages', 'a') + self._always.always = bot.getOption('always') + self.additional_choices = [ + AlwaysChoice(self, 'unlink all on page', 'p'), + self._always, EditReplacement()] + self._bot = bot + self.context = 100 + self.context_change = 100 + + def handle_answer(self, choice): + """Handle choice and store in bot's options.""" + answer = super(InteractiveUnlink, self).handle_answer(choice) + self._bot.options['always'] = self._always.always + return answer + + +class BaseUnlinkBot(ExistingPageBot, NoRedirectPageBot, AutomaticTWSummaryBot): + + """A basic bot unlinking a given link from the current page.""" + + def __init__(self, **kwargs): + """Redirect all parameters and add namespace as an available option.""" + self.availableOptions.update({ + 'namespaces': [], + # Which namespaces should be processed? + # default to [] which means all namespaces will be processed + }) + super(BaseUnlinkBot, self).__init__(**kwargs) + + def _create_callback(self): + """Create a new callback instance for replace_links.""" + return InteractiveUnlink(self) + + def unlink(self, target_page): + """Unlink all links linking to the target page.""" + text = self.current_page.text + while True: + unlink_callback = self._create_callback() + try: + text = replace_links(text, unlink_callback, target_page.site) + except EditReplacement: + new_text = TextEditor().edit( + unlink_callback.current_text, + jumpIndex=unlink_callback.current_range[0]) + # if user didn't press Cancel + if new_text: + text = new_text + else: + text = unlink_callback.current_text + else: + break + + self.put_current(text) diff --git a/scripts/selflink.py b/scripts/selflink.py index f148fe6..b1a20d1 100755 --- a/scripts/selflink.py +++ b/scripts/selflink.py @@ -21,8 +21,7 @@
from pywikibot.bot import Choice, MultipleSitesBot from pywikibot.pagegenerators import GeneratorFactory, parameterHelp - -from scripts.unlink import BaseUnlinkBot +from pywikibot.specialbots import BaseUnlinkBot
# This is required for the text that is shown when you run this script # with the parameter -help. diff --git a/scripts/unlink.py b/scripts/unlink.py index 1e4de82..7161e90 100755 --- a/scripts/unlink.py +++ b/scripts/unlink.py @@ -31,84 +31,8 @@ #
import pywikibot -from pywikibot.bot import ( - SingleSiteBot, ExistingPageBot, NoRedirectPageBot, AutomaticTWSummaryBot, - InteractiveReplace, ChoiceException, UnhandledAnswer, AlwaysChoice, -) -from pywikibot.editor import TextEditor -from pywikibot.textlib import replace_links - - -class EditReplacement(ChoiceException, UnhandledAnswer): - - """The text should be edited and replacement should be restarted.""" - - def __init__(self): - """Constructor.""" - super(EditReplacement, self).__init__('edit', 'e') - self.stop = True - - -class InteractiveUnlink(InteractiveReplace): - - """An implementation which just allows unlinking.""" - - def __init__(self, bot): - """Create default settings.""" - super(InteractiveUnlink, self).__init__( - old_link=bot.pageToUnlink, new_link=False, default='u') - self._always = AlwaysChoice(self, 'unlink all pages', 'a') - self._always.always = bot.getOption('always') - self.additional_choices = [AlwaysChoice(self, 'unlink all on page', 'p'), - self._always, EditReplacement()] - self._bot = bot - self.context = 100 - self.context_change = 100 - - def handle_answer(self, choice): - """Handle choice and store in bot's options.""" - answer = super(InteractiveUnlink, self).handle_answer(choice) - self._bot.options['always'] = self._always.always - return answer - - -class BaseUnlinkBot(ExistingPageBot, NoRedirectPageBot, AutomaticTWSummaryBot): - - """A bot unlinking a given link from the current page.""" - - def __init__(self, **kwargs): - """Redirect all parameters and add namespace as an available option.""" - self.availableOptions.update({ - 'namespaces': [], - # Which namespaces should be processed? - # default to [] which means all namespaces will be processed - }) - super(BaseUnlinkBot, self).__init__(**kwargs) - - def _create_callback(self): - """Create a new callback instance for replace_links.""" - return InteractiveUnlink(self) - - def unlink(self, target_page): - """Unlink all links linking to the target page.""" - text = self.current_page.text - while True: - unlink_callback = self._create_callback() - try: - text = replace_links(text, unlink_callback, target_page.site) - except EditReplacement: - new_text = TextEditor().edit( - unlink_callback.current_text, - jumpIndex=unlink_callback.current_range[0]) - # if user didn't press Cancel - if new_text: - text = new_text - else: - text = unlink_callback.current_text - else: - break - - self.put_current(text) +from pywikibot.bot import SingleSiteBot +from pywikibot.specialbots import BaseUnlinkBot
class UnlinkBot(SingleSiteBot, BaseUnlinkBot):