jenkins-bot has submitted this change. (
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1343046?usp=email )
Change subject: solve_disambiguation: Reuse link match boundaries
......................................................................
solve_disambiguation: Reuse link match boundaries
Read each link match span once and reuse its start and end offsets throughout
the disambiguation replacement loop. This avoids repeated match method calls
when building options, checking templates, and replacing link text.
Change-Id: I1a7fad5723a77629265e4385aab7cefe443e30a6
---
M scripts/solve_disambiguation.py
1 file changed, 17 insertions(+), 14 deletions(-)
Approvals:
jenkins-bot: Verified
Xqt: Looks good to me, approved
diff --git a/scripts/solve_disambiguation.py b/scripts/solve_disambiguation.py
index 8152861..c72d8dc 100755
--- a/scripts/solve_disambiguation.py
+++ b/scripts/solve_disambiguation.py
@@ -818,8 +818,9 @@
# There are links to change; stop loop and save page
break
+ match_start, match_end = m.span()
# Ensure that next time around we will not find this same hit.
- curpos = m.start() + 1
+ curpos = match_start + 1
try:
foundlink = pywikibot.Link(m['title'], disamb_page.site)
foundlink.parse()
@@ -847,16 +848,18 @@
context = 60
# check if there's a dn-template here already
- if (self.opt.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
+ if self.opt.dnskip and self.dn_template_str:
+ dn_template_end = (match_end
+ + len(self.dn_template_str) + 8)
+ if self.dn_template_str[:-2] in text[
+ match_end:dn_template_end]:
+ continue
- edit = EditOption('edit page', 'e', text, m.start(),
+ edit = EditOption('edit page', 'e', text, match_start,
disamb_page.title())
context_option = HighlightContextOption(
- 'more context', 'm', text, 60, start=m.start(),
- end=m.end())
+ 'more context', 'm', text, 60, start=match_start,
+ end=match_end)
context_option.before_question = True
options = [ListOption(self.opt.pos, ''),
@@ -875,7 +878,7 @@
options.append(context_option)
if not edited:
options.append(ShowPageOption(
- 'show disambiguation page', 'd', m.start(),
+ 'show disambiguation page', 'd', match_start,
disamb_page))
options += [
@@ -934,7 +937,7 @@
if answer == 't':
assert self.dn_template_str
# small chunk of text to search
- search_text = text[m.end():m.end() + context]
+ search_text = text[match_end:match_end + context]
# figure out where the link (and sentence) ends, put note
# there
end_of_word_match = re.search(r'\s', search_text)
@@ -945,15 +948,15 @@
position_split = 0
# insert dab needed template
- text = (text[:m.end() + position_split]
+ text = (text[:match_end + position_split]
+ self.dn_template_str
- + text[m.end() + position_split:])
+ + text[match_end + position_split:])
dn = True
continue
if answer == 'u':
# unlink - we remove the section if there's any
- text = text[:m.start()] + link_text + text[m.end():]
+ text = text[:match_start] + link_text + text[match_end:]
unlink_counter += 1
continue
@@ -997,7 +1000,7 @@
f'{link_text[len(new_page_title):]}')
else:
newlink = f'[[{new_page_title}{section}|{link_text}]]'
- text = text[:m.start()] + newlink + text[m.end():]
+ text = text[:match_start] + newlink + text[match_end:]
continue
if text == original_text:
--
To view, visit
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1343046?usp=email
To unsubscribe, or for help writing mail filters, visit
https://gerrit.wikimedia.org/r/settings?usp=email
Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I1a7fad5723a77629265e4385aab7cefe443e30a6
Gerrit-Change-Number: 1343046
Gerrit-PatchSet: 2
Gerrit-Owner: Mahveotm <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]