jenkins-bot has submitted this change. (
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1167531?usp=email )
Change subject: IMPR: Refactor Subject.replaceLinks and decrease complexity
......................................................................
IMPR: Refactor Subject.replaceLinks and decrease complexity
- Move page checks to _fetch_text helper method
Change-Id: I774c86aba6189bf15f366a53f158d65673ac9007
---
M scripts/interwiki.py
1 file changed, 21 insertions(+), 4 deletions(-)
Approvals:
jenkins-bot: Verified
Xqt: Looks good to me, approved
diff --git a/scripts/interwiki.py b/scripts/interwiki.py
index e2b5f60..06af442 100755
--- a/scripts/interwiki.py
+++ b/scripts/interwiki.py
@@ -1528,16 +1528,27 @@
except GiveUpOnPage:
break
- def replaceLinks(self, page, newPages) -> bool:
- """Return True if saving was successful."""
+ def _fetch_text(self, page: pywikibot.Page) -> str:
+ """Validate page and load it's content for editing.
+
+ This includes checking for:
+ - `-localonly` flag and whether the page is the origin
+ - Section-only pages (pages with `#section`)
+ - Non-existent pages
+ - Empty pages
+
+ :param page: The page to check.
+ :return: The text content of the page if it passes all checks.
+ :raises SaveError: If the page is not eligible for editing.
+ """
# In this case only continue on the Page we started with
if self.conf.localonly and page != self.origin:
raise SaveError('-localonly and page != origin')
if page.section():
# This is not a page, but a subpage. Do not edit it.
- pywikibot.info(
- f'Not editing {page}: not doing interwiki on subpages')
+ pywikibot.info(f'Not editing {page}: interwiki not done on'
+ ' subpages (#section)')
raise SaveError('Link has a #section')
try:
@@ -1550,6 +1561,12 @@
pywikibot.info(f'Not editing {page}: page is empty')
raise SaveError('Page is empty.')
+ return pagetext
+
+ def replaceLinks(self, page, newPages) -> bool:
+ """Return True if saving was successful."""
+ pagetext = self._fetch_text(page)
+
# clone original newPages dictionary, so that we can modify it to the
# local page's needs
new = newPages.copy()
--
To view, visit
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1167531?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: I774c86aba6189bf15f366a53f158d65673ac9007
Gerrit-Change-Number: 1167531
Gerrit-PatchSet: 5
Gerrit-Owner: Xqt <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
Gerrit-CC: Matěj Suchánek <[email protected]>
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]