Florianschmidtwelzow has uploaded a new change for review.

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

Change subject: Show a nice status bar for captcha generation script
......................................................................

Show a nice status bar for captcha generation script

If you create a huge amount of CAPTCHAs you don't see any feedback (if
you don't use --verbose, which isn't really what I mean), which makes
me dissatisfied.

This change adds a little and nice status bar (based on the linked gist)
which should accomplish the goal completely. You'll get a stattus bar (based
on the percentage of finished generated images on a 100 points scala), a
field for mentioning the elapsed time, one for the time remaining (ok,
that's basically just a thought :P) and a field which mentions, how fast
the script is (images per second).

Change-Id: I37b81fc0ae78267cb436a6f8f0d633a33d38195e
---
M captcha.py
1 file changed, 40 insertions(+), 4 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ConfirmEdit 
refs/changes/29/265529/1

diff --git a/captcha.py b/captcha.py
index 222dfb9..2800de2 100644
--- a/captcha.py
+++ b/captcha.py
@@ -31,6 +31,8 @@
 from optparse import OptionParser
 import os
 import sys
+import time
+import datetime
 import re
 
 try:
@@ -76,7 +78,7 @@
        # white text on a black background
        bgcolor = 0x0
        fgcolor = 0xffffff
-       # create a font object 
+       # create a font object
        font = ImageFont.truetype(fontname,fontsize)
        # determine dimensions of the text
        dim = font.getsize(text)
@@ -98,7 +100,7 @@
                im = wobbly_copy(im, wob, bgcolor, i*2+1, rot+45)
                im = wobbly_copy(im, wob, bgcolor, i*2+2, rot+90)
                rot += 30
-       
+
        # now get the bounding box of the nonzero parts of the image
        bbox = im.getbbox()
        bord = min(dim[0], dim[1])/4 # a bit of a border
@@ -192,11 +194,40 @@
        f.close()
        return words
 
+"""
+based on:
+https://gist.github.com/vladignatyev/06860ec2040cb497f0f3
+which was the easiest solution here :P
+"""
+def progress(count, total, starttime):
+       bar_len = 100
+       filled_len = int(round(bar_len * count / float(total)))
+
+       percents = round(100.0 * count / float(total), 1)
+       bar = '=' * filled_len + '-' * (bar_len - filled_len)
+       elapsed = time.time() - starttime
+       persecond = round(count / elapsed)
+       if persecond:
+               remainingseconds = datetime.datetime.utcfromtimestamp(
+                       (total - count) / persecond).strftime('%H:%M:%S')
+       else:
+               remainingseconds = 'n/a'
+
+       sys.stdout.write('[%s] %s%s [elapsed: %s; remaining: %s; images per 
second: %s]\r' % (
+               bar,
+               percents,
+               '%',
+               
datetime.datetime.utcfromtimestamp(elapsed).strftime('%H:%M:%S'),
+               remainingseconds,
+               int(persecond)
+       ))
+       sys.stdout.flush()
+
 if __name__ == '__main__':
        """This grabs random words from the dictionary 'words' (one
        word per line) and generates a captcha image for each one,
        with a keyed salted hash of the correct answer in the filename.
-       
+
        To check a reply, hash it in the same way with the same salt and
        secret key, then compare with the hash value given.
        """
@@ -216,7 +247,7 @@
        parser.add_option("--number-words", help="Number of words from the 
wordlist which make a captcha challenge (default 2)", type='int', default=2)
        parser.add_option("--min-length", help="Minimum length for a captcha 
challenge", type='int', default=1)
        parser.add_option("--max-length", help="Maximum length for a captcha 
challenge", type='int', default=-1)
-       
+
        opts, args = parser.parse_args()
 
        if opts.wordlist:
@@ -244,6 +275,7 @@
        dirs = opts.dirs
        verbose = opts.verbose
        fontsize = opts.font_size
+       starttime = time.time()
 
        if fill:
                count = max(0, fill - len(os.listdir(output)))
@@ -267,4 +299,8 @@
                if verbose:
                        print filename
                gen_captcha(word, font, fontsize, os.path.join(output, 
filename))
+               progress(i, count, starttime)
 
+
+
+       sys.stdout.write("\n")

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I37b81fc0ae78267cb436a6f8f0d633a33d38195e
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/ConfirmEdit
Gerrit-Branch: master
Gerrit-Owner: Florianschmidtwelzow <florian.schmidt.stargatewis...@gmail.com>

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

Reply via email to