Revision: 8278 Author: xqt Date: 2010-06-11 17:01:24 +0000 (Fri, 11 Jun 2010)
Log Message: ----------- separate methods for loading and saving the page
Modified Paths: -------------- trunk/pywikipedia/basic.py
Modified: trunk/pywikipedia/basic.py =================================================================== --- trunk/pywikipedia/basic.py 2010-06-11 09:56:33 UTC (rev 8277) +++ trunk/pywikipedia/basic.py 2010-06-11 17:01:24 UTC (rev 8278) @@ -72,26 +72,39 @@ """ Loads the given page, does some changes, and saves it. """ + text = self.load(page) + if not text: + return + + ################################################################ + # NOTE: Here you can modify the text in whatever way you want. # + ################################################################ + + # If you find out that you do not want to edit this page, just return. + # Example: This puts the text 'Test' at the beginning of the page. + text = 'Test ' + text + + if not self.save(text, page, self.summary): + pywikibot.output(u'Page %s not saved.' % page.aslink()) + + def load(self, page): + """ + Loads the given page, does some changes, and saves it. + """ try: # Load the page text = page.get() except pywikibot.NoPage: pywikibot.output(u"Page %s does not exist; skipping." % page.aslink()) - return except pywikibot.IsRedirectPage: pywikibot.output(u"Page %s is a redirect; skipping." % page.aslink()) - return + else: + return text + return None
- ################################################################ - # NOTE: Here you can modify the text in whatever way you want. # - ################################################################ - - # If you find out that you do not want to edit this page, just return. - # Example: This puts the text 'Test' at the beginning of the page. - text = 'Test ' + text - + def save(self, text, page, comment, minorEdit=True, botflag=True): # only save if something was changed if text != page.get(): # Show the title of the page we're working on. @@ -100,12 +113,16 @@ % page.title()) # show what was changed pywikibot.showDiff(page.get(), text) + pywikibot.output(u'Comment: %s' %comment) if not self.dry: - choice = pywikibot.inputChoice(u'Do you want to accept these changes?', ['Yes', 'No'], ['y', 'N'], 'N') + choice = pywikibot.inputChoice( + u'Do you want to accept these changes?', + ['Yes', 'No'], ['y', 'N'], 'N') if choice == 'y': try: # Save the page - page.put(text, comment=self.summary) + page.put(text, comment=comment, + minorEdit=minorEdit, botflag=botflag) except pywikibot.LockedPage: pywikibot.output(u"Page %s is locked; skipping." % page.aslink()) @@ -117,8 +134,10 @@ pywikibot.output( u'Cannot change %s because of spam blacklist entry %s' % (page.title(), error.url)) + else: + return True + return False
- def main(): # This factory is responsible for processing command line arguments # that are also used by other scripts and that determine on which pages
pywikipedia-svn@lists.wikimedia.org