jenkins-bot has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/844924 )

Change subject: [IMPR] Refactor delete.main() function
......................................................................

[IMPR] Refactor delete.main() function

- simplify arg parsing
- add a pagegenerators loop and create summary inside this loop.
  At this point -undelete and -summary options are already seen.
- first ask for -file option in pg loop because -file may also have
  a value argument. Unfortunately the summary creation still fails
  if the page title was not given with the option e.g. for -link or
  -imageused.
- use suggest_help() result to continue

Change-Id: I4f57bfc8808d5f19b0f99eaa5af391679adb554e
---
M scripts/delete.py
1 file changed, 26 insertions(+), 36 deletions(-)

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



diff --git a/scripts/delete.py b/scripts/delete.py
index c563213..449e741 100755
--- a/scripts/delete.py
+++ b/scripts/delete.py
@@ -224,8 +224,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.

@@ -234,6 +233,7 @@
     page_name = ''
     summary = None
     options = {}
+    pg_args = []

     # read command line parameters
     local_args = pywikibot.handle_args(args)
@@ -241,35 +241,31 @@
     mysite = pywikibot.Site()

     for arg in local_args:
-
-        if arg == '-always':
-            options['always'] = True
-        elif arg.startswith('-summary'):
-            if len(arg) == len('-summary'):
-                summary = pywikibot.input('Enter a reason for the deletion:')
-            else:
-                summary = arg[len('-summary:'):]
-        elif arg.startswith('-undelete'):
-            options['undelete'] = True
-        elif arg.startswith('-isorphan'):
-            options['isorphan'] = int(arg[10:]) if arg[10:] != '' else 0
-            if options['isorphan'] < 0:
-                options['isorphan'] = False
-        elif arg.startswith('-orphansonly'):
-            if arg[13:]:
-                namespaces = mysite.namespaces.resolve(arg[13:].split(','))
+        opt, _, value = arg.partition(':')
+        if opt in ('-always', '-undelete'):
+            options[opt[1:]] = True
+        elif opt == '-summary':
+            summary = value or pywikibot.input(
+                'Enter a reason for the deletion:')
+        elif opt == '-isorphan':
+            value = int(value or 0)
+            options[opt[1:]] = value if value >= 0 else False
+        elif opt == '-orphansonly':
+            if value:
+                namespaces = mysite.namespaces.resolve(value.split(','))
             else:
                 namespaces = mysite.namespaces
-            options['orphansonly'] = namespaces
+            options[opt[1:]] = namespaces
         else:
-            gen_factory.handle_arg(arg)
-            found = arg.find(':') + 1
-            if found:
-                page_name = arg[found:]
+            pg_args.append(arg)

-        if not summary:
-            un = 'un' if 'undelete' in options else ''
-            if page_name:
+    un = 'un' if 'undelete' in options else ''
+    for arg in pg_args:
+        *_, page_name = arg.partition(':')
+        if gen_factory.handle_arg(arg) and not summary:
+            if arg.startswith('-file'):
+                summary = i18n.twtranslate(mysite, un + 'delete-from-file')
+            elif page_name:
                 if arg.startswith(('-cat', '-subcats')):
                     summary = i18n.twtranslate(mysite, 'delete-from-category',
                                                {'page': page_name})
@@ -283,21 +279,15 @@
                 elif arg.startswith('-imageused'):
                     summary = i18n.twtranslate(mysite, un + 'delete-images',
                                                {'page': page_name})
-            elif arg.startswith('-file'):
-                summary = i18n.twtranslate(mysite, un + 'delete-from-file')

-    generator = gen_factory.getCombinedGenerator()
     # We are just deleting pages, so we have no need of using a preloading
     # page generator to actually get the text of those pages.
-    if generator:
+    generator = gen_factory.getCombinedGenerator()
+    if not pywikibot.bot.suggest_help(missing_generator=not generator):
         if summary is None:
-            summary = pywikibot.input('Enter a reason for the {}deletion:'
-                                      .format(['', 'un'][options
-                                              .get('undelete', False)]))
+            summary = pywikibot.input(f'Enter a reason for the {un}deletion:')
         bot = DeletionRobot(summary, generator=generator, **options)
         bot.run()
-    else:
-        pywikibot.bot.suggest_help(missing_generator=True)


 if __name__ == '__main__':

--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/844924
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: I4f57bfc8808d5f19b0f99eaa5af391679adb554e
Gerrit-Change-Number: 844924
Gerrit-PatchSet: 4
Gerrit-Owner: Xqt <[email protected]>
Gerrit-Reviewer: D3r1ck01 <[email protected]>
Gerrit-Reviewer: Matěj Suchánek <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to