jenkins-bot has submitted this change and it was merged.
Change subject: [FIX] replace: Allow tuples of two again
......................................................................
[FIX] replace: Allow tuples of two again
With babc7cef9b7805b0bb8c2b5abab4b715e7fc1e7a the replacements needed to
be a list of Replacement instances, but some scripts might call it still
with a list of tuples. This now supports any sequence as long as it has
two entries in it and converts them into Replacement instances.
Bug: T90016
Change-Id: I257badc80a70fc836b9d452402bae75faf2141b1
---
M scripts/replace.py
1 file changed, 24 insertions(+), 5 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/scripts/replace.py b/scripts/replace.py
index efbb9c6..5c57a4f 100755
--- a/scripts/replace.py
+++ b/scripts/replace.py
@@ -125,6 +125,7 @@
__version__ = '$Id$'
#
+import collections
import re
import time
import sys
@@ -209,6 +210,15 @@
self._use_regex = use_regex
self.exceptions = exceptions
self._case_insensitive = case_insensitive
+
+ @classmethod
+ def from_compiled(cls, old_regex, new, **kwargs):
+ """Create instance from already compiled regex."""
+ if kwargs.get('use_regex', True) is not True:
+ raise ValueError('The use_regex parameter can only be True.')
+ repl = cls(old_regex.pattern, new, **kwargs)
+ repl.old_regex = old_regex
+ return repl
@property
def case_insensitive(self):
@@ -393,9 +403,10 @@
Arguments:
* generator - A generator that yields Page objects.
- * replacements - A list of 2-tuples of original text (as a
- compiled regular expression) and replacement
- text (as a string).
+ * replacements - A list of Replacement instances or sequences of
+ length 2 with the original text (as a compiled
+ regular expression) and replacement text (as a
+ string).
* exceptions - A dictionary which defines when not to change an
occurrence. See below.
* acceptall - If True, the user won't be prompted before changes
@@ -424,8 +435,16 @@
exceptionRegexes dictionary in textlib.replaceExcept().
"""
- super(ReplaceRobot, self).__init__()
- self.generator = generator
+ super(ReplaceRobot, self).__init__(generator=generator)
+ for i, replacement in enumerate(replacements):
+ if isinstance(replacement, collections.Sequence):
+ if len(replacement) != 2:
+ raise ValueError('Replacement number {0} does not have '
+ 'exactly two elements: {1}'.format(
+ i, replacement))
+ # Replacement assumes it gets strings but it's already compiled
+ replacements[i] = Replacement.from_compiled(replacement[0],
+ replacement[1])
self.replacements = replacements
self.exceptions = exceptions
self.acceptall = acceptall
--
To view, visit
https://gerrit.wikimedia.org/r/195545
To unsubscribe, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I257badc80a70fc836b9d452402bae75faf2141b1
Gerrit-PatchSet: 3
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: XZise <CommodoreFabianus(a)gmx.de>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Ladsgroup <ladsgroup(a)gmail.com>
Gerrit-Reviewer: Merlijn van Deen <valhallasw(a)arctus.nl>
Gerrit-Reviewer: XZise <CommodoreFabianus(a)gmx.de>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot <>