jenkins-bot has submitted this change and it was merged.
Change subject: new Bot.current_page property to set and log the working page ......................................................................
new Bot.current_page property to set and log the working page
Nearly every script that 'treats' a page used to print its title on the screen, sometimes in purple, sometimes prepended with "Processing..." or "Working on...". Now the setter uses the standard output for the colorized message and the logging.VERBOSE level for the %r-formatted string, improving consistency and semantic value of the console output.
This property also allows to get the current working page at any time, and to avoid printing the same title twice.
Change-Id: I886c48c7bf7b66917be814df3833f70caa22bd4a --- M pywikibot/bot.py M scripts/capitalize_redirects.py M scripts/claimit.py M scripts/commons_link.py M scripts/commonscat.py M scripts/coordinate_import.py M scripts/cosmetic_changes.py M scripts/delete.py M scripts/harvest_template.py M scripts/illustrate_wikidata.py M scripts/movepages.py M scripts/newitem.py M scripts/noreferences.py M scripts/nowcommons.py M scripts/pagefromfile.py M scripts/protect.py M scripts/selflink.py M scripts/solve_disambiguation.py M scripts/unlink.py 19 files changed, 48 insertions(+), 48 deletions(-)
Approvals: John Vandenberg: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/bot.py b/pywikibot/bot.py index 53e3733..b332099 100644 --- a/pywikibot/bot.py +++ b/pywikibot/bot.py @@ -796,6 +796,8 @@ 'always': False, # ask for confirmation when putting a page? }
+ _current_page = None + def __init__(self, **kwargs): """ Only accept options defined in availableOptions. @@ -836,6 +838,33 @@ except KeyError: raise pywikibot.Error(u'%s is not a valid bot option.' % option)
+ @property + def current_page(self): + return self._current_page + + @current_page.setter + def current_page(self, page): + """Set the current working page as a property. + + When the value is actually changed, the page title is printed + to the standard output (highlighted in purple) and logged + with a VERBOSE level. + + This also prevents the same title from being printed twice. + + @param page: the working page + @type page: pywikibot.Page + """ + if page != self._current_page: + self._current_page = page + msg = u'Working on %r' % page + if config.colorized_output: + log(msg) + stdout(u'\n\n>>> \03{lightpurple}%s\03{default} <<<' + % page.title()) + else: + stdout(msg) + def user_confirm(self, question): """ Obtain user response if bot option 'always' not enabled. """ if self.getOption('always'): @@ -871,8 +900,7 @@ % page.title(asLink=True)) return
- pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" - % page.title()) + self.current_page = page pywikibot.showDiff(oldtext, newtext) if 'comment' in kwargs: pywikibot.output(u'Comment: %s' % kwargs['comment']) diff --git a/scripts/capitalize_redirects.py b/scripts/capitalize_redirects.py index 4db3e40..0c4c7a9 100644 --- a/scripts/capitalize_redirects.py +++ b/scripts/capitalize_redirects.py @@ -54,10 +54,7 @@ if page.isRedirectPage(): page = page.getRedirectTarget() page_t = page.title() - # Show the title of the page we're working on. - # Highlight the title in purple. - pywikibot.output(u"\n>>> \03{lightpurple}%s\03{default} <<<" - % page_t) + self.current_page = page if self.getOption('titlecase'): page_cap = pywikibot.Page(page.site, page_t.title()) else: diff --git a/scripts/claimit.py b/scripts/claimit.py index 443f628..39fcf11 100755 --- a/scripts/claimit.py +++ b/scripts/claimit.py @@ -90,7 +90,7 @@ if self.exists_arg: pywikibot.output(''exists' argument set to '%s'' % self.exists_arg) for page in self.generator: - pywikibot.output('Processing %s' % page) + self.current_page = page item = pywikibot.ItemPage.fromPage(page) if not item.exists(): # TODO FIXME: We should provide an option to create the page diff --git a/scripts/commons_link.py b/scripts/commons_link.py index 79b4580..45887c0 100644 --- a/scripts/commons_link.py +++ b/scripts/commons_link.py @@ -60,7 +60,7 @@ catmode = (self.getOption('action') == 'categories') for page in self.generator: try: - pywikibot.output(u'\n>>>> %s <<<<' % page.title()) + self.current_page = page commons = page.site.image_repository() commonspage = getattr(pywikibot, ('Page', 'Category')[catmode] diff --git a/scripts/commonscat.py b/scripts/commonscat.py index 48f9591..5029830 100755 --- a/scripts/commonscat.py +++ b/scripts/commonscat.py @@ -293,7 +293,7 @@ category is found add it to the page.
""" - pywikibot.output(u'Working on ' + page.title()) + self.current_page = page # Get the right templates for this page primaryCommonscat, commonscatAlternatives = self.getCommonscatTemplate( page.site.code) diff --git a/scripts/coordinate_import.py b/scripts/coordinate_import.py index 1998cf8..322db55 100644 --- a/scripts/coordinate_import.py +++ b/scripts/coordinate_import.py @@ -63,7 +63,7 @@ def run(self): """Start the robot.""" for page in self.generator: - pywikibot.output(u'Working on %s' % page.title()) + self.current_page = page item = pywikibot.ItemPage.fromPage(page)
if item.exists(): diff --git a/scripts/cosmetic_changes.py b/scripts/cosmetic_changes.py index b6ba0e1..9e87462 100755 --- a/scripts/cosmetic_changes.py +++ b/scripts/cosmetic_changes.py @@ -845,10 +845,7 @@
def treat(self, page): try: - # Show the title of the page we're working on. - # Highlight the title in purple. - pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" - % page.title()) + self.current_page = page ccToolkit = CosmeticChangesToolkit(page.site, debug=True, namespace=page.namespace(), pageTitle=page.title()) diff --git a/scripts/delete.py b/scripts/delete.py index 3211852..2efe8e0 100644 --- a/scripts/delete.py +++ b/scripts/delete.py @@ -68,7 +68,7 @@
""" for page in self.generator: - pywikibot.output(u'Processing page %s' % page.title()) + self.current_page = page if self.getOption('undelete'): page.undelete(self.summary) else: diff --git a/scripts/harvest_template.py b/scripts/harvest_template.py index fd61850..d26245b 100755 --- a/scripts/harvest_template.py +++ b/scripts/harvest_template.py @@ -109,7 +109,7 @@ def processPage(self, page): """Process a single page.""" item = pywikibot.ItemPage.fromPage(page) - pywikibot.output('Processing %s' % page) + self.current_page = page if not item.exists(): pywikibot.output('%s doesn't have a wikidata item :(' % page) #TODO FIXME: We should provide an option to create the page diff --git a/scripts/illustrate_wikidata.py b/scripts/illustrate_wikidata.py index a1b25f9..21b1a36 100644 --- a/scripts/illustrate_wikidata.py +++ b/scripts/illustrate_wikidata.py @@ -50,7 +50,7 @@ def run(self): """Starts the bot.""" for page in self.generator: - pywikibot.output(u'Working on %s' % page.title()) + self.current_page = page item = pywikibot.ItemPage.fromPage(page)
if item.exists(): diff --git a/scripts/movepages.py b/scripts/movepages.py index 618ac26..3cdc360 100644 --- a/scripts/movepages.py +++ b/scripts/movepages.py @@ -89,10 +89,7 @@ pywikibot.output(e.message)
def treat(self, page): - # Show the title of the page we're working on. - # Highlight the title in purple. - pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" - % page.title()) + self.current_page = page if self.getOption('skipredirects') and page.isRedirectPage(): pywikibot.output(u'Page %s is a redirect; skipping.' % page.title()) return diff --git a/scripts/newitem.py b/scripts/newitem.py index 7169de4..99e83dd 100644 --- a/scripts/newitem.py +++ b/scripts/newitem.py @@ -60,7 +60,7 @@ % (self.lastEdit, self.lastEditBefore.isoformat()))
for page in self.generator: - pywikibot.output('Processing %s' % page) + self.current_page = page if not page.exists(): pywikibot.output(u'%s does not exist anymore. Skipping...' % page) diff --git a/scripts/noreferences.py b/scripts/noreferences.py index dde9bfd..8fece93 100755 --- a/scripts/noreferences.py +++ b/scripts/noreferences.py @@ -610,10 +610,7 @@ def run(self):
for page in self.generator: - # Show the title of the page we're working on. - # Highlight the title in purple. - pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" - % page.title()) + self.current_page = page try: text = page.text except pywikibot.NoPage: diff --git a/scripts/nowcommons.py b/scripts/nowcommons.py index 4e5338f..ef6d436 100644 --- a/scripts/nowcommons.py +++ b/scripts/nowcommons.py @@ -315,10 +315,7 @@ page = pywikibot.Page(self.site, images_list[0]) else: # If use_hash is true, we have already print this before, no need - # Show the title of the page we're working on. - # Highlight the title in purple. - pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" - % page.title()) + self.current_page = page try: localImagePage = pywikibot.FilePage(self.site, page.title()) if localImagePage.fileIsShared(): diff --git a/scripts/pagefromfile.py b/scripts/pagefromfile.py index 7c2f482..7b204c2 100644 --- a/scripts/pagefromfile.py +++ b/scripts/pagefromfile.py @@ -94,10 +94,7 @@ mysite = pywikibot.Site()
page = pywikibot.Page(mysite, title) - # Show the title of the page we're working on. - # Highlight the title in purple. - pywikibot.output(u">>> \03{lightpurple}%s\03{default} <<<" - % page.title()) + self.current_page = page
if self.getOption('summary'): comment = self.getOption('summary') diff --git a/scripts/protect.py b/scripts/protect.py index f3f4fc5..7a20ed1 100644 --- a/scripts/protect.py +++ b/scripts/protect.py @@ -89,7 +89,7 @@ protections. """ for page in self.generator: - pywikibot.output(u'Processing page %s' % page.title()) + self.current_page = page if not self.getOption('always'): choice = pywikibot.inputChoice( u'Do you want to change the protection level of %s?' diff --git a/scripts/selflink.py b/scripts/selflink.py index ee436bc..8ce58e2 100644 --- a/scripts/selflink.py +++ b/scripts/selflink.py @@ -138,10 +138,7 @@ return False
def treat(self, page): - # Show the title of the page we're working on. - # Highlight the title in purple. - pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" - % page.title()) + self.current_page = page try: oldText = page.text # Inside image maps, don't touch selflinks, as they're used diff --git a/scripts/solve_disambiguation.py b/scripts/solve_disambiguation.py index a10c069..c6d86a2 100644 --- a/scripts/solve_disambiguation.py +++ b/scripts/solve_disambiguation.py @@ -637,11 +637,7 @@ # This loop will run while the user doesn't choose an option # that will actually change the page while True: - # Show the title of the page where the link was found. - # Highlight the title in purple. - pywikibot.output( - u"\n\n>>> \03{lightpurple}%s\03{default} <<<" - % refPage.title()) + self.current_page = refPage
if not self.always: # at the beginning of the link, start red color. diff --git a/scripts/unlink.py b/scripts/unlink.py index 862cd36..b9525ce 100755 --- a/scripts/unlink.py +++ b/scripts/unlink.py @@ -133,10 +133,7 @@ return text[:match.start()] + new + text[match.end():], False
def treat(self, page): - # Show the title of the page we're working on. - # Highlight the title in purple. - pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" - % page.title()) + self.current_page = page try: oldText = page.get() text = oldText
pywikibot-commits@lists.wikimedia.org