jenkins-bot has submitted this change and it was merged.

Change subject: [IMPROV] replace: Ask for summary if necessary
......................................................................


[IMPROV] replace: Ask for summary if necessary

At the moment it always asks for a default summary unless one was defined via
the command line. But if only fixes are used and all replacements in these
fixes have a summary defined (either via the fix itself or separate for each
replacement) it shouldn't ask for this. This makes it easier to be used
autonomously.

As a byproduct it now knows which replacements from fixes have no summary
defined and lists them before asking for a default summary.

It also adds an option (-automaticsummary) which does prevent that the script
asks the operator what summary it uses and allows to use it without
user intervention but using the automatically generated summary.

Change-Id: Ie72ed81550631c3f53f7991fc688ae73a9b2515e
---
M scripts/replace.py
M tests/data/fixes.py
M tests/replacebot_tests.py
M tox.ini
4 files changed, 92 insertions(+), 20 deletions(-)

Approvals:
  John Vandenberg: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/scripts/replace.py b/scripts/replace.py
index 9dacfb8..ec8d114 100755
--- a/scripts/replace.py
+++ b/scripts/replace.py
@@ -53,7 +53,10 @@
 
 -summary:XYZ      Set the summary message text for the edit to XYZ, bypassing
                   the predefined message texts with original and replacements
-                  inserted.
+                  inserted. Can't be used with -automaticsummary.
+
+-automaticsummary Uses an automatic summary for all replacements which don't
+                  have a summary defined. Can't be used with -summary.
 
 -sleep:123        If you use -fix you can check multiple regex at the same time
                   in every page. This can lead to a great waste of CPU because
@@ -790,6 +793,8 @@
             add_cat = arg[8:]
         elif arg.startswith('-summary:'):
             edit_summary = arg[9:]
+        elif arg.startswith('-automaticsummary'):
+            edit_summary = True
         elif arg.startswith('-allowoverlap'):
             allowoverlap = True
         elif arg.startswith('-manualinput'):
@@ -846,6 +851,7 @@
                 'Please enter another text that should be replaced,'
                 '\nor press Enter to start:')
 
+    # The summary stored here won't be actually used but is only an example
     single_summary = None
     for i in range(0, len(commandline_replacements), 2):
         replacement = Replacement(commandline_replacements[i],
@@ -858,22 +864,11 @@
             )
         replacements.append(replacement)
 
-    if not edit_summary:
-        if single_summary:
-            pywikibot.output(u'The summary message for the command line '
-                             'replacements will be something like: %s'
-                             % single_summary)
-        if fixes_set:
-            pywikibot.output('If a summary is defined for the fix, this '
-                             'default summary won\'t be applied.')
-        edit_summary = pywikibot.input(
-            'Press Enter to use this automatic message, or enter a '
-            'description of the\nchanges your bot will make:')
-
     # Perform one of the predefined actions.
-    for fix in fixes_set:
+    missing_fixes_summaries = []  # which a fixes/replacements miss a summary
+    for fix_name in fixes_set:
         try:
-            fix = fixes.fixes[fix]
+            fix = fixes.fixes[fix_name]
         except KeyError:
             pywikibot.output(u'Available predefined fixes are: %s'
                              % ', '.join(fixes.fixes.keys()))
@@ -881,6 +876,10 @@
                 pywikibot.output('The user fixes file could not be found: '
                                  '{0}'.format(fixes.filename))
             return
+        if not fix['replacements']:
+            pywikibot.warning('No replacements defined for fix '
+                              '"{0}"'.format(fix_name))
+            continue
         if "msg" in fix:
             if isinstance(fix['msg'], basestring):
                 set_summary = i18n.twtranslate(site, str(fix['msg']))
@@ -892,8 +891,14 @@
                                           fix.get('exceptions'),
                                           fix.get('nocase'),
                                           set_summary)
-        for replacement in fix['replacements']:
+        # Whether some replacements have a summary, if so only show which
+        # have none, otherwise just mention the complete fix
+        missing_fix_summaries = []
+        for index, replacement in enumerate(fix['replacements'], start=1):
             summary = None if len(replacement) < 3 else replacement[2]
+            if not set_summary and not summary:
+                missing_fix_summaries.append(
+                    '"{0}" (replacement #{1})'.format(fix_name, index))
             if chars.contains_invisible(replacement[0]):
                 pywikibot.warning('The old string "{0}" contains formatting '
                                   'characters like U+200E'.format(
@@ -908,9 +913,34 @@
                 fix_set=replacement_set,
                 edit_summary=summary,
             ))
+
         if replacement_set:
             replacements.extend(replacement_set)
 
+        if len(fix['replacements']) == len(missing_fix_summaries):
+            missing_fixes_summaries.append(
+                '"{0}" (all replacements)'.format(fix_name))
+        else:
+            missing_fixes_summaries += missing_fix_summaries
+
+    if ((not edit_summary or edit_summary is True) and
+            (missing_fixes_summaries or single_summary)):
+        if single_summary:
+            pywikibot.output(u'The summary message for the command line '
+                             'replacements will be something like: %s'
+                             % single_summary)
+        if missing_fixes_summaries:
+            pywikibot.output('The summary will not be used when the fix has '
+                             'one defined but the following fix(es) do(es) not 
'
+                             'have a summary defined: '
+                             '{0}'.format(', '.join(missing_fixes_summaries)))
+        if edit_summary is not True:
+            edit_summary = pywikibot.input(
+                'Press Enter to use this automatic message, or enter a '
+                'description of the\nchanges your bot will make:')
+        else:
+            edit_summary = ''
+
     # Set the regular expression flags
     flags = re.UNICODE
     if caseInsensitive:
diff --git a/tests/data/fixes.py b/tests/data/fixes.py
index ef76906..e750b98 100644
--- a/tests/data/fixes.py
+++ b/tests/data/fixes.py
@@ -32,6 +32,21 @@
     ]
 }
 
+fixes['all-repl-msg'] = {
+    'regex': False,
+    'replacements': [
+        ('1', '2', 'M1'),
+    ]
+}
+
+fixes['partial-repl-msg'] = {
+    'regex': False,
+    'replacements': [
+        ('1', '2', 'M1'),
+        ('3', '4'),
+    ]
+}
+
 fixes['has-msg-multiple'] = {
     'regex': False,
     'msg': {
diff --git a/tests/replacebot_tests.py b/tests/replacebot_tests.py
index 610b811..cd854c8 100644
--- a/tests/replacebot_tests.py
+++ b/tests/replacebot_tests.py
@@ -97,11 +97,17 @@
         self.assertEqual(replacement.old, str(offset * 2 + 1))
         self.assertEqual(replacement.new, str(offset * 2 + 2))
 
-    def _test_fix_replacement(self, replacement, length=1, offset=0):
+    def _test_fix_replacement(self, replacement, length=1, offset=0, 
msg=False):
         """Test a replacement from a fix."""
         assert length > offset
         self._test_replacement(replacement, replace.ReplacementListEntry,
                                offset)
+        if msg:
+            self.assertEqual(replacement.edit_summary,
+                             'M{0}'.format(offset + 1))
+        else:
+            self.assertIs(replacement.edit_summary,
+                          replacement.fix_set.edit_summary)
         self.assertIsInstance(replacement.fix_set, replace.ReplacementList)
         self.assertIsInstance(replacement.fix_set, list)
         self.assertIn(replacement, replacement.fix_set)
@@ -128,15 +134,22 @@
         self.assertEqual(len(bot.replacements), 1)
         self._test_replacement(bot.replacements[0])
 
+    def test_cmd_automatic(self):
+        """Test command line replacements with automatic summary."""
+        bot = self._get_bot(None, '1', '2', '-automaticsummary')
+        self.assertEqual(len(bot.replacements), 1)
+        self._test_replacement(bot.replacements[0])
+        self.assertEqual(self.inputs, [])
+
     def test_only_fix_global_message(self):
         """Test fixes replacements only."""
-        bot = self._get_bot(True, '-fix:has-msg')
+        bot = self._get_bot(None, '-fix:has-msg')
         self.assertEqual(len(bot.replacements), 1)
         self._test_fix_replacement(bot.replacements[0])
 
     def test_only_fix_global_message_tw(self):
         """Test fixes replacements only."""
-        bot = self._get_bot(True, '-fix:has-msg-tw')
+        bot = self._get_bot(None, '-fix:has-msg-tw')
         self.assertEqual(len(bot.replacements), 1)
         self._test_fix_replacement(bot.replacements[0])
 
@@ -146,9 +159,22 @@
         self.assertEqual(len(bot.replacements), 1)
         self._test_fix_replacement(bot.replacements[0])
 
+    def test_only_fix_all_replacement_summary(self):
+        """Test fixes replacements only."""
+        bot = self._get_bot(None, '-fix:all-repl-msg')
+        self.assertEqual(len(bot.replacements), 1)
+        self._test_fix_replacement(bot.replacements[0], msg=True)
+
+    def test_only_fix_partial_replacement_summary(self):
+        """Test fixes replacements only."""
+        bot = self._get_bot(True, '-fix:partial-repl-msg')
+        for offset, replacement in enumerate(bot.replacements):
+            self._test_fix_replacement(replacement, 2, offset, offset == 0)
+        self.assertEqual(len(bot.replacements), 2)
+
     def test_only_fix_multiple(self):
         """Test fixes replacements only."""
-        bot = self._get_bot(True, '-fix:has-msg-multiple')
+        bot = self._get_bot(None, '-fix:has-msg-multiple')
         for offset, replacement in enumerate(bot.replacements):
             self._test_fix_replacement(replacement, 3, offset)
         self.assertEqual(len(bot.replacements), 3)
diff --git a/tox.ini b/tox.ini
index c85de9f..e65a2cc 100644
--- a/tox.ini
+++ b/tox.ini
@@ -142,6 +142,7 @@
     tests/protectbot_tests.py \
     tests/pwb/ \
     tests/pwb_tests.py \
+    tests/replacebot_tests.py \
     tests/script_tests.py \
     tests/site_detect_tests.py \
     tests/tests_tests.py \

-- 
To view, visit https://gerrit.wikimedia.org/r/221377
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Ie72ed81550631c3f53f7991fc688ae73a9b2515e
Gerrit-PatchSet: 7
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: XZise <[email protected]>
Gerrit-Reviewer: John Vandenberg <[email protected]>
Gerrit-Reviewer: Ladsgroup <[email protected]>
Gerrit-Reviewer: Merlijn van Deen <[email protected]>
Gerrit-Reviewer: XZise <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to