Ricordisamoa has uploaded a new change for review. https://gerrit.wikimedia.org/r/137334
Change subject: improvements to pagefromfile.py ...................................................................... improvements to pagefromfile.py - PageFromFileRobot extends pywikibot.Bot using an options dict - use config.default_edit_summary setter instead of .setAction() - change page.put() to page.save() and self.put() to self.save() - "Top" and "Bottom" values are now lowercase Change-Id: Ie46eb9a003c9893a24dc01fe0b0cbf854f367187 --- M scripts/pagefromfile.py 1 file changed, 32 insertions(+), 36 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core refs/changes/34/137334/1 diff --git a/scripts/pagefromfile.py b/scripts/pagefromfile.py index badc72b..b7d961d 100644 --- a/scripts/pagefromfile.py +++ b/scripts/pagefromfile.py @@ -1,5 +1,5 @@ #!/usr/bin/python -#coding: utf-8 +# -*- coding: utf-8 -*- """ This bot takes its input from a file that contains a number of pages to be put on the wiki. The pages should all have the same @@ -52,7 +52,7 @@ import re import codecs import pywikibot -from pywikibot import config +from pywikibot import config, Bot class NoTitle(Exception): @@ -61,7 +61,7 @@ self.offset = offset -class PageFromFileRobot: +class PageFromFileRobot(Bot): """ Responsible for writing pages to the wiki, with the titles and contents given by a PageFromFileReader. @@ -146,21 +146,24 @@ 'zh': u'機器人: 覆寫已存在的文字', } - def __init__(self, reader, force, append, summary, minor, autosummary, - nocontent): + def __init__(self, reader, **kwargs): + self.availableOptions.update({ + 'force': False, + 'append': None, + 'summary': None, + 'minor': False, + 'autosummary': False, + 'nocontent': '', + }) + + super(PageFromFileRobot, self).__init__(**kwargs) self.reader = reader - self.force = force - self.append = append - self.summary = summary - self.minor = minor - self.autosummary = autosummary - self.nocontent = nocontent def run(self): for title, contents in self.reader.run(): - self.put(title, contents) + self.save(title, contents) - def put(self, title, contents): + def save(self, title, contents): mysite = pywikibot.Site() page = pywikibot.Page(mysite, title) @@ -194,12 +197,12 @@ if pagecontents.find(self.nocontent) != -1 or pagecontents.find(self.nocontent.lower()) != -1: pywikibot.output(u'Page has %s so it is skipped' % (self.nocontent)) return - if self.append == "Top": + if self.append == 'top': pywikibot.output(u"Page %s already exists, appending on top!" % title) contents = contents + page.get() comment = comment_top - elif self.append == "Bottom": + elif self.append == 'bottom': pywikibot.output(u"Page %s already exists, appending on bottom!" % title) contents = page.get() + contents @@ -214,9 +217,10 @@ else: if self.autosummary: comment = '' - pywikibot.setAction('') + config.default_edit_summary = '' try: - page.put(contents, comment=comment, minorEdit=self.minor) + page.text = contents + page.save(comment, minorEdit=self.minor) except pywikibot.LockedPage: pywikibot.output(u"Page %s is locked; skipping." % title) except pywikibot.EditConflict: @@ -305,14 +309,9 @@ pageEndMarker = "{{-stop-}}" titleStartMarker = u"'''" titleEndMarker = u"'''" - nocontent = u"" + options = {} include = False - force = False - append = None notitle = False - summary = None - minor = False - autosummary = False for arg in pywikibot.handleArgs(): if arg.startswith("-start:"): @@ -323,37 +322,34 @@ filename = arg[6:] elif arg == "-include": include = True - elif arg == "-appendtop": - append = "Top" - elif arg == "-appendbottom": - append = "Bottom" + elif arg.startswith('-append') and arg[7:] in ('top', 'bottom'): + options['append'] = arg[7:] elif arg == "-force": - force = True + options['force'] = True elif arg == "-safe": - force = False - append = None + options['force'] = False + options['append'] = None elif arg == '-notitle': notitle = True elif arg == '-minor': - minor = True + options['minor'] = True elif arg.startswith('-nocontent:'): - nocontent = arg[11:] + options['nocontent'] = arg[11:] elif arg.startswith("-titlestart:"): titleStartMarker = arg[12:] elif arg.startswith("-titleend:"): titleEndMarker = arg[10:] elif arg.startswith("-summary:"): - summary = arg[9:] + options['summary'] = arg[9:] elif arg == '-autosummary': - autosummary = True + options['autosummary'] = True else: pywikibot.output(u"Disregarding unknown argument %s." % arg) reader = PageFromFileReader(filename, pageStartMarker, pageEndMarker, titleStartMarker, titleEndMarker, include, notitle) - bot = PageFromFileRobot(reader, force, append, summary, minor, autosummary, - nocontent) + bot = PageFromFileRobot(reader, **options) bot.run() if __name__ == "__main__": -- To view, visit https://gerrit.wikimedia.org/r/137334 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ie46eb9a003c9893a24dc01fe0b0cbf854f367187 Gerrit-PatchSet: 1 Gerrit-Project: pywikibot/core Gerrit-Branch: master Gerrit-Owner: Ricordisamoa <ricordisa...@openmailbox.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits