jenkins-bot has submitted this change and it was merged.

Change subject: standardize basic.py
......................................................................


standardize basic.py

* inherits from the Bot class

* does not override .run()

* uses .userPut() instead of the custom .save()

* -dry enables pywikibot.config.simulate

Change-Id: I3820cb381b3529a9ca894f71064ab1d161e95773
---
M pywikibot/bot.py
M scripts/basic.py
2 files changed, 32 insertions(+), 67 deletions(-)

Approvals:
  John Vandenberg: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/pywikibot/bot.py b/pywikibot/bot.py
index b6a5f7b..ddd866e 100644
--- a/pywikibot/bot.py
+++ b/pywikibot/bot.py
@@ -1108,6 +1108,9 @@
         * 'show_diff' - show changes between oldtext and newtext (enabled)
         * 'ignore_save_related_errors' - report and ignore (disabled)
         * 'ignore_server_errors' - report and ignore (disabled)
+
+        @return: whether the page was saved successfully
+        @rtype: bool
         """
         if oldtext == newtext:
             pywikibot.output(u'No changes were needed on %s'
@@ -1125,7 +1128,7 @@
             pywikibot.output(u'Edit summary: %s' % kwargs['summary'])
 
         page.text = newtext
-        self._save_page(page, page.save, **kwargs)
+        return self._save_page(page, page.save, **kwargs)
 
     def _save_page(self, page, func, *args, **kwargs):
         """
@@ -1141,6 +1144,8 @@
         @kwarg ignore_save_related_errors: if True, errors related to
         page save will be reported and ignored (default: False)
         @kwtype ignore_save_related_errors: bool
+        @return: whether the page was saved successfully
+        @rtype: bool
         """
         if not self.user_confirm('Do you want to accept these changes?'):
             return
@@ -1177,6 +1182,9 @@
                 raise
             pywikibot.error(u'Server Error while processing %s: %s'
                             % (page.title(), e))
+        else:
+            return True
+        return False
 
     def quit(self):
         """Cleanup and quit processing."""
diff --git a/scripts/basic.py b/scripts/basic.py
index 23dac2b..192d85f 100755
--- a/scripts/basic.py
+++ b/scripts/basic.py
@@ -16,7 +16,7 @@
 
 """
 #
-# (C) Pywikibot team, 2006-2014
+# (C) Pywikibot team, 2006-2015
 #
 # Distributed under the terms of the MIT license.
 #
@@ -26,8 +26,9 @@
 #
 
 import pywikibot
-from pywikibot import pagegenerators
-from pywikibot import i18n
+from pywikibot import i18n, pagegenerators, Bot
+
+from pywikibot.tools import issue_deprecation_warning
 
 # This is required for the text that is shown when you run this script
 # with the parameter -help.
@@ -36,7 +37,7 @@
 }
 
 
-class BasicBot:
+class BasicBot(Bot):
 
     """An incomplete sample bot."""
 
@@ -44,29 +45,26 @@
     # The file containing these messages should have the same name as the 
caller
     # script (i.e. basic.py in this case)
 
-    def __init__(self, generator, dry):
+    def __init__(self, generator, dry=False, **kwargs):
         """
         Constructor.
 
-        Parameters:
-            @param generator: The page generator that determines on which pages
-                              to work.
-            @type generator: generator.
-            @param dry: If True, doesn't do any real changes, but only shows
-                        what would have been changed.
-            @type dry: boolean.
+        @param generator: the page generator that determines on which pages
+            to work
+        @type generator: generator
+        @param dry: if True, doesn't do any real changes, but only shows
+            what would have been changed
+        @type dry: bool
         """
+        if dry:
+            issue_deprecation_warning('dry argument', 
'pywikibot.config.simulate', 1)
+            pywikibot.config.simulate = True
+        super(BasicBot, self).__init__(**kwargs)
         self.generator = generator
-        self.dry = dry
 
         # Set the edit summary message
         site = pywikibot.Site()
         self.summary = i18n.twtranslate(site, 'basic-changing')
-
-    def run(self):
-        """Process each page from the generator."""
-        for page in self.generator:
-            self.treat(page)
 
     def treat(self, page):
         """Load the given page, does some changes, and saves it."""
@@ -82,13 +80,13 @@
         # Example: This puts the text 'Test' at the beginning of the page.
         text = 'Test ' + text
 
-        if not self.save(text, page, self.summary):
+        if not self.userPut(page, page.text, text, summary=self.summary,
+                            ignore_save_related_errors=True):
             pywikibot.output(u'Page %s not saved.' % page.title(asLink=True))
 
     def load(self, page):
         """Load the text of the given page."""
         try:
-            # Load the page
             text = page.get()
         except pywikibot.NoPage:
             pywikibot.output(u"Page %s does not exist; skipping."
@@ -99,42 +97,6 @@
         else:
             return text
         return None
-
-    def save(self, text, page, comment=None, minorEdit=True,
-             botflag=True):
-        """Update the given page with new text."""
-        # only save if something was changed
-        if text != page.get():
-            # 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())
-            # show what was changed
-            pywikibot.showDiff(page.get(), text)
-            pywikibot.output(u'Comment: %s' % comment)
-            if not self.dry:
-                if pywikibot.input_yn(
-                        u'Do you want to accept these changes?',
-                        default=False, automatic_quit=False):
-                    try:
-                        page.text = text
-                        # Save the page
-                        page.save(summary=comment or self.comment,
-                                  minor=minorEdit, botflag=botflag)
-                    except pywikibot.LockedPage:
-                        pywikibot.output(u"Page %s is locked; skipping."
-                                         % page.title(asLink=True))
-                    except pywikibot.EditConflict:
-                        pywikibot.output(
-                            u'Skipping %s because of edit conflict'
-                            % (page.title()))
-                    except pywikibot.SpamfilterError as error:
-                        pywikibot.output(
-                            u'Cannot change %s because of spam blacklist entry 
%s'
-                            % (page.title(), error.url))
-                    else:
-                        return True
-        return False
 
 
 def main(*args):
@@ -153,26 +115,21 @@
     # that are also used by other scripts and that determine on which pages
     # to work on.
     genFactory = pagegenerators.GeneratorFactory()
-    # The generator gives the pages that should be worked upon.
-    gen = None
-    # If dry is True, doesn't do any real changes, but only show
-    # what would have been changed.
-    dry = False
 
     # Parse command line arguments
     for arg in local_args:
-        if arg.startswith("-dry"):
-            dry = True
+        if arg == '-dry':
+            issue_deprecation_warning('-dry option', '-simulate', 1)
+            pywikibot.config.simulate = True
         else:
             genFactory.handleArg(arg)
 
-    if not gen:
-        gen = genFactory.getCombinedGenerator()
+    gen = genFactory.getCombinedGenerator()
     if gen:
         # The preloading generator is responsible for downloading multiple
         # pages from the wiki simultaneously.
         gen = pagegenerators.PreloadingGenerator(gen)
-        bot = BasicBot(gen, dry)
+        bot = BasicBot(gen)
         bot.run()
     else:
         pywikibot.showHelp()

-- 
To view, visit https://gerrit.wikimedia.org/r/181717
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I3820cb381b3529a9ca894f71064ab1d161e95773
Gerrit-PatchSet: 14
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Ricordisamoa <ricordisa...@openmailbox.org>
Gerrit-Reviewer: John Vandenberg <jay...@gmail.com>
Gerrit-Reviewer: Ladsgroup <ladsgr...@gmail.com>
Gerrit-Reviewer: Merlijn van Deen <valhall...@arctus.nl>
Gerrit-Reviewer: Ricordisamoa <ricordisa...@openmailbox.org>
Gerrit-Reviewer: XZise <commodorefabia...@gmx.de>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to