jenkins-bot has submitted this change and it was merged.
Change subject: small improvements to solve_disambiguation.py ......................................................................
small improvements to solve_disambiguation.py
* run also on sites with dn_template not defined * add tools.concat_options which is similar to textwrap.fill() but only adds a newline between two options (and not any space) * replace `find` with `in`, which is prettier and faster: * http://stackoverflow.com/a/4901653 * http://stackoverflow.com/a/18437853 * buffer the translated dn_template into dn_template_str once per site * use dn_template_str[:-2] instead of dn_template_str[:4] to support floating-length templates
Bug: T78521 Change-Id: Iade765b402498876b8ea6ebbd31649f17c67f9eb --- M pywikibot/tools.py M scripts/solve_disambiguation.py 2 files changed, 42 insertions(+), 15 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/tools.py b/pywikibot/tools.py index 7ff4008..9691ea2 100644 --- a/pywikibot/tools.py +++ b/pywikibot/tools.py @@ -73,6 +73,28 @@ return other != self._cmpkey()
+def concat_options(message, line_length, options): + indent = len(message) + 2 + line_length -= indent + option_msg = u'' + option_line = u'' + for option in options: + if option_line: + option_line += ', ' + # +1 for ',' + if len(option_line) + len(option) + 1 > line_length: + if option_msg: + option_msg += '\n' + ' ' * indent + option_msg += option_line[:-1] # remove space + option_line = '' + option_line += option + if option_line: + if option_msg: + option_msg += '\n' + ' ' * indent + option_msg += option_line + return u'{0} ({1}):'.format(message, option_msg) + + class MediaWikiVersion(Version):
"""Version object to allow comparing 'wmf' versions with normal ones.""" diff --git a/scripts/solve_disambiguation.py b/scripts/solve_disambiguation.py index 0374f5c..43115bd 100644 --- a/scripts/solve_disambiguation.py +++ b/scripts/solve_disambiguation.py @@ -83,6 +83,7 @@
import pywikibot from pywikibot import editor as editarticle +from pywikibot.tools import concat_options from pywikibot import pagegenerators, config, i18n from pywikibot.bot import Bot, QuitKeyboardInterrupt
@@ -485,6 +486,8 @@ self.mylang = self.mysite.language() self.comment = None
+ self.dn_template_str = i18n.translate(self.mysite, dn_template) + self.setupRegexes()
def checkContents(self, text): @@ -555,7 +558,6 @@ """ # TODO: break this function up into subroutines!
- dn_template_str = i18n.translate(self.mysite, dn_template) include = False unlink = False new_targets = [] @@ -643,9 +645,10 @@ n += 1 # how many bytes should be displayed around the current link context = 60 - # check if there's a {{dn}} here already - already_dn = text[m.end():m.end() + 8].find(dn_template_str[:4]) > -1 - if already_dn and self.dnSkip: + # check if there's a dn-template here already + if (self.dnSkip and self.dn_template_str and + self.dn_template_str[:-2] in text[m.end():m.end() + + len(self.dn_template_str) + 8]): continue
# This loop will run while the user doesn't choose an option @@ -662,16 +665,18 @@ + text[m.start():m.end()] + '\03{default}' + text[m.end():m.end() + context]) + options = ['#', 'r#', '[s]kip link', '[e]dit page', + '[n]ext page', '[u]nlink', '[q]uit'] + if self.dn_template_str: + options.append(u'[t]ag template %s' % self.dn_template_str) + options.append('[m]ore context') + if not edited: + options.append('show [d]isambiguation page') + options += ['[l]ist', '[a]dd new'] if edited: - choice = pywikibot.input( -u"Option (#, r#, [s]kip link, [e]dit page, [n]ext page, [u]nlink, [q]uit,\n" -u" [t]ag template " + dn_template_str + ",\n" -u" [m]ore context, [l]ist, [a]dd new, x=save in this form):") - else: - choice = pywikibot.input( -u"Option (#, r#, [s]kip link, [e]dit page, [n]ext page, [u]nlink, [q]uit,\n" -u" [t]ag template " + dn_template_str + ",\n" -u" [m]ore context, show [d]isambiguation page, [l]ist, [a]dd new):") + options += ['save in this form [x]'] + options = concat_options('Option', 72, options) + choice = pywikibot.input(options) else: choice = self.always if choice in ['a', 'A']: @@ -746,7 +751,7 @@ if trailing_chars: link_text += trailing_chars # '?', '/' for old choice - if choice in ['t', 'T', '?', '/']: + if choice in ['t', 'T', '?', '/'] and self.dn_template_str: # small chunk of text to search search_text = text[m.end():m.end() + context] # figure out where the link (and sentance) ends, put note @@ -758,7 +763,7 @@ position_split = 0 # insert dab needed template text = (text[:m.end() + position_split] + - dn_template_str + + self.dn_template_str + text[m.end() + position_split:]) dn = True continue
pywikibot-commits@lists.wikimedia.org