jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/327740 )
Change subject: newitem.py doesn't crash on LockedPage ......................................................................
newitem.py doesn't crash on LockedPage
Also: * handle NoPage and PageNotSaved errors * lines length below 79 characters * fix pycodestyle (former PEP8) E402 error
Bug: T152996 Change-Id: Id0949dc9162be28fdc20b5afd9e630f7564ba074 --- M scripts/newitem.py 1 file changed, 29 insertions(+), 12 deletions(-)
Approvals: Lokal Profil: Looks good to me, approved jenkins-bot: Verified
diff --git a/scripts/newitem.py b/scripts/newitem.py index c8f2700..9d83d45 100755 --- a/scripts/newitem.py +++ b/scripts/newitem.py @@ -20,20 +20,19 @@ """ # # (C) Multichill, 2014 -# (C) Pywikibot team, 2014 +# (C) Pywikibot team, 2014-2016 # # Distributed under the terms of the MIT license. # from __future__ import absolute_import, unicode_literals
-__version__ = '$Id$' -# - from datetime import timedelta
import pywikibot - from pywikibot import pagegenerators, WikidataBot +from pywikibot.exceptions import LockedPage, NoPage, PageNotSaved + +__version__ = '$Id$'
class NewItemRobot(WikidataBot): @@ -52,15 +51,32 @@ self.generator = pagegenerators.PreloadingGenerator(generator) self.pageAge = self.getOption('pageage') self.lastEdit = self.getOption('lastedit') - self.pageAgeBefore = self.repo.getcurrenttime() - timedelta(days=self.pageAge) - self.lastEditBefore = self.repo.getcurrenttime() - timedelta(days=self.lastEdit) + self.pageAgeBefore = self.repo.getcurrenttime() - timedelta( + days=self.pageAge) + self.lastEditBefore = self.repo.getcurrenttime() - timedelta( + days=self.lastEdit) self.treat_missing_item = True pywikibot.output('Page age is set to %s days so only pages created' '\nbefore %s will be considered.' % (self.pageAge, self.pageAgeBefore.isoformat())) - pywikibot.output('Last edit is set to %s days so only pages last edited' - '\nbefore %s will be considered.' - % (self.lastEdit, self.lastEditBefore.isoformat())) + pywikibot.output( + 'Last edit is set to {0} days so only pages last edited' + '\nbefore {1} will be considered.'.format( + self.lastEdit, self.lastEditBefore.isoformat())) + + @staticmethod + def _touch_page(page): + try: + page.touch() + except NoPage: + pywikibot.error('Page {0} does not exist.'.format( + page.title(asLink=True))) + except LockedPage: + pywikibot.error('Page {0} is locked.'.format( + page.title(asLink=True))) + except PageNotSaved: + pywikibot.error('Page {0} not saved.'.format( + page.title(asLink=True)))
def treat(self, page, item): """Treat page/item.""" @@ -68,7 +84,7 @@ pywikibot.output(u'%s already has an item: %s.' % (page, item)) if self.getOption('touch'): pywikibot.output(u'Doing a null edit on the page.') - page.put(page.text) + self._touch_page(page) return
self.current_page = page @@ -116,7 +132,7 @@ item = pywikibot.ItemPage(page.site.data_repository()) item.editEntity(data, summary=summary) # And do a null edit to force update - page.put(page.text) + self._touch_page(page)
def main(*args): @@ -153,5 +169,6 @@ bot.run() return True
+ if __name__ == "__main__": main()
pywikibot-commits@lists.wikimedia.org