Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
1d53350
add test via pytest and tox, tools/conosle/pagan, minor changes
xnor Oct 13, 2016
d8789a4
add readme, log warning webserver
xnor Oct 13, 2016
7e68ac1
check for raise FalseHashError on buggy hash values as input for images
xnor Oct 13, 2016
cb2b562
check for raise FalseHashError on buggy hash values as input for images
xnor Oct 13, 2016
c0eb637
allowed hash can contain upper and lower chars
xnor Oct 17, 2016
e88d38f
merge
xnor Oct 17, 2016
34fb974
fix merge relicts
xnor Oct 17, 2016
047a895
use bottle app, changed 404 message
xnor Oct 18, 2016
00e0fe7
better 404, todo ideas
xnor Oct 18, 2016
4f639e6
Merge branch 'webserver'
xnor Oct 18, 2016
3d9daa4
branch issue, taken stuff from master to webserver branch
xnor Oct 18, 2016
47fc565
Merge branch 'webserver'
xnor Oct 18, 2016
968e61a
minor changes
xnor Oct 19, 2016
fd3eacd
Merge remote-tracking branch 'upstream/master'
xnor Oct 19, 2016
7fb5996
dict keys must not be in intended order, fixed this
xnor Oct 19, 2016
b530f12
add simple test case for calling pagan
xnor Oct 19, 2016
6757ce9
test on failure, fals param or no input
xnor Oct 19, 2016
a64ffd2
cleanup index
xnor Oct 20, 2016
6729957
rm wbr add
xnor Oct 20, 2016
5a94204
default hash is md5
xnor Oct 20, 2016
51e5647
check for raise FalseHashError on buggy hash values as input for images
xnor Oct 13, 2016
af33f7d
allowed hash can contain upper and lower chars
xnor Oct 17, 2016
1237856
fix merge relicts
xnor Oct 17, 2016
ec7b43c
use bottle app, changed 404 message
xnor Oct 18, 2016
80d8f23
Minor fixes to README
rohitjha Oct 19, 2016
2fa9f7d
Modified README.md, expanded the pagan command line interface (CLI), …
daboth Oct 19, 2016
43340d2
Updated CHANGELOG.md and README.md
daboth Oct 19, 2016
4fe0d73
minor changes
xnor Oct 19, 2016
8f8aad5
dict keys must not be in intended order, fixed this
xnor Oct 19, 2016
f78813e
add simple test case for calling pagan
xnor Oct 19, 2016
1f5663b
test on failure, fals param or no input
xnor Oct 19, 2016
0236807
cleanup index
xnor Oct 20, 2016
bd30ee9
rm wbr add
xnor Oct 20, 2016
35150e2
Merge branch 'console_pagan' into test
xnor Oct 20, 2016
0dac299
Merge branch 'console_pagan'
xnor Oct 20, 2016
447df6d
Merge branch 'test'
xnor Oct 20, 2016
bafebe9
Merge branch 'master' into webserver
xnor Oct 20, 2016
c19dc42
fixed host in webserver.py
xnor Oct 25, 2016
2b98216
running pagan webservice on docker
xnor Oct 25, 2016
e0afc37
webserver: error404 -> template
xnor Oct 25, 2016
0d50d6b
pagan console, removed / and .. from filename
xnor Oct 25, 2016
f98310e
pagan webserver rep
xnor Nov 7, 2016
fb0dcbf
more prepare new git rep
xnor Nov 8, 2016
4d33f05
check for raise FalseHashError on buggy hash values as input for images
xnor Oct 13, 2016
5ea3e56
allowed hash can contain upper and lower chars
xnor Oct 17, 2016
474636c
fix merge relicts
xnor Oct 17, 2016
9dbb81e
Minor fixes to README
rohitjha Oct 19, 2016
3397885
Modified README.md, expanded the pagan command line interface (CLI), …
daboth Oct 19, 2016
168a3db
Updated CHANGELOG.md and README.md
daboth Oct 19, 2016
01e5a60
dict keys must not be in intended order, fixed this
xnor Oct 19, 2016
2dc804a
add simple test case for calling pagan
xnor Oct 19, 2016
2644961
test on failure, fals param or no input
xnor Oct 19, 2016
09df945
cleanup index
xnor Oct 20, 2016
1a070c5
rm wbr add
xnor Oct 20, 2016
e17e423
default hash is md5
xnor Oct 20, 2016
405b7ff
use bottle app, changed 404 message
xnor Oct 18, 2016
9c5fd6e
minor changes
xnor Oct 19, 2016
aab65fe
bin_pagan fixed filename, added --noshow to allow tox tests again, fa…
xnor Nov 8, 2016
41816dc
minor pylint fixes
xnor Nov 8, 2016
c380a55
fixed some stuff
xnor Nov 8, 2016
617d806
Add test for console version of pagan, add more tests
xnor Nov 15, 2016
ab705db
Remove tools/webserver, new repo for webserver version
xnor Nov 15, 2016
25ddc28
bin/pagan sanatize file names, add --noshow option
xnor Nov 15, 2016
99d23de
Docstring reformat to pass pylint
xnor Nov 15, 2016
6d91a18
minor changes
xnor Nov 28, 2016
d67e729
use future module for byte strings
xnor Nov 28, 2016
ddb8b7a
add hyotehsis test
xnor Nov 28, 2016
7fee321
Merge branch 'master' of github.com:xnor/pagan
xnor Nov 28, 2016
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ var/
*.egg-info/
.installed.cfg
*.egg
.hypothesis/
test/.hypothesis/

# PyInstaller
# Usually these files are written by a python script from a template
Expand Down
30 changes: 15 additions & 15 deletions pagan/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import os
import sys

from builtins import bytes

class FalseHashError(Exception):
""" """
Expand Down Expand Up @@ -103,12 +104,7 @@ def hash_input(inpt, algo=HASH_SHA256):
elif (algo == HASH_SHA512):
hashcode = hashlib.sha512()

if sys.version_info.major == 2:
inpt = bytes(inpt)
else:
inpt = bytes(inpt, "utf-8")

hashcode.update(inpt)
hashcode.update(bytes(inpt, "utf-8"))
hexhash = hashcode.hexdigest()
return hexhash

Expand Down Expand Up @@ -270,8 +266,10 @@ def setup_pixelmap(hashcode):


def generate(str, alg):
"""Generates an PIL image avatar based on the given
input String. Acts as the main accessor to pagan."""
"""Generates an PIL image avatar based on the given input String.

Acts as the main accessor to pagan.
"""
img = Image.new(IMAGE_MODE, IMAGE_SIZE, BACKGROUND_COLOR)
hashcode = hash_input(str, alg)
pixelmap = setup_pixelmap(hashcode)
Expand All @@ -280,18 +278,20 @@ def generate(str, alg):


def generate_by_hash(hashcode):
"""Generates an PIL image avatar based on the given
hash String. Acts as the main accessor to pagan."""
"""Generates an PIL image avatar based on the given hash String.

Acts as the main accessor to pagan.
"""
img = Image.new(IMAGE_MODE, IMAGE_SIZE, BACKGROUND_COLOR)
if len(hashcode) < 32:
print ("hashcode must have lenght >= 32, %s" % hashcode)
raise FalseHashError

allowed = "0123456789abcdef"
hashcheck = [c in allowed for c in hashcode]
if False in hashcheck:
print ("hashcode has not allowed structure %s" % hashcode)
raise FalseHashError
allowed = "0123456789ABCDEFabcdef"
for c in hashcode:
if c not in allowed:
print ("hashcode has not allowed structure %s" % hashcode)
raise FalseHashError

pixelmap = setup_pixelmap(hashcode)
draw_image(pixelmap, img)
Expand Down
24 changes: 14 additions & 10 deletions pagan/pagan.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os


class Avatar():
class Avatar(object):

# Default output path is in the current working directory.
DEFAULT_OUTPUT_PATH = os.path.join(os.getcwd(), "output/")
Expand All @@ -18,8 +18,9 @@ def __init__(self, inpt, hashfun=DEFAULT_HASHFUN):
self.img = self.__create_image(inpt, hashfun)

def __create_image(self, inpt, hashfun):
"""Creates the avatar based on the input and
the chosen hash function."""
"""Creates the avatar based on the input and the chosen hash function.

"""
if hashfun not in generator.HASHES.keys():
print ("Unknown or unsupported hash function. Using default: %s"
% self.DEFAULT_HASHFUN)
Expand All @@ -29,22 +30,24 @@ def __create_image(self, inpt, hashfun):
return generator.generate(inpt, algo)

def show(self):
"""Shows a preview of the avatar in an external
image viewer."""
"""Shows a preview of the avatar in an external image viewer."""
self.img.show()

def change(self, inpt, hashfun=DEFAULT_HASHFUN):
"""Change the avatar by providing a new input.
Uses the standard hash function if no one is given."""

Uses the standard hash function if no one is given.
"""
self.img = self.__create_image(inpt, hashfun)

def save(self, path=DEFAULT_OUTPUT_PATH, filename=DEFAULT_FILENAME):
"""Saves a avatar under the given output path to
a given filename. The file ending ".png" is appended
"""Saves a avatar under the given output path to a given filename.

The file ending ".png" is appended
automatically. If the path does not exist, it will be
created. When no parameters are omitted, a default path
and/or filename will be used."""

and/or filename will be used.
"""
# Creates a path when it does not exist.
if not os.path.exists(path):
os.makedirs(path)
Expand All @@ -56,6 +59,7 @@ def save(self, path=DEFAULT_OUTPUT_PATH, filename=DEFAULT_FILENAME):
# Saves the image under the given filepath.
filepath = ("%s%s.png" % (path, filename))
filepath = os.path.join(path, "%s.png" % filename)

# FIXIT: filepath without SUFFIX, print writes false filename
print ("Saving: %s" % filepath)
self.img.save(filepath, 'PNG')
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@
'Environment :: Web Environment',
'Topic :: Software Development :: Libraries :: Python Modules'
],
install_requires=['Pillow>=2.3.0']
install_requires=['Pillow>=2.3.0', 'future']

)
46 changes: 46 additions & 0 deletions test/test_bin_pagan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import os
from subprocess import PIPE
from subprocess import Popen


def test_simple_call():
"""check if calling the script simply works and yields no error"""
p = Popen(["pagan", "--noshow", "--output=/tmp/0101010101.png", "0101"],
stdout=PIPE, stderr=PIPE)
stdout, stderr = p.communicate()
assert stdout
assert not stderr


def test_simple_call_err():
"""fail if called with wrong arguments"""
p = Popen(["pagan", "--output=/tmp/0101010101.png", "--err", "0101"],
stdout=PIPE, stderr=PIPE)
stdout, stderr = p.communicate()
assert not stdout
assert stderr


def test_simple_call_err_no_input():
"""fail if no input given"""
p = Popen(["pagan", "--output=/tmp/0101010101.png"],
stdout=PIPE, stderr=PIPE)
stdout, stderr = p.communicate()
assert not stdout
assert stderr


def test_replace_chars_in_filename():
"""test replace chars in filename, if only called with input

no punctuation allowed, so replace "one.png" with "one-png"
"""
p = Popen(["pagan", "--noshow", "/tmp/one.png"],
stdout=PIPE, stderr=PIPE)
stdout, stderr = p.communicate()
assert not stderr
assert stdout
expected_filename = "-tmp-one-png.png"
assert os.path.isfile(expected_filename)
if os.path.isfile(expected_filename):
os.unlink(expected_filename)
2 changes: 1 addition & 1 deletion test/test_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ def test_generate_by_hash():
img = pagan.generator.generate_by_hash(md5.hexdigest()[:2])

with pytest.raises(pagan.generator.FalseHashError):
img = pagan.generator.generate_by_hash(md5.hexdigest()+"A")
img = pagan.generator.generate_by_hash(md5.hexdigest()+"G")
14 changes: 14 additions & 0 deletions test/test_hypo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# -*- coding: latin-1 -*-
import pagan
from hypothesis import given
import hypothesis.strategies as st


@given(st.text(), st.integers())
def test_hypo(inpt, hashnr):
img = pagan.Avatar(inpt, hashnr)
assert (img.img)


if __name__ == "__main__":
test_hypo()
15 changes: 15 additions & 0 deletions test/test_pagan.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# -*- coding: latin-1 -*-
import os
import random
import string
import tempfile
import pagan

Expand Down Expand Up @@ -49,10 +52,22 @@ def test_save():
img0.save(tmpdir, tmpfile)


def test_save_new_dir():
"""save file in not existing directory"""
img0 = pagan.Avatar("1")
tmpdir = tempfile.gettempdir()
testdir = ''.join(random.SystemRandom().choice(string.digits)
for _ in range(12))
testdir = os.path.join(tmpdir, testdir)
testfile = tempfile.mkstemp("", dir=tmpdir)[1].split("/")[-1]
img0.save(testdir, testfile)


if __name__ == "__main__":
test_create()
test_diffrent_hash()
test_umlaute()
test_show()
test_change()
test_save()
test_save_new_dir()
27 changes: 20 additions & 7 deletions tools/console/pagan
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import argparse
import os
import string
import pagan
from pagan import generator

Expand All @@ -11,29 +12,41 @@ parser.add_argument("input", nargs="+",
help="string, basis of avatar computing")
parser.add_argument("--show", action="store_true",
help="show avatar in external editor, default behavior")
parser.add_argument("--noshow", action="store_true",
help="don't show avatar in external editor")
parser.add_argument("--output",
help="save image to specific output path")
parser.add_argument("--hash", default="MD5",
help="use hash function, allowed %s, default %s" %
(generator.HASHES.values(), list(generator.HASHES.values())[0]))
(generator.HASHES.values(), "MD5"))

args = parser.parse_args()

slogan = " ".join(args.input)
INPUT = " ".join(args.input)

if args.hash and args.hash not in generator.HASHES.values():
HASH = ""
elif args.hash and args.hash in generator.HASHES.values():
HASH = list(generator.HASHES.values()).index(args.hash)
# dict.keys() must not be right ordered
for key in generator.HASHES:
if args.hash == generator.HASHES[key]:
HASH = key
break

if not args.output:
if not args.output and not args.noshow:
args.show = True

img = pagan.Avatar(slogan, HASH)
if args.show:
img = pagan.Avatar(INPUT, HASH)
if not args.noshow and args.show:
img.show()

if args.output:
img.save(*os.path.split(args.output))
else:
img.save(os.getcwd(), filename=args.input[0])
filename = args.input[0]
# make filename save again
allowed = string.ascii_letters + string.digits + "+-_"
filename = "".join([((c in allowed) and c or "-") for c in filename])

img.save(os.getcwd(),
filename=filename)
2 changes: 0 additions & 2 deletions tools/webserver/Readme.md

This file was deleted.

Loading