jenkins-bot has submitted this change and it was merged.
Change subject: Respect title inclusion exceptions from the user fixes file
......................................................................
Respect title inclusion exceptions from the user fixes file
Change-Id: I3c0ee5f6cd1ed870d4e5aadfb1f03bff69aaccf7
---
M scripts/replace.py
1 file changed, 87 insertions(+), 11 deletions(-)
Approvals:
XZise: Looks good to me, approved
jenkins-bot: Verified
diff --git a/scripts/replace.py b/scripts/replace.py
index ec8d114..529c4da 100755
--- a/scripts/replace.py
+++ b/scripts/replace.py
@@ -142,6 +142,7 @@
import re
import time
import sys
+import warnings
import pywikibot
@@ -191,6 +192,31 @@
@property
def edit_summary(self):
return self._edit_summary
+
+ @property
+ def description(self):
+ """Description of the changes that this replacement applies.
+
+ This description is used as the default summary of the replacement. If
+ you do not specify an edit summary on the command line or in some other
+ way, whenever you apply this replacement to a page and submit the
+ changes to the MediaWiki server, the edit summary includes the
+ descriptions of each replacement that you applied to the page.
+ """
+ return '-{0} +{1}'.format(self.old, self.new)
+
+ @property
+ def container(self):
+ """Container object which contains this replacement.
+
+ A container object is an object that groups one or more replacements
+ together and provides some properties that are common to all of them.
+ For example, containers may define a common name for a group of
+ replacements, or a common edit summary.
+
+ Container objects must have a "name" attribute.
+ """
+ return None
def _compile(self, use_regex, flags):
# This does not update use_regex and flags depending on this instance
@@ -263,13 +289,15 @@
are compiled only once.
"""
- def __init__(self, use_regex, exceptions, case_insensitive, edit_summary):
+ def __init__(self, use_regex, exceptions, case_insensitive, edit_summary,
+ name):
super(ReplacementList, self).__init__()
self.use_regex = use_regex
self._exceptions = exceptions
self.exceptions = None
self.case_insensitive = case_insensitive
self.edit_summary = edit_summary
+ self.name = name
def _compile_exceptions(self, use_regex, flags):
if not self.exceptions and self._exceptions is not None:
@@ -305,6 +333,19 @@
return self.fix_set.edit_summary
else:
return self._edit_summary
+
+ @property
+ def container(self):
+ """Container object which contains this replacement.
+
+ A container object is an object that groups one or more replacements
+ together and provides some properties that are common to all of them.
+ For example, containers may define a common name for a group of
+ replacements, or a common edit summary.
+
+ Container objects must have a "name" attribute.
+ """
+ return self.fix_set
def _compile(self, use_regex, flags):
super(ReplacementListEntry, self)._compile(use_regex, flags)
@@ -483,18 +524,20 @@
self.summary = summary
self.changed_pages = 0
- def isTitleExcepted(self, title):
+ def isTitleExcepted(self, title, exceptions=None):
"""
Return True iff one of the exceptions applies for the given title.
@rtype: bool
"""
- if "title" in self.exceptions:
- for exc in self.exceptions['title']:
+ if exceptions is None:
+ exceptions = self.exceptions
+ if 'title' in exceptions:
+ for exc in exceptions['title']:
if exc.search(title):
return True
- if "require-title" in self.exceptions:
- for req in self.exceptions['require-title']:
+ if 'require-title' in exceptions:
+ for req in exceptions['require-title']:
if not req.search(title):
return True
return False
@@ -511,7 +554,7 @@
return True
return False
- def apply_replacements(self, original_text, applied):
+ def apply_replacements(self, original_text, applied, page=None):
"""
Apply all replacements to the given text.
@@ -519,11 +562,35 @@
"""
def get_exceptions(exceptions):
return exceptions.get('inside-tags', []) + exceptions.get('inside', [])
+
+ if page is None:
+ pywikibot.warn(
+ 'You must pass the target page as the "page" parameter to '
+ 'apply_replacements().', DeprecationWarning, stacklevel=2)
new_text = original_text
exceptions = get_exceptions(self.exceptions)
+ skipped_containers = set()
for replacement in self.replacements:
if self.sleep is not None:
time.sleep(self.sleep)
+ if (replacement.container and
+ replacement.container.name in skipped_containers):
+ continue
+ elif page is not None and self.isTitleExcepted(
+ page.title(), replacement.exceptions):
+ if replacement.container:
+ pywikibot.output(
+ 'Skipping fix "{0}" on {1} because the title is on the '
+ 'exceptions list.'.format(
+ replacement.container.name,
+ page.title(asLink=True)))
+ skipped_containers.add(replacement.container.name)
+ else:
+ pywikibot.output(
+ 'Skipping unnamed replacement ({0}) on {1} because the '
+ 'title is on the exceptions list.'.format(
+ replacement.description, page.title(asLink=True)))
+ continue
old_text = new_text
new_text = textlib.replaceExcept(
new_text, replacement.old_regex, replacement.new,
@@ -534,8 +601,15 @@
return new_text
- def doReplacements(self, original_text):
- return self.apply_replacements(original_text, set())
+ def doReplacements(self, original_text, page=None):
+ if page is None:
+ pywikibot.warn(
+ 'You must pass the target page as the "page" parameter to '
+ 'doReplacements().', DeprecationWarning, stacklevel=2)
+ with warnings.catch_warnings():
+ warnings.simplefilter('ignore', DeprecationWarning)
+ new_text = self.apply_replacements(original_text, set(), page=page)
+ return new_text
def count_changes(self, page, err): # pylint: disable=unused-argument
"""Count succesfully changed pages."""
@@ -601,7 +675,8 @@
last_text = None
while new_text != last_text:
last_text = new_text
- new_text = self.apply_replacements(last_text, applied)
+ new_text = self.apply_replacements(last_text, applied,
+ page)
if not self.recursive:
break
if new_text == original_text:
@@ -890,7 +965,8 @@
replacement_set = ReplacementList(fix.get('regex'),
fix.get('exceptions'),
fix.get('nocase'),
- set_summary)
+ set_summary,
+ name=fix_name)
# Whether some replacements have a summary, if so only show which
# have none, otherwise just mention the complete fix
missing_fix_summaries = []
--
To view, visit https://gerrit.wikimedia.org/r/229074
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I3c0ee5f6cd1ed870d4e5aadfb1f03bff69aaccf7
Gerrit-PatchSet: 11
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Gallaecio <adriyetichaves(a)gmail.com>
Gerrit-Reviewer: Gallaecio <adriyetichaves(a)gmail.com>
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 <>
jenkins-bot has submitted this change and it was merged.
Change subject: [IMPROV] i18n_tests: Remove superfluous assertion
......................................................................
[IMPROV] i18n_tests: Remove superfluous assertion
If a method raises an exception, its output can't be compared via `assertEqual`
and usually this would lead to the test to error. But if this is then wrapped
in an `assertRaises`, it will expect that exception and thus prevent the error.
But that requires an exception so that the only way `assertEqual` actually does
compare two values will lead to `assertRaises` to fail because no exception was
raised.
This also removes any string formatting which would be done after the method
call when the call is tested and which is thus never done anyway.
Change-Id: I10690b8bd5d263001ae5b12a331b3b465150ca30
---
M tests/i18n_tests.py
1 file changed, 7 insertions(+), 21 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/tests/i18n_tests.py b/tests/i18n_tests.py
index aeb214e..89ab7ce 100644
--- a/tests/i18n_tests.py
+++ b/tests/i18n_tests.py
@@ -279,38 +279,24 @@
"""Test wrong parameter length."""
err_msg = 'Length of parameter does not match PLURAL occurrences'
with self.assertRaisesRegex(ValueError, err_msg):
- self.assertEqual(
- i18n.twntranslate('de', 'test-multiple-plurals', (1, 2))
- % {'action': u'Ändere', 'line': u'drei'},
- u'Bot: Ändere drei Zeilen von mehreren Seiten.')
+ i18n.twntranslate('de', 'test-multiple-plurals', (1, 2))
with self.assertRaisesRegex(ValueError, err_msg):
- self.assertEqual(
- i18n.twntranslate('de', 'test-multiple-plurals', ["321"])
- % {'action': u'Ändere', 'line': u'dreihunderteinundzwanzig'},
- u'Bot: Ändere dreihunderteinundzwanzig Zeilen von mehreren Seiten.')
+ i18n.twntranslate('de', 'test-multiple-plurals', ["321"])
def testMultipleNonNumbers(self):
"""Test error handling for multiple non-numbers."""
with self.assertRaisesRegex(ValueError, "invalid literal for int\(\) with base 10: 'drei'"):
- self.assertEqual(
- i18n.twntranslate('de', 'test-multiple-plurals', ["drei", "1", 1])
- % {'action': u'Ändere', 'line': u'drei'},
- u'Bot: Ändere drei Zeilen von einer Seite.')
+ i18n.twntranslate('de', 'test-multiple-plurals', ["drei", "1", 1])
with self.assertRaisesRegex(ValueError, "invalid literal for int\(\) with base 10: 'elf'"):
- self.assertEqual(
- i18n.twntranslate('de', 'test-multiple-plurals',
- {'action': u'Ändere', 'line': "elf", 'page': 2}),
- u'Bot: Ändere elf Zeilen von mehreren Seiten.')
+ i18n.twntranslate('de', 'test-multiple-plurals',
+ {'action': u'Ändere', 'line': "elf", 'page': 2})
def testAllParametersExist(self):
with self.assertRaisesRegex(KeyError, repr(u'line')):
# all parameters must be inside twntranslate
- self.assertEqual(
- i18n.twntranslate('de', 'test-multiple-plurals',
- {'line': 1, 'page': 1})
- % {'action': u'Ändere'},
- u'Bot: Ändere 1 Zeile von einer Seite.')
+ i18n.twntranslate('de', 'test-multiple-plurals',
+ {'line': 1, 'page': 1}) % {'action': u'Ändere'}
def test_fallback_lang(self):
"""
--
To view, visit https://gerrit.wikimedia.org/r/231967
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I10690b8bd5d263001ae5b12a331b3b465150ca30
Gerrit-PatchSet: 2
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: Siebrand <siebrand(a)kitano.nl>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot <>