[weboob] [PATCH 2/7] VirtKeyboard: make color argument mandatory

Pierre Mazière pierre.maziere at gmail.com
Thu Nov 3 10:02:55 CET 2011


Signed-off-by: Pierre Mazière <pierre.maziere at gmail.com>
---
 weboob/tools/captcha/virtkeyboard.py |   23 ++++++++++++++---------
 1 files changed, 14 insertions(+), 9 deletions(-)

diff --git a/weboob/tools/captcha/virtkeyboard.py b/weboob/tools/captcha/virtkeyboard.py
index 44cff48..5ed4bc2 100644
--- a/weboob/tools/captcha/virtkeyboard.py
+++ b/weboob/tools/captcha/virtkeyboard.py
@@ -27,26 +27,31 @@ class VirtKeyboardError(Exception):
 
 
 class VirtKeyboard(object):
-    def __init__(self, file,coords,color=None):
+    def __init__(self, file,coords,color):
         # file: virtual keyboard image
         # coords: dictionary <value to return>:<tuple(x1,y1,x2,y2)>
         # color: color of the symbols in the image
         #        depending on the image, it can be a single value or a tuple
-        self.color=color
         img=Image.open(file)
+
         self.bands=img.getbands()
+        if isinstance(color,int) and not isinstance(self.bands,str) and len(self.bands)!=1:
+            raise VirtKeyboardError("Color requires %i component but only 1 is provided" \
+                                    % len(self.bands))
+        if not isinstance(color, int) and len(color)!=len(self.bands):
+            raise VirtKeyboardError("Color requires %i components but %i are provided" \
+                                    % (len(self.bands),len(color)))
+        self.color=color
+
         (self.width,self.height)=img.size
         self.pixar=img.load()
         self.coords={}
         self.md5={}
         for i in coords.keys():
-            if self.color is None:
-                self.coords[i]=coords[i]
-            else:
-                coord=self.get_symbol_coords(coords[i])
-                if coord==(-1,-1,-1,-1):
-                    continue
-                self.coords[i]=coord
+            coord=self.get_symbol_coords(coords[i])
+            if coord==(-1,-1,-1,-1):
+                continue
+            self.coords[i]=coord
             self.md5[i]=self.checksum(self.coords[i])
 
     def get_symbol_coords(self,(x1,y1,x2,y2)):
-- 
1.7.7



More information about the weboob mailing list