Skip to content

Commit

Permalink
Merge "[IMPR] Move unlink.BaseUnlinkBot to specialbots.py"
Browse files Browse the repository at this point in the history
  • Loading branch information
jenkins-bot authored and Gerrit Code Review committed Aug 20, 2017
2 parents 60c3013 + c78f609 commit 0c1872e
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 81 deletions.
81 changes: 80 additions & 1 deletion pywikibot/specialbots.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -476,3 +482,76 @@ def run(self):
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)
3 changes: 1 addition & 2 deletions scripts/selflink.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
80 changes: 2 additions & 78 deletions scripts/unlink.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,84 +28,8 @@
from __future__ import absolute_import, unicode_literals

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):
Expand Down

0 comments on commit 0c1872e

Please sign in to comment.