jenkins-bot has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1022465?usp=email )

Change subject: [bugfix] cleanup ReplaceRobot.treat(), move statements outside 
the loop
......................................................................

[bugfix] cleanup ReplaceRobot.treat(), move statements outside the loop

- test isTextExcepted() for the original_text instead for the modified
- replace text only once instead over and over again
- add the optionated category outside the loop
- leave treat method after opening the browser

Bug: T363047
Change-Id: I57417bfaf22627a76cc61db5ba58c4c35e3d77fd
---
M scripts/replace.py
1 file changed, 39 insertions(+), 40 deletions(-)

Approvals:
  jenkins-bot: Verified
  Matěj Suchánek: Looks good to me, approved
  Xqt: Looks good to me, approved




diff --git a/scripts/replace.py b/scripts/replace.py
index 5a809f4..7c91495 100755
--- a/scripts/replace.py
+++ b/scripts/replace.py
@@ -686,36 +686,37 @@
         except InvalidPageError as e:
             pywikibot.error(e)
             return
+
+        if self.isTextExcepted(original_text):
+            pywikibot.info(f'Skipping {page} because it contains text '
+                           f'that is on the exceptions list.')
+            return
+
         applied = set()
         new_text = original_text
         last_text = None
+        while new_text != last_text:
+            last_text = new_text
+            new_text = self.apply_replacements(last_text, applied, page)
+            if not self.opt.recursive:
+                break
+
+        if new_text == original_text:
+            if not self.opt.quiet:
+                pywikibot.info(f'No changes were necessary in {page}')
+            return
+
+        if self.opt.addcat:
+            # Fetch only categories in wikitext, otherwise the others
+            # will be explicitly added.
+            cats = textlib.getCategoryLinks(new_text, site=page.site)
+            if self.opt.addcat not in cats:
+                cats.append(self.opt.addcat)
+                new_text = textlib.replaceCategoryLinks(new_text, cats,
+                                                        site=page.site)
+
         context = 0
         while True:
-            if self.isTextExcepted(new_text):
-                pywikibot.info(f'Skipping {page} because it contains text '
-                               f'that is on the exceptions list.')
-                return
-
-            while new_text != last_text:
-                last_text = new_text
-                new_text = self.apply_replacements(last_text, applied, page)
-                if not self.opt.recursive:
-                    break
-
-            if new_text == original_text:
-                if not self.opt.quiet:
-                    pywikibot.info(f'No changes were necessary in {page}')
-                return
-
-            if self.opt.addcat:
-                # Fetch only categories in wikitext, otherwise the others
-                # will be explicitly added.
-                cats = textlib.getCategoryLinks(new_text, site=page.site)
-                if self.opt.addcat not in cats:
-                    cats.append(self.opt.addcat)
-                    new_text = textlib.replaceCategoryLinks(new_text,
-                                                            cats,
-                                                            site=page.site)
             # Show the title of the page we're working on.
             # Highlight the title in purple.
             self.current_page = page
@@ -729,9 +730,11 @@
                  ('edit Latest', 'l'), ('open in Browser', 'b'),
                  ('More context', 'm'), ('All', 'a')],
                 default='N')
+
             if choice == 'm':
                 context = context * 3 if context else 3
                 continue
+
             if choice in ('e', 'l'):
                 text_editor = editor.TextEditor()
                 edit_text = original_text if choice == 'e' else new_text
@@ -739,32 +742,28 @@
                 # if user didn't press Cancel
                 if as_edited and as_edited != new_text:
                     new_text = as_edited
-                    if choice == 'l':
-                        # prevent changes from being applied again
-                        last_text = new_text
                 continue
+
             if choice == 'b':
+                # open in browser and leave
                 pywikibot.bot.open_webbrowser(page)
                 try:
-                    original_text = page.get(get_redirect=True, force=True)
+                    page.get(get_redirect=True, force=True)
                 except NoPageError:
                     pywikibot.info(f'Page {page.title()} has been deleted.')
-                    break
-                new_text = original_text
-                last_text = None
-                continue
+                return
+
+            if choice == 'n':
+                return
+
             if choice == 'a':
                 self.opt.always = True
-            if choice == 'y':
-                self.save(page, original_text, new_text, applied,
-                          show_diff=False, asynchronous=True)

-            # choice must be 'N'
+            # break if choice is 'y' or 'a' to save
             break

-        if self.opt.always and new_text != original_text:
-            self.save(page, original_text, new_text, applied,
-                      show_diff=False, asynchronous=False)
+        self.save(page, original_text, new_text, applied, show_diff=False,
+                  asynchronous=not self.opt.always)

     def save(self, page, oldtext, newtext, applied, **kwargs) -> None:
         """Save the given page."""

--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1022465?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.wikimedia.org/r/settings

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I57417bfaf22627a76cc61db5ba58c4c35e3d77fd
Gerrit-Change-Number: 1022465
Gerrit-PatchSet: 5
Gerrit-Owner: Xqt <i...@gno.de>
Gerrit-Reviewer: D3r1ck01 <dalangi-...@wikimedia.org>
Gerrit-Reviewer: Matěj Suchánek <matejsuchane...@gmail.com>
Gerrit-Reviewer: Xqt <i...@gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged
_______________________________________________
Pywikibot-commits mailing list -- pywikibot-commits@lists.wikimedia.org
To unsubscribe send an email to pywikibot-commits-le...@lists.wikimedia.org

Reply via email to