jenkins-bot has submitted this change and it was merged.
Change subject: Positional args support for GeneratorFactory
......................................................................
Positional args support for GeneratorFactory
Some scripts have positional parameters which are used to create
a page generator. A common example is page names, which could be
written as -page:"..." but the scripts allow to be simple "...".
In order to bring these parameters into the common infrastructure,
support has been added for positional parameters being detected as
belonging to a standard page generator selected by the script.
Change-Id: I93f9289aa93ea7a263cd8347aa78136065cf4e6b
---
M pywikibot/pagegenerators.py
M scripts/imagetransfer.py
M scripts/solve_disambiguation.py
3 files changed, 23 insertions(+), 22 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/pagegenerators.py b/pywikibot/pagegenerators.py
index 521dd98..b115958 100644
--- a/pywikibot/pagegenerators.py
+++ b/pywikibot/pagegenerators.py
@@ -339,12 +339,15 @@
# When not in intersect mode, _filter_unique could be:
# functools.partial(filter_unique, container=global_seen_list)
- def __init__(self, site=None):
+ def __init__(self, site=None, positional_arg_name=None):
"""
Constructor.
@param site: Site for generator results.
@type site: L{pywikibot.site.BaseSite}
+ @param positional_arg_name: generator to use for positional args,
+ which do not begin with a hyphen
+ @type positional_arg_name: basestring
"""
self.gens = []
self._namespaces = []
@@ -358,6 +361,7 @@
self.intersect = False
self.subpage_max_depth = None
self._site = site
+ self._positional_arg_name = positional_arg_name
@property
def site(self):
@@ -569,6 +573,10 @@
can try parsing the argument. Call getCombinedGenerator() after all
arguments have been parsed to get the final output generator.
+ @param arg: Pywikibot argument consisting of -name:value
+ @type arg: basestring
+ @return: True if the argument supplied was recognised by the factory
+ @rtype: bool
"""
def intNone(v):
"""Return None if v is None or '' else return int(v)."""
@@ -579,6 +587,8 @@
value = None
gen = None
+ if not arg.startswith('-') and self._positional_arg_name:
+ arg = u'-{0}:{1}'.format(self._positional_arg_name, arg)
if arg == '-filelinks':
if not value:
diff --git a/scripts/imagetransfer.py b/scripts/imagetransfer.py
index ac6b898..f13f4cd 100755
--- a/scripts/imagetransfer.py
+++ b/scripts/imagetransfer.py
@@ -40,7 +40,7 @@
import pywikibot
-from pywikibot import config, i18n, textlib
+from pywikibot import config, i18n, pagegenerators, textlib
from scripts import upload
@@ -282,7 +282,6 @@
@param args: command line arguments
@type args: list of unicode
"""
- pageTitle = None
gen = None
interwiki = False
@@ -291,6 +290,8 @@
targetFamily = None
local_args = pywikibot.handle_args(args)
+ generator_factory = pagegenerators.GeneratorFactory(
+ positional_arg_name='page')
for arg in local_args:
if arg == '-interwiki':
@@ -301,13 +302,11 @@
targetLang = arg[8:]
elif arg.startswith('-tofamily:'):
targetFamily = arg[10:]
- elif not pageTitle:
- pageTitle = arg
+ else:
+ generator_factory.handleArg(arg)
- if pageTitle:
- page = pywikibot.Page(pywikibot.Site(), pageTitle)
- gen = iter([page])
- else:
+ gen = generator_factory.getCombinedGenerator()
+ if not gen:
pywikibot.bot.suggest_help(missing_parameters=['page'])
return False
diff --git a/scripts/solve_disambiguation.py b/scripts/solve_disambiguation.py
index 5f153ae..5886164 100755
--- a/scripts/solve_disambiguation.py
+++ b/scripts/solve_disambiguation.py
@@ -1084,7 +1084,6 @@
getAlternatives = True
dnSkip = False
generator = None
- pageTitle = None
primary = False
main_only = False
@@ -1092,6 +1091,8 @@
minimum = 0
local_args = pywikibot.handle_args(args)
+ generator_factory = pagegenerators.GeneratorFactory(
+ positional_arg_name='page')
for arg in local_args:
if arg.startswith('-primary:'):
@@ -1102,13 +1103,6 @@
primary = True
elif arg.startswith('-always:'):
always = arg[8:]
- elif arg.startswith('-file'):
- if len(arg) == 5:
- generator = pagegenerators.TextfilePageGenerator(
- filename=None)
- else:
- generator = pagegenerators.TextfilePageGenerator(
- filename=arg[6:])
elif arg.startswith('-pos:'):
if arg[5] != ':':
mysite = pywikibot.Site()
@@ -1139,14 +1133,12 @@
except pywikibot.NoPage:
pywikibot.output("Disambiguation category for your wiki is not known.")
raise
- elif not pageTitle:
- pageTitle = arg
+ else:
+ generator_factory.handleArg(arg)
site = pywikibot.Site()
- if pageTitle:
- page = pywikibot.Page(pywikibot.Link(pageTitle, site))
- generator = iter([page])
+ generator = generator_factory.getCombinedGenerator(generator)
if not generator:
pywikibot.bot.suggest_help(missing_generator=True)
--
To view, visit https://gerrit.wikimedia.org/r/179622
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I93f9289aa93ea7a263cd8347aa78136065cf4e6b
Gerrit-PatchSet: 7
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Merlijn van Deen <valhallasw(a)arctus.nl>
Gerrit-Reviewer: Ricordisamoa <ricordisamoa(a)openmailbox.org>
Gerrit-Reviewer: XZise <CommodoreFabianus(a)gmx.de>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot <>