Ricordisamoa has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/181717

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

standardize basic.py

* inherits from the Bot class

* does not override .run()

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

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


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/17/181717/1

diff --git a/pywikibot/bot.py b/pywikibot/bot.py
index 21eb4a4..5cb2dc9 100644
--- a/pywikibot/bot.py
+++ b/pywikibot/bot.py
@@ -1041,6 +1041,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 9c315ea..a8e3792 100755
--- a/scripts/basic.py
+++ b/scripts/basic.py
@@ -24,8 +24,7 @@
 #
 
 import pywikibot
-from pywikibot import pagegenerators
-from pywikibot import i18n
+from pywikibot import i18n, pagegenerators, Bot
 
 # This is required for the text that is shown when you run this script
 # with the parameter -help.
@@ -34,7 +33,7 @@
 }
 
 
-class BasicBot:
+class BasicBot(Bot):
 
     """An incomplete sample bot."""
 
@@ -42,29 +41,25 @@
     # 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, **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
+        @keyword dry: if True, doesn't do any real changes, but only shows
+            what would have been changed
         """
+        self.availableOptions.update({
+            'dry': False,
+        })
+        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. """
@@ -80,13 +75,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, 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."
@@ -97,42 +92,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(comment=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):
@@ -151,26 +110,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
+    options = {}
 
     # Parse command line arguments
     for arg in local_args:
-        if arg.startswith("-dry"):
-            dry = True
+        if arg == '-dry':
+            options['dry'] = 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, **options)
         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: newchange
Gerrit-Change-Id: I3820cb381b3529a9ca894f71064ab1d161e95773
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

Reply via email to