Fixed: Style and lint issues in src/qidenticon.py
This commit is contained in:
parent
7445665963
commit
ce955ba844
28
LICENSE
28
LICENSE
|
@ -19,3 +19,31 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
SOFTWARE.
|
SOFTWARE.
|
||||||
|
|
||||||
|
===== qidenticon.py identicon python implementation with QPixmap output by sendiulo <sendiulo@gmx.net>
|
||||||
|
|
||||||
|
qidenticon.py is Licesensed under FreeBSD License.
|
||||||
|
(http://www.freebsd.org/copyright/freebsd-license.html)
|
||||||
|
|
||||||
|
Copyright 2013 "Sendiulo". All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||||
|
|
||||||
|
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||||
|
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
|
===== based on identicon.py identicon python implementation. by Shin Adachi <shn@glucose.jp>
|
||||||
|
|
||||||
|
identicon.py is Licesensed under FreeBSD License.
|
||||||
|
(http://www.freebsd.org/copyright/freebsd-license.html)
|
||||||
|
|
||||||
|
Copyright 1994-2009 Shin Adachi. All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||||
|
|
||||||
|
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||||
|
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
|
@ -1,44 +1,7 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
# -*- coding:utf-8 -*-
|
# -*- coding:utf-8 -*-
|
||||||
|
# pylint: disable=too-many-locals,too-many-arguments,too-many-function-args
|
||||||
###
|
|
||||||
# qidenticon.py is Licesensed under FreeBSD License.
|
|
||||||
# (http://www.freebsd.org/copyright/freebsd-license.html)
|
|
||||||
#
|
|
||||||
# Copyright 2013 "Sendiulo". All rights reserved.
|
|
||||||
#
|
|
||||||
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
||||||
#
|
|
||||||
# 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
||||||
# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|
||||||
#
|
|
||||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
###
|
|
||||||
|
|
||||||
###
|
|
||||||
# identicon.py is Licesensed under FreeBSD License.
|
|
||||||
# (http://www.freebsd.org/copyright/freebsd-license.html)
|
|
||||||
#
|
|
||||||
# Copyright 1994-2009 Shin Adachi. All rights reserved.
|
|
||||||
#
|
|
||||||
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
||||||
#
|
|
||||||
# 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
||||||
# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|
||||||
#
|
|
||||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
###
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
qidenticon.py
|
|
||||||
identicon python implementation with QPixmap output
|
|
||||||
by sendiulo <sendiulo@gmx.net>
|
|
||||||
|
|
||||||
based on
|
|
||||||
identicon.py
|
|
||||||
identicon python implementation.
|
|
||||||
by Shin Adachi <shn@glucose.jp>
|
|
||||||
|
|
||||||
= usage =
|
= usage =
|
||||||
|
|
||||||
== python ==
|
== python ==
|
||||||
|
@ -49,14 +12,16 @@ Return a PIL Image class instance which have generated identicon image.
|
||||||
```size``` specifies `patch size`. Generated image size is 3 * ```size```.
|
```size``` specifies `patch size`. Generated image size is 3 * ```size```.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# we probably don't need all of them, but i don't want to check now
|
from PyQt4 import QtGui
|
||||||
from PyQt4 import QtCore, QtGui
|
from PyQt4.QtCore import QSize, QPointF, Qt
|
||||||
from PyQt4.QtCore import *
|
from PyQt4.QtGui import QPixmap, QPainter, QPolygonF
|
||||||
from PyQt4.QtGui import *
|
|
||||||
|
|
||||||
__all__ = ['render_identicon', 'IdenticonRendererBase']
|
__all__ = ['render_identicon', 'IdenticonRendererBase']
|
||||||
|
|
||||||
|
|
||||||
class IdenticonRendererBase(object):
|
class IdenticonRendererBase(object):
|
||||||
|
"""Encapsulate methods around rendering identicons"""
|
||||||
|
|
||||||
PATH_SET = []
|
PATH_SET = []
|
||||||
|
|
||||||
def __init__(self, code):
|
def __init__(self, code):
|
||||||
|
@ -97,33 +62,31 @@ class IdenticonRendererBase(object):
|
||||||
|
|
||||||
# side patch
|
# side patch
|
||||||
kwds['foreColor'] = foreColor
|
kwds['foreColor'] = foreColor
|
||||||
kwds['type'] = side[0]
|
kwds['patch_type'] = side[0]
|
||||||
for i in xrange(4):
|
for i in xrange(4):
|
||||||
pos = [(1, 0), (2, 1), (1, 2), (0, 1)][i]
|
pos = [(1, 0), (2, 1), (1, 2), (0, 1)][i]
|
||||||
image = self.drawPatchQt(pos, side[2] + 1 + i, side[1], **kwds)
|
image = self.drawPatchQt(pos, side[2] + 1 + i, side[1], **kwds)
|
||||||
|
|
||||||
# corner patch
|
# corner patch
|
||||||
kwds['foreColor'] = secondColor
|
kwds['foreColor'] = secondColor
|
||||||
kwds['type'] = corner[0]
|
kwds['patch_type'] = corner[0]
|
||||||
for i in xrange(4):
|
for i in xrange(4):
|
||||||
pos = [(0, 0), (2, 0), (2, 2), (0, 2)][i]
|
pos = [(0, 0), (2, 0), (2, 2), (0, 2)][i]
|
||||||
image = self.drawPatchQt(pos, corner[2] + 1 + i, corner[1], **kwds)
|
image = self.drawPatchQt(pos, corner[2] + 1 + i, corner[1], **kwds)
|
||||||
|
|
||||||
return image
|
return image
|
||||||
|
|
||||||
|
def drawPatchQt(self, pos, turn, invert, patch_type, image, size, foreColor,
|
||||||
def drawPatchQt(self, pos, turn, invert, type, image, size, foreColor,
|
backColor, penwidth): # pylint: disable=unused-argument
|
||||||
backColor, penwidth):
|
|
||||||
"""
|
"""
|
||||||
@param size patch size
|
@param size patch size
|
||||||
"""
|
"""
|
||||||
path = self.PATH_SET[type]
|
path = self.PATH_SET[patch_type]
|
||||||
if not path:
|
if not path:
|
||||||
# blank patch
|
# blank patch
|
||||||
invert = not invert
|
invert = not invert
|
||||||
path = [(0., 0.), (1., 0.), (1., 1.), (0., 1.), (0., 0.)]
|
path = [(0., 0.), (1., 0.), (1., 1.), (0., 1.), (0., 0.)]
|
||||||
|
|
||||||
|
|
||||||
polygon = QPolygonF([QPointF(x * size, y * size) for x, y in path])
|
polygon = QPolygonF([QPointF(x * size, y * size) for x, y in path])
|
||||||
|
|
||||||
rot = turn % 4
|
rot = turn % 4
|
||||||
|
@ -162,10 +125,12 @@ class IdenticonRendererBase(object):
|
||||||
|
|
||||||
return image
|
return image
|
||||||
|
|
||||||
### virtual functions
|
def decode(self, code, twoColor):
|
||||||
def decode(self, code):
|
"""virtual functions"""
|
||||||
|
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
|
|
||||||
class DonRenderer(IdenticonRendererBase):
|
class DonRenderer(IdenticonRendererBase):
|
||||||
"""
|
"""
|
||||||
Don Park's implementation of identicon
|
Don Park's implementation of identicon
|
||||||
|
@ -211,26 +176,42 @@ class DonRenderer(IdenticonRendererBase):
|
||||||
# modify path set
|
# modify path set
|
||||||
for idx in xrange(len(PATH_SET)):
|
for idx in xrange(len(PATH_SET)):
|
||||||
if PATH_SET[idx]:
|
if PATH_SET[idx]:
|
||||||
p = map(lambda vec: (vec[0] / 4.0, vec[1] / 4.0), PATH_SET[idx])
|
p = [(vec[0] / 4.0, vec[1] / 4.0) for vec in PATH_SET[idx]]
|
||||||
PATH_SET[idx] = p + p[:1]
|
PATH_SET[idx] = p + p[:1]
|
||||||
|
|
||||||
def decode(self, code, twoColor):
|
def decode(self, code, twoColor):
|
||||||
# decode the code
|
"""decode the code"""
|
||||||
shift = 0; middleType = (code >> shift) & 0x03
|
|
||||||
shift += 2; middleInvert= (code >> shift) & 0x01
|
shift = 0
|
||||||
shift += 1; cornerType = (code >> shift) & 0x0F
|
middleType = (code >> shift) & 0x03
|
||||||
shift += 4; cornerInvert= (code >> shift) & 0x01
|
shift += 2
|
||||||
shift += 1; cornerTurn = (code >> shift) & 0x03
|
middleInvert = (code >> shift) & 0x01
|
||||||
shift += 2; sideType = (code >> shift) & 0x0F
|
shift += 1
|
||||||
shift += 4; sideInvert = (code >> shift) & 0x01
|
cornerType = (code >> shift) & 0x0F
|
||||||
shift += 1; sideTurn = (code >> shift) & 0x03
|
shift += 4
|
||||||
shift += 2; blue = (code >> shift) & 0x1F
|
cornerInvert = (code >> shift) & 0x01
|
||||||
shift += 5; green = (code >> shift) & 0x1F
|
shift += 1
|
||||||
shift += 5; red = (code >> shift) & 0x1F
|
cornerTurn = (code >> shift) & 0x03
|
||||||
shift += 5; second_blue = (code >> shift) & 0x1F
|
shift += 2
|
||||||
shift += 5; second_green= (code >> shift) & 0x1F
|
sideType = (code >> shift) & 0x0F
|
||||||
shift += 5; second_red = (code >> shift) & 0x1F
|
shift += 4
|
||||||
shift += 1; swap_cross = (code >> shift) & 0x01
|
sideInvert = (code >> shift) & 0x01
|
||||||
|
shift += 1
|
||||||
|
sideTurn = (code >> shift) & 0x03
|
||||||
|
shift += 2
|
||||||
|
blue = (code >> shift) & 0x1F
|
||||||
|
shift += 5
|
||||||
|
green = (code >> shift) & 0x1F
|
||||||
|
shift += 5
|
||||||
|
red = (code >> shift) & 0x1F
|
||||||
|
shift += 5
|
||||||
|
second_blue = (code >> shift) & 0x1F
|
||||||
|
shift += 5
|
||||||
|
second_green = (code >> shift) & 0x1F
|
||||||
|
shift += 5
|
||||||
|
second_red = (code >> shift) & 0x1F
|
||||||
|
shift += 1
|
||||||
|
swap_cross = (code >> shift) & 0x01
|
||||||
|
|
||||||
middleType = self.MIDDLE_PATCH_SET[middleType]
|
middleType = self.MIDDLE_PATCH_SET[middleType]
|
||||||
|
|
||||||
|
@ -250,6 +231,8 @@ class DonRenderer(IdenticonRendererBase):
|
||||||
|
|
||||||
|
|
||||||
def render_identicon(code, size, twoColor=False, opacity=255, penwidth=0, renderer=None):
|
def render_identicon(code, size, twoColor=False, opacity=255, penwidth=0, renderer=None):
|
||||||
|
"""Render an image"""
|
||||||
|
|
||||||
if not renderer:
|
if not renderer:
|
||||||
renderer = DonRenderer
|
renderer = DonRenderer
|
||||||
return renderer(code).render(size, twoColor, opacity, penwidth)
|
return renderer(code).render(size, twoColor, opacity, penwidth)
|
Reference in New Issue
Block a user