Xqt has uploaded a new change for review.

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

Change subject: Implement -wait option for checkimage.py
......................................................................

Implement -wait option for checkimage.py

- bugfix for convert_to_url method.
  Its parameter is a string not a page object,
  update code from compat
- rewrite wait() as a simple generator, convert Page to FilePage,
  use pywikibot.Timestamp to compare seconds,
  normal and limit parameters are obsolete

Bug:T76136
Change-Id: I2e04f4020281c1184a240a24d41cc6166880fb68
---
M scripts/checkimages.py
1 file changed, 28 insertions(+), 77 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/32/189232/1

diff --git a/scripts/checkimages.py b/scripts/checkimages.py
index 2cdde5e..eab91d2 100644
--- a/scripts/checkimages.py
+++ b/scripts/checkimages.py
@@ -32,7 +32,6 @@
 -time[:#]           Time in seconds between repeat runs (default: 30)
 
 -wait[:#]           Wait x second before check the images (default: 0)
-                    NOT YET IMPLEMENTED
 
 -skip[:#]           The bot skip the first [:#] images (default: 0)
 
@@ -82,7 +81,7 @@
 # (C) Kyle/Orgullomoore, 2006-2007 (newimage.py)
 # (C) Siebrand Mazeland, 2007-2010
 # (C) Filnik, 2007-2011
-# (C) Pywikibot team, 2007-2014
+# (C) Pywikibot team, 2007-2015
 #
 # Distributed under the terms of the MIT license.
 #
@@ -91,8 +90,8 @@
 
 import re
 import time
-import datetime
 import sys
+import urllib
 
 import pywikibot
 from pywikibot import pagegenerators as pg
@@ -869,9 +868,11 @@
             if not not_the_oldest:
                 return imageName
 
-    def convert_to_url(self, page):
+    def convert_to_url(self, pagename):
         """Return the page title suitable as for an URL."""
-        return page.title(asUrl=True)
+        title = pagename.replace(u" ", u"_")
+        encodedTitle = title.encode(self.site.encoding())
+        return urllib.quote(encodedTitle)
 
     def countEdits(self, pagename, userlist):
         """Function to count the edit of a user or a list of users in a 
page."""
@@ -1452,78 +1453,31 @@
         else:
             pywikibot.output('')
 
-    def wait(self, waitTime, generator, normal, limit):
+    def wait(self, generator, waitTime):
         """
         Skip the images uploaded before x seconds.
 
         Let the users to fix the image's problem alone in the first x seconds.
         """
-        imagesToSkip = 0
-        # if normal, we can take as many images as "limit" has told us,
-        # otherwise, sorry, nope.
-        # TODO: remove this exception as part of bug 65136
-        raise NotImplementedError(
-            "The wait option is not available at core yet.")
-
-        if normal:
-            printWithTimeZone(
-                u'Skipping the files uploaded less than %s seconds ago..'
-                % waitTime)
-            imagesToSkip = 0
-            while True:
-                # ensure that all the images loaded aren't to skip!
-                loadOtherImages = True
-                for image in generator:
-                    try:
-                        timestamp = image.latest_file_info.timestamp
-                    except pywikibot.NoPage:
-                        continue
-                    # not relative to localtime
-                    img_time = datetime.datetime.strptime(timestamp,
-                                                          
u"%Y-%m-%dT%H:%M:%SZ")
-
-                    now = datetime.datetime.strptime(
-                        str(datetime.datetime.utcnow()).split('.')[0],
-                        "%Y-%m-%d %H:%M:%S")  # timezones are UTC
-                    # + seconds to be sure that now > img_time
-                    while now < img_time:
-                        now = (now + datetime.timedelta(seconds=1))
-                    delta = now - img_time
-                    secs_of_diff = delta.seconds
-                    if waitTime > secs_of_diff:
-                        pywikibot.output(
-                            u'Skipping %s, uploaded %s seconds ago..'
-                            % (image.title(), int(secs_of_diff)))
-                        imagesToSkip += 1
-                        continue  # Still wait
-                    else:
-                        loadOtherImages = False
-                        break  # Not ok, continue
-                # if yes, we have skipped all the images given!
-                if loadOtherImages:
-                    generator = (x[0] for x in
-                                 self.site.newimages(number=limit,
-                                                     lestart=timestamp))
-                    imagesToSkip = 0
-                    # continue to load images!
-                    continue
-                else:
-                    break  # ok some other images, go below
-            newGen = list()
-            imagesToSkip += 1  # some calcs, better add 1
-            # Add new images, instead of the images skipped
-            newImages = self.site.newimages(number=imagesToSkip,
-                                            lestart=timestamp)
-            for image in generator:
-                newGen.append(image)
-            for imageData in newImages:
-                newGen.append(imageData[0])
-            return newGen
-        else:
-            pywikibot.output(
-                u"The wait option is available only with the standard "
-                u"generator.")
-            return generator
+        printWithTimeZone(
+            u'Skipping the files uploaded less than %s seconds ago..'
+            % waitTime)
+        for image in generator:
+            image = pywikibot.FilePage(image)
+            try:
+                timestamp = image.latest_file_info.timestamp
+            except pywikibot.PageRelatedError:
+                continue
+            now = pywikibot.Timestamp.utcnow()
+            delta = (now-timestamp)
+            if delta.total_seconds() > waitTime:
+                yield image
+            else:
+                pywikibot.warning(
+                    u'Skipping %s, uploaded %d %s ago..'
+                    % ((image.title(), delta.days, 'days')
+                       if delta.days > 0
+                       else (image.title(), delta.seconds, 'seconds')))
 
     def isTagged(self):
         """ Understand if a file is already tagged or not. """
@@ -1753,9 +1707,6 @@
             elif len(arg) > 5:
                 skip_number = int(arg[6:])
         elif arg.startswith('-wait'):
-            # FIXME: bug 65136
-            raise NotImplementedError(
-                "-wait option is not available at core yet. Sorry!")
             if len(arg) == 5:
                 waitTime = int(pywikibot.input(
                     u'How many time do you want to wait before checking the '
@@ -1860,8 +1811,8 @@
             generator = Bot.regexGenerator(regexpToUse, textRegex)
 
         Bot.takesettings()
-        if waitTime:
-            generator = Bot.wait(waitTime, generator, normal, limit)
+        if waitTime > 0:
+            generator = Bot.wait(generator, waitTime)
         for image in generator:
             # Setting the image for the main class
             Bot.setParameters(image.title(withNamespace=False))

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I2e04f4020281c1184a240a24d41cc6166880fb68
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Xqt <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to