jenkins-bot has submitted this change and it was merged. (
https://gerrit.wikimedia.org/r/154824 )
Change subject: standardize replace.py
......................................................................
standardize replace.py
- options dict instead of properties
Change-Id: I600816e5a297427159c5bbedad988c0dfd823868
---
M scripts/replace.py
M scripts/template.py
2 files changed, 35 insertions(+), 50 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/scripts/replace.py b/scripts/replace.py
index 49da2b5..cd8183f 100755
--- a/scripts/replace.py
+++ b/scripts/replace.py
@@ -534,8 +534,8 @@
@param recursive: Recurse replacement as long as possible.
@type recursive: bool
@warning: Be careful, this might lead to an infinite loop.
- @param addedCat: category to be added to every page touched
- @type addedCat: pywikibot.Category or str or None
+ @param addcat: category to be added to every page touched
+ @type addcat: pywikibot.Category or str or None
@param sleep: slow down between processing multiple regexes
@type sleep: int
@param summary: Set the summary message text bypassing the default
@@ -548,13 +548,17 @@
about the missing site
"""
- @deprecated_args(acceptall='always')
- def __init__(self, generator, replacements, exceptions={},
- allowoverlap=False, recursive=False, addedCat=None,
- sleep=None, summary='', **kwargs):
+ @deprecated_args(acceptall='always', addedCat='addcat')
+ def __init__(self, generator, replacements, exceptions={}, **kwargs):
"""Initializer."""
- super(ReplaceRobot, self).__init__(generator=generator,
- **kwargs)
+ self.availableOptions.update({
+ 'addcat': None,
+ 'allowoverlap': False,
+ 'recursive': False,
+ 'sleep': 0.0,
+ 'summary': None,
+ })
+ super(ReplaceRobot, self).__init__(generator=generator, **kwargs)
for i, replacement in enumerate(replacements):
if isinstance(replacement, Sequence):
@@ -567,17 +571,14 @@
replacement[1])
self.replacements = replacements
self.exceptions = exceptions
- self.allowoverlap = allowoverlap
- self.recursive = recursive
- if addedCat:
- if isinstance(addedCat, pywikibot.Category):
- self.addedCat = addedCat
- else:
- self.addedCat = pywikibot.Category(self.site, addedCat)
+ self.sleep = self.getOption('sleep')
+ self.summary = self.getOption('summary')
- self.sleep = sleep
- self.summary = summary
+ self.addcat = self.getOption('addcat')
+ if self.addcat and isinstance(self.addcat, UnicodeType):
+ self.addcat = pywikibot.Category(self.site, self.addcat)
+
self._pending_processed_titles = Queue()
def isTitleExcepted(self, title, exceptions=None):
@@ -648,7 +649,7 @@
new_text = textlib.replaceExcept(
new_text, replacement.old_regex, replacement.new,
exceptions + replacement.get_inside_exceptions(),
- allowoverlap=self.allowoverlap, site=self.site)
+ allowoverlap=self.getOption('allowoverlap'), site=self.site)
if old_text != new_text:
applied.add(replacement)
@@ -747,18 +748,18 @@
last_text = new_text
new_text = self.apply_replacements(last_text, applied,
page)
- if not self.recursive:
+ if not self.getOption('recursive'):
break
if new_text == original_text:
pywikibot.output('No changes were necessary in '
+ page.title(as_link=True))
break
- if hasattr(self, 'addedCat'):
+ if self.addcat:
# Fetch only categories in wikitext, otherwise the others
# will be explicitly added.
cats = textlib.getCategoryLinks(new_text, site=page.site)
- if self.addedCat not in cats:
- cats.append(self.addedCat)
+ if self.addcat not in cats:
+ cats.append(self.addcat)
new_text = textlib.replaceCategoryLinks(new_text,
cats,
site=page.site)
@@ -864,7 +865,7 @@
@param args: command line arguments
@type args: str
"""
- add_cat = None
+ options = {}
gen = None
# summary message
edit_summary = ''
@@ -892,18 +893,8 @@
xmlFilename = None
useSql = False
sql_query = None
- # will become True when the user presses a ('yes to all') or uses the
- # -always flag.
- acceptall = False
# Set the default regular expression flags
flags = re.UNICODE
- # Do all hits when they overlap
- allowoverlap = False
- # Do not recurse replacement
- recursive = False
- # Between a regex and another (using -fix) sleep some time (not to waste
- # too much CPU
- sleep = None
# Request manual replacements even if replacements are already defined
manual_input = False
# Replacements loaded from a file
@@ -951,11 +942,9 @@
elif arg.startswith('-fix:'):
fixes_set += [arg[5:]]
elif arg.startswith('-sleep:'):
- sleep = float(arg[7:])
- elif arg == '-always':
- acceptall = True
- elif arg == '-recursive':
- recursive = True
+ options['sleep'] = float(arg[7:])
+ elif arg in ('-always', '-recursive', -'allowoverlap'):
+ options[arg[1:]] = True
elif arg == '-nocase':
flags |= re.IGNORECASE
elif arg == '-dotall':
@@ -963,13 +952,11 @@
elif arg == '-multiline':
flags |= re.MULTILINE
elif arg.startswith('-addcat:'):
- add_cat = arg[8:]
+ options['addcat'] = arg[8:]
elif arg.startswith('-summary:'):
- edit_summary = arg[9:]
+ options['summary'] = arg[9:]
elif arg.startswith('-automaticsummary'):
edit_summary = True
- elif arg.startswith('-allowoverlap'):
- allowoverlap = True
elif arg.startswith('-manualinput'):
manual_input = True
elif arg.startswith('-replacementfile'):
@@ -1175,9 +1162,7 @@
pywikibot.bot.suggest_help(missing_generator=True)
return
- bot = ReplaceRobot(gen, replacements, exceptions,
- allowoverlap, recursive, add_cat, sleep, edit_summary,
- always=acceptall, site=site)
+ bot = ReplaceRobot(gen, replacements, exceptions, site=site, **options)
site.login()
bot.run()
diff --git a/scripts/template.py b/scripts/template.py
index b21581f..bce9b2d 100755
--- a/scripts/template.py
+++ b/scripts/template.py
@@ -109,7 +109,7 @@
# (C) Daniel Herding, 2004
# (C) Rob W.W. Hooft, 2003-2005
# (C) xqt, 2009-2018
-# (C) Pywikibot team, 2004-2019
+# (C) Pywikibot team, 2004-2020
#
# Distributed under the terms of the MIT license.
#
@@ -192,10 +192,10 @@
@type templates: dict
"""
self.availableOptions.update({
- 'subst': False,
+ 'addcat': None,
'remove': False,
+ 'subst': False,
'summary': None,
- 'addedCat': None,
})
SingleSiteBot.__init__(self, generator=generator, **kwargs)
@@ -262,7 +262,7 @@
super(TemplateRobot, self).__init__(
generator, replacements, exceptions,
always=self.getOption('always'),
- addedCat=self.getOption('addedCat'),
+ addcat=self.getOption('addcat'),
summary=self.getOption('summary'))
@@ -313,7 +313,7 @@
else:
xmlfilename = arg[5:]
elif arg.startswith('-addcat:'):
- options['addedCat'] = arg[len('-addcat:'):]
+ options['addcat'] = arg[len('-addcat:'):]
elif arg.startswith('-summary:'):
options['summary'] = arg[len('-summary:'):]
elif arg.startswith('-onlyuser:'):
--
To view, visit
https://gerrit.wikimedia.org/r/154824
To unsubscribe, or for help writing mail filters, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: I600816e5a297427159c5bbedad988c0dfd823868
Gerrit-Change-Number: 154824
Gerrit-PatchSet: 22
Gerrit-Owner: Ricordisamoa <ricordisamoa(a)disroot.org>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Merlijn van Deen <valhallasw(a)arctus.nl>
Gerrit-Reviewer: Mpaa <mpaa.wiki(a)gmail.com>
Gerrit-Reviewer: Ricordisamoa <ricordisamoa(a)disroot.org>
Gerrit-Reviewer: XZise <CommodoreFabianus(a)gmx.de>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot (75)