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

Change subject: [IMPR] move code parts from SandboxBot.run() to 
SandboxBot.tread()
......................................................................

[IMPR] move code parts from SandboxBot.run() to SandboxBot.tread()

- move code parts from SandboxBot.run() to SandboxBot.tread()
  and decrease nested flow statements

Change-Id: Ia7e5f3312140aa1450a499dc9d918ca021d0f8ce
---
M scripts/clean_sandbox.py
1 file changed, 63 insertions(+), 66 deletions(-)

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




diff --git a/scripts/clean_sandbox.py b/scripts/clean_sandbox.py
index 44fb69d..7f23e6a 100755
--- a/scripts/clean_sandbox.py
+++ b/scripts/clean_sandbox.py
@@ -56,7 +56,7 @@
 import pywikibot
 from pywikibot import i18n, pagegenerators
 from pywikibot.bot import Bot, ConfigParserBot
-from pywikibot.exceptions import EditConflictError, NoPageError
+from pywikibot.exceptions import EditConflictError


 content = {
@@ -227,77 +227,75 @@

             self.generator = pages

+    def treat(self, page):
+        """Treat a single page."""
+        if not page.exists():
+            pywikibot.info('*** The sandbox is not existent, skipping.')
+            return
+
+        pywikibot.info(f'Preparing to process sandbox page {page}')
+        if page.isRedirectPage():
+            pywikibot.warning(f'{page.title(as_link=True)} is a redirect'
+                              ' page, cleaning it anyway')
+        text = page.text
+        if self.opt.summary:
+            translated_msg = self.opt.summary
+        else:
+            translated_msg = i18n.twtranslate(
+                self.site, 'clean_sandbox-cleaned')
+
+        subst = 'subst:' in self.translated_content
+        pos = text.find(self.translated_content.strip())
+        latest_user = page.latest_revision.user
+
+        try:
+            if text.strip() == self.translated_content.strip():
+                pywikibot.info(
+                    'The sandbox is still clean, no change necessary.')
+            elif subst and latest_user == self.site.user():
+                pywikibot.info(
+                    'The sandbox might be clean, no change necessary.')
+            elif pos != 0 and not subst:
+                page.put(self.translated_content, translated_msg)
+                pywikibot.showDiff(text, self.translated_content)
+                pywikibot.info(
+                    'Standard content was changed, sandbox cleaned.')
+            else:
+                edit_delta = (pywikibot.Timestamp.utcnow()
+                              - page.latest_revision.timestamp)
+                delta = self.delay_td - edit_delta
+                # Is the last edit more than 'delay' minutes ago?
+                if delta <= datetime.timedelta(0):
+                    page.put(self.translated_content, translated_msg)
+                    pywikibot.showDiff(text, self.translated_content)
+                    pywikibot.info('Standard content was changed, '
+                                   'sandbox cleaned.')
+                else:  # wait for the rest
+                    pywikibot.info(
+                        'Sandbox edited '
+                        f'{edit_delta.seconds / 60.0:.1f} minutes ago...'
+                    )
+                    pywikibot.info(
+                        f'Sleeping for {delta.seconds // 60} minutes.')
+                    pywikibot.sleep(delta.seconds)
+                    self.wait = True
+        except EditConflictError:
+            pywikibot.info('*** Skipping because of an edit conflict.\n')
+
     def run(self) -> None:
         """Run bot."""
         self.site.login()
         while True:
-            wait = False
+            self.wait = False
             now = time.strftime('%d %b %Y %H:%M:%S (UTC)', time.gmtime())
             for sandbox_page in self.generator:
-                pywikibot.info('Preparing to process sandbox page '
-                               + sandbox_page.title(as_link=True))
-                if sandbox_page.isRedirectPage():
-                    pywikibot.warning(
-                        f'{sandbox_page.title(as_link=True)} is a redirect'
-                        ' page, cleaning it anyway'
-                    )
-                try:
-                    text = sandbox_page.text
-                    if self.opt.summary:
-                        translated_msg = self.opt.summary
-                    else:
-                        translated_msg = i18n.twtranslate(
-                            self.site, 'clean_sandbox-cleaned')
-
-                    subst = 'subst:' in self.translated_content
-                    pos = text.find(self.translated_content.strip())
-                    latest_user = sandbox_page.latest_revision.user
-                    if text.strip() == self.translated_content.strip():
-                        pywikibot.info(
-                            'The sandbox is still clean, no change necessary.')
-                    elif subst and latest_user == self.site.user():
-                        pywikibot.info(
-                            'The sandbox might be clean, no change necessary.')
-                    elif pos != 0 and not subst:
-                        sandbox_page.put(self.translated_content,
-                                         translated_msg)
-                        pywikibot.showDiff(text, self.translated_content)
-                        pywikibot.info(
-                            'Standard content was changed, sandbox cleaned.')
-                    else:
-                        edit_delta = (pywikibot.Timestamp.utcnow()
-                                      - sandbox_page.latest_revision.timestamp)
-                        delta = self.delay_td - edit_delta
-                        # Is the last edit more than 'delay' minutes ago?
-                        if delta <= datetime.timedelta(0):
-                            sandbox_page.put(
-                                self.translated_content, translated_msg)
-                            pywikibot.showDiff(text, self.translated_content)
-                            pywikibot.info('Standard content was changed, '
-                                           'sandbox cleaned.')
-                        else:  # wait for the rest
-                            pywikibot.info(
-                                'Sandbox edited '
-                                f'{edit_delta.seconds / 60.0:.1f} minutes'
-                                ' ago...'
-                            )
-                            pywikibot.info(
-                                f'Sleeping for {delta.seconds // 60} minutes.')
-                            pywikibot.sleep(delta.seconds)
-                            wait = True
-                except EditConflictError:
-                    pywikibot.info(
-                        '*** Loading again because of edit conflict.\n')
-                except NoPageError:
-                    pywikibot.info(
-                        '*** The sandbox is not existent, skipping.')
-                    continue
+                self.treat(sandbox_page)

             if self.opt.hours < 0:
                 pywikibot.info('\nDone.')
                 return

-            if not wait:
+            if not self.wait:
                 if self.opt.hours < 1.0:
                     pywikibot.info(
                         f'\nSleeping {self.opt.hours * 60} minutes, now {now}')
@@ -308,8 +306,7 @@


 def main(*args: str) -> None:
-    """
-    Process command line arguments and invoke bot.
+    """Process command line arguments and invoke bot.

     If args is an empty list, sys.argv is used.

@@ -321,10 +318,10 @@
     gen_factory = pagegenerators.GeneratorFactory()
     for arg in local_args:
         opt, _, value = arg.partition(':')
-        if opt.startswith('-'):
-            opt = opt[1:]
-        else:
+        if not opt.startswith('-'):
             continue
+
+        opt = opt[1:]
         if opt == 'hours':
             opts[opt] = float(value)
         elif opt == 'delay':

--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1078090?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: Ia7e5f3312140aa1450a499dc9d918ca021d0f8ce
Gerrit-Change-Number: 1078090
Gerrit-PatchSet: 3
Gerrit-Owner: Xqt <i...@gno.de>
Gerrit-Reviewer: D3r1ck01 <dalangi-...@wikimedia.org>
Gerrit-Reviewer: Xqt <i...@gno.de>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
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