jenkins-bot submitted this change.

View Change

Approvals: Matěj Suchánek: Looks good to me, but someone else must approve JJMC89: Looks good to me, approved jenkins-bot: Verified
[IMPR] Implement pagegenerators.handle_args() to process all options at once

- Implement pagegenerators.handle_args() which handles all pagegenerators
options and return a list of the remaining options
- Update several scripts using new pagegenerators.handle_args()
- test handle_args in RedirectFilterPageGenerator

Change-Id: I8ee233c9c5e1c82c0b3d224702c2884dab2b85a1
---
M pywikibot/pagegenerators.py
M scripts/basic.py
M scripts/coordinate_import.py
M scripts/interwikidata.py
M scripts/movepages.py
M scripts/ndashredir.py
M scripts/replace.py
M scripts/touch.py
M tests/pagegenerators_tests.py
9 files changed, 31 insertions(+), 33 deletions(-)

diff --git a/pywikibot/pagegenerators.py b/pywikibot/pagegenerators.py
index 78d7e01..63d27a7 100644
--- a/pywikibot/pagegenerators.py
+++ b/pywikibot/pagegenerators.py
@@ -432,6 +432,9 @@

This factory is responsible for processing command line arguments
that are used by many scripts and that determine which pages to work on.
+
+ :Note: GeneratorFactory must be instantiated after global arguments are
+ parsed except if site parameter is given.
"""

def __init__(self, site=None,
@@ -1206,6 +1209,10 @@
'Invalid -logevents parameter "{0}"'.format(params[0]))
return self._parse_log_events(*params)

+ def handle_args(self, args: Iterable[str]) -> List[str]:
+ """Handle command line arguments and return the rest as a list."""
+ return [arg for arg in args if not self.handle_arg(arg)]
+
def handle_arg(self, arg: str) -> bool:
"""Parse one argument at a time.

diff --git a/scripts/basic.py b/scripts/basic.py
index f56e38f..fadc42d 100755
--- a/scripts/basic.py
+++ b/scripts/basic.py
@@ -156,14 +156,11 @@
# to work on.
gen_factory = pagegenerators.GeneratorFactory()

- # Parse command line arguments
+ # Process pagegenerators arguments
+ local_args = gen_factory.handle_args(local_args)
+
+ # Parse your own command line arguments
for arg in local_args:
-
- # Catch the pagegenerators options
- if gen_factory.handle_arg(arg):
- continue # nothing to do here
-
- # Now pick up your own options
arg, sep, value = arg.partition(':')
option = arg[1:]
if option in ('summary', 'text'):
diff --git a/scripts/coordinate_import.py b/scripts/coordinate_import.py
index 565dd5e..c75276b 100755
--- a/scripts/coordinate_import.py
+++ b/scripts/coordinate_import.py
@@ -160,10 +160,11 @@
local_args = pywikibot.handle_args(args)
generator_factory = pagegenerators.GeneratorFactory()

+ # Process pagegenerators args
+ local_args = generator_factory.handle_args(local_args)
+
create_new = False
for arg in local_args:
- if generator_factory.handle_arg(arg):
- continue
if arg == '-create':
create_new = True

diff --git a/scripts/interwikidata.py b/scripts/interwikidata.py
index 370614b..acf398d 100755
--- a/scripts/interwikidata.py
+++ b/scripts/interwikidata.py
@@ -22,7 +22,7 @@
-summary: Use your own edit summary for cleaning the page.
"""

-# (C) Pywikibot team, 2015-2020
+# (C) Pywikibot team, 2015-2021
#
# Distributed under the terms of the MIT license.
#
@@ -221,10 +221,10 @@
"""
local_args = pywikibot.handle_args(args)
gen_factory = pagegenerators.GeneratorFactory()
+ local_args = gen_factory.handle_args(local_args)
+
options = {}
for arg in local_args:
- if gen_factory.handle_arg(arg):
- continue
option, sep, value = arg.partition(':')
option = option[1:] if option.startswith('-') else None
if option == 'summary':
diff --git a/scripts/movepages.py b/scripts/movepages.py
index 0182af2..0081486 100755
--- a/scripts/movepages.py
+++ b/scripts/movepages.py
@@ -31,7 +31,7 @@

"""
#
-# (C) Pywikibot team, 2006-2020
+# (C) Pywikibot team, 2006-2021
#
# Distributed under the terms of the MIT license.
#
@@ -187,10 +187,9 @@
# Process global args and prepare generator args parser
local_args = pywikibot.handle_args(args)
genFactory = pagegenerators.GeneratorFactory()
+ local_args = genFactory.handle_args(local_args)

for arg in local_args:
- if genFactory.handle_arg(arg):
- continue
if arg.startswith('-pairsfile'):
if len(arg) == len('-pairsfile'):
filename = pywikibot.input(
diff --git a/scripts/ndashredir.py b/scripts/ndashredir.py
index b5f8068..3b5d350 100644
--- a/scripts/ndashredir.py
+++ b/scripts/ndashredir.py
@@ -25,7 +25,7 @@
&params;
"""
#
-# (C) Pywikibot team, 2012-2020
+# (C) Pywikibot team, 2012-2021
#
# Distributed under the terms of the MIT license.
#
@@ -129,14 +129,11 @@
# to work on.
gen_factory = pagegenerators.GeneratorFactory()

+ # Process the pagegenerators options
+ local_args = gen_factory.handle_args(local_args)
+
# Parse command line arguments
for arg in local_args:
-
- # Catch the pagegenerators options
- if gen_factory.handle_arg(arg):
- continue # nothing to do here
-
- # Now pick up custom options
arg, sep, value = arg.partition(':')
option = arg[1:]
if option == 'summary':
diff --git a/scripts/replace.py b/scripts/replace.py
index 4559626..546e037 100755
--- a/scripts/replace.py
+++ b/scripts/replace.py
@@ -135,7 +135,7 @@
the top of the help.
"""
#
-# (C) Pywikibot team, 2004-2020
+# (C) Pywikibot team, 2004-2021
#
# Distributed under the terms of the MIT license.
#
@@ -832,13 +832,11 @@
replacement_file_arg_misplaced = False

# Read commandline parameters.
-
local_args = pywikibot.handle_args(args)
genFactory = pagegenerators.GeneratorFactory()
+ local_args = genFactory.handle_args(local_args)

for arg in local_args:
- if genFactory.handle_arg(arg):
- continue
if arg == '-regex':
regex = True
elif arg.startswith('-xmlstart'):
diff --git a/scripts/touch.py b/scripts/touch.py
index 65eff2e..89051b1 100755
--- a/scripts/touch.py
+++ b/scripts/touch.py
@@ -23,7 +23,7 @@
&params;
"""
#
-# (C) Pywikibot team, 2009-2020
+# (C) Pywikibot team, 2009-2021
#
# Distributed under the terms of the MIT license.
#
@@ -91,14 +91,13 @@
"""
options = {}

- # Process global args and prepare generator args parser
+ # Process global and pagegenerators args
local_args = pywikibot.handle_args(args)
gen_factory = pagegenerators.GeneratorFactory()
+ local_args = gen_factory.handle_args(local_args)

bot_class = TouchBot
for arg in local_args:
- if gen_factory.handle_arg(arg):
- continue
if arg == '-purge':
bot_class = PurgeBot
elif arg.startswith('-'):
diff --git a/tests/pagegenerators_tests.py b/tests/pagegenerators_tests.py
index b01bfbc..b88a87d 100755
--- a/tests/pagegenerators_tests.py
+++ b/tests/pagegenerators_tests.py
@@ -324,11 +324,11 @@
code = 'en'

def test_redirect_filter(self):
- """Test RedirectFilterPageGenerator."""
+ """Test RedirectFilterPageGenerator with handle_args()."""
from pywikibot.pagegenerators import RedirectFilterPageGenerator
gf = pagegenerators.GeneratorFactory(site=self.site)
- gf.handle_arg('-randomredirect:3')
- gf.handle_arg('-page:Main_Page')
+ args = gf.handle_args(['-randomredirect:3', '-page:Main_Page'])
+ self.assertIsEmpty(args)
gen = gf.getCombinedGenerator()
pages = list(gen)
gen = RedirectFilterPageGenerator(pages, no_redirects=True)

To view, visit change 660633. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I8ee233c9c5e1c82c0b3d224702c2884dab2b85a1
Gerrit-Change-Number: 660633
Gerrit-PatchSet: 5
Gerrit-Owner: Xqt <info@gno.de>
Gerrit-Reviewer: D3r1ck01 <xsavitar.wiki@aol.com>
Gerrit-Reviewer: JJMC89 <JJMC89.Wikimedia@gmail.com>
Gerrit-Reviewer: Matěj Suchánek <matejsuchanek97@gmail.com>
Gerrit-Reviewer: Mpaa <mpaa.wiki@gmail.com>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged