PEP8 style fixes and other small style fixes
I used autopep8 on some files and did carefully check the changes. I ignored E501,E302,E231,E225,E222,E221,E241,E203 in my search, and at least E501 on any autopep8 run. Other style fixes not related to PEP8: * Only use new-style classes. I don't think the usage of old-style classes was voluntary. Old-style classes are removed in Python 3. * Convert an if/else to a one-liner in mediawiki, change docstring style change to a comment something that wasn't really appropriate for a docstring. * Unneeded first if condition in meteofrance
This commit is contained in:
parent
1e000acf3a
commit
006e97a8be
99 changed files with 441 additions and 350 deletions
|
|
@ -21,87 +21,88 @@
|
|||
import hashlib
|
||||
import Image
|
||||
|
||||
|
||||
class VirtKeyboardError(Exception):
|
||||
def __init__(self, msg):
|
||||
Exception.__init__(self, msg)
|
||||
|
||||
|
||||
class VirtKeyboard(object):
|
||||
def __init__(self, file,coords,color):
|
||||
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
|
||||
img=Image.open(file)
|
||||
img = Image.open(file)
|
||||
|
||||
self.bands=img.getbands()
|
||||
if isinstance(color,int) and not isinstance(self.bands,str) and len(self.bands)!=1:
|
||||
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):
|
||||
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
|
||||
% (len(self.bands), len(color)))
|
||||
self.color = color
|
||||
|
||||
(self.width,self.height)=img.size
|
||||
self.pixar=img.load()
|
||||
self.coords={}
|
||||
self.md5={}
|
||||
(self.width, self.height) = img.size
|
||||
self.pixar = img.load()
|
||||
self.coords = {}
|
||||
self.md5 = {}
|
||||
for i in coords.keys():
|
||||
coord=self.get_symbol_coords(coords[i])
|
||||
if coord==(-1,-1,-1,-1):
|
||||
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])
|
||||
self.coords[i] = coord
|
||||
self.md5[i] = self.checksum(self.coords[i])
|
||||
|
||||
def get_symbol_coords(self,(x1,y1,x2,y2)):
|
||||
newY1=-1
|
||||
newY2=-1
|
||||
for y in range(y1,min(y2+1,self.height)):
|
||||
empty_line=True
|
||||
for x in range(x1,min(x2+1,self.width)):
|
||||
if self.pixar[x,y] == self.color:
|
||||
empty_line=False
|
||||
if newY1==-1:
|
||||
newY1=y
|
||||
break;
|
||||
else:
|
||||
break
|
||||
if newY1!=-1 and not empty_line:
|
||||
newY2=y
|
||||
newX1=-1
|
||||
newX2=-1
|
||||
for x in range(x1,min(x2+1,self.width)):
|
||||
empty_column=True
|
||||
for y in range(y1,min(y2+1,self.height)):
|
||||
if self.pixar[x,y] == self.color:
|
||||
empty_column=False
|
||||
if newX1==-1:
|
||||
newX1=x
|
||||
def get_symbol_coords(self, (x1, y1, x2, y2)):
|
||||
newY1 = -1
|
||||
newY2 = -1
|
||||
for y in range(y1, min(y2 + 1, self.height)):
|
||||
empty_line = True
|
||||
for x in range(x1, min(x2 + 1, self.width)):
|
||||
if self.pixar[x, y] == self.color:
|
||||
empty_line = False
|
||||
if newY1 == -1:
|
||||
newY1 = y
|
||||
break
|
||||
else:
|
||||
break
|
||||
if newX1!=-1 and not empty_column:
|
||||
newX2=x
|
||||
return (newX1,newY1,newX2,newY2)
|
||||
if newY1 != -1 and not empty_line:
|
||||
newY2 = y
|
||||
newX1 = -1
|
||||
newX2 = -1
|
||||
for x in range(x1, min(x2 + 1, self.width)):
|
||||
empty_column = True
|
||||
for y in range(y1, min(y2 + 1, self.height)):
|
||||
if self.pixar[x, y] == self.color:
|
||||
empty_column = False
|
||||
if newX1 == -1:
|
||||
newX1 = x
|
||||
break
|
||||
else:
|
||||
break
|
||||
if newX1 != -1 and not empty_column:
|
||||
newX2 = x
|
||||
return (newX1, newY1, newX2, newY2)
|
||||
|
||||
def checksum(self,(x1,y1,x2,y2)):
|
||||
def checksum(self, (x1, y1, x2, y2)):
|
||||
s = ''
|
||||
for y in range(y1,min(y2+1,self.height)):
|
||||
for x in range(x1,min(x2+1,self.width)):
|
||||
if self.pixar[x,y]==self.color:
|
||||
for y in range(y1, min(y2 + 1, self.height)):
|
||||
for x in range(x1, min(x2 + 1, self.width)):
|
||||
if self.pixar[x, y] == self.color:
|
||||
s += "."
|
||||
else:
|
||||
s += " "
|
||||
return hashlib.md5(s).hexdigest()
|
||||
|
||||
def get_symbol_code(self,md5sum):
|
||||
def get_symbol_code(self, md5sum):
|
||||
for i in self.md5.keys():
|
||||
if md5sum == self.md5[i]:
|
||||
return i
|
||||
raise VirtKeyboardError('Symbol not found')
|
||||
|
||||
def check_symbols(self,symbols,dirname):
|
||||
def check_symbols(self, symbols, dirname):
|
||||
# symbols: dictionary <symbol>:<md5 value>
|
||||
for s in symbols.keys():
|
||||
try:
|
||||
|
|
@ -109,32 +110,33 @@ class VirtKeyboard(object):
|
|||
except VirtKeyboardError:
|
||||
self.generate_MD5(dirname)
|
||||
raise VirtKeyboardError("Symbol '%s' not found; all symbol hashes are available in %s"\
|
||||
% (s,dirname))
|
||||
% (s, dirname))
|
||||
|
||||
def generate_MD5(self,dir):
|
||||
def generate_MD5(self, dir):
|
||||
for i in self.coords.keys():
|
||||
width=self.coords[i][2]-self.coords[i][0]+1
|
||||
height=self.coords[i][3]-self.coords[i][1]+1
|
||||
img=Image.new(''.join(self.bands),(width,height))
|
||||
matrix=img.load()
|
||||
width = self.coords[i][2] - self.coords[i][0] + 1
|
||||
height = self.coords[i][3] - self.coords[i][1] + 1
|
||||
img = Image.new(''.join(self.bands), (width, height))
|
||||
matrix = img.load()
|
||||
for y in range(height):
|
||||
for x in range(width):
|
||||
matrix[x,y]=self.pixar[self.coords[i][0]+x,self.coords[i][1]+y]
|
||||
img.save(dir+"/"+self.md5[i]+".png")
|
||||
matrix[x, y] = self.pixar[self.coords[i][0] + x, self.coords[i][1] + y]
|
||||
img.save(dir + "/" + self.md5[i] + ".png")
|
||||
|
||||
|
||||
class MappedVirtKeyboard(VirtKeyboard):
|
||||
def __init__(self, file, document, img_element, color, map_attr="onclick"):
|
||||
map_id=img_element.attrib.get("usemap")[1:]
|
||||
map=document.find("//map[@id='"+map_id+"']")
|
||||
map_id = img_element.attrib.get("usemap")[1:]
|
||||
map = document.find("//map[@id='" + map_id + "']")
|
||||
if map is None:
|
||||
map=document.find("//map[@name='"+map_id+"']")
|
||||
map = document.find("//map[@name='" + map_id + "']")
|
||||
|
||||
coords={}
|
||||
coords = {}
|
||||
for area in map.getiterator("area"):
|
||||
code=area.attrib.get(map_attr)
|
||||
area_coords=[]
|
||||
code = area.attrib.get(map_attr)
|
||||
area_coords = []
|
||||
for coord in area.attrib.get("coords").split(','):
|
||||
area_coords.append(int(coord))
|
||||
coords[code]=tuple(area_coords)
|
||||
coords[code] = tuple(area_coords)
|
||||
|
||||
VirtKeyboard.__init__(self,file,coords,color)
|
||||
VirtKeyboard.__init__(self, file, coords, color)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue