Skip to content

Commit

Permalink
First working version of Python 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorge Bastida committed Apr 20, 2014
1 parent 33fa00c commit 4d4110d
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 17 deletions.
18 changes: 12 additions & 6 deletions glue/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,14 @@ def __init__(self, path, config):
@cached_property
def image(self):
"""Return a Pil representation of this image """
io = StringIO.StringIO(self._image_data)

if sys.version < '3':
imageio = StringIO.StringIO(self._image_data)
else:
imageio = StringIO.BytesIO(self._image_data)

try:
source_image = PILImage.open(io)
source_image = PILImage.open(imageio)
img = PILImage.new('RGBA', source_image.size, (0, 0, 0, 0))

if source_image.mode == 'L':
Expand All @@ -68,7 +73,7 @@ def image(self):
except IOError, e:
raise PILUnavailableError(e.args[0].split()[1])
finally:
io.close()
imageio.close()

self.original_width, self.original_height = img.size

Expand Down Expand Up @@ -147,7 +152,7 @@ def __lt__(self, img):
ordering = ordering[1:] if ordering.startswith('-') else ordering

if ordering == "filename":
return cmp(self.filename, img.filename) > 0
return sorted([self.filename, img.filename])[0] == img.filename
if ordering == 'width':
return self.absolute_width <= img.absolute_width
elif ordering == 'height':
Expand All @@ -158,7 +163,6 @@ def __lt__(self, img):
return max(self.absolute_width, self.absolute_height) <= max(img.absolute_width, img.absolute_height)



class Sprite(ConfigurableFromFile):

config_filename = 'sprite.conf'
Expand Down Expand Up @@ -220,7 +224,9 @@ def hash(self):
hash_list.append(key)
hash_list.append(value)

return hashlib.sha1(''.join(map(str, hash_list))).hexdigest()[:10]
if sys.version < '3':
return hashlib.sha1(''.join(map(str, hash_list))).hexdigest()[:10]
return hashlib.sha1(''.join(map(str, hash_list)).encode('utf-8')).hexdigest()[:10]

@cached_property
def canvas_size(self):
Expand Down
5 changes: 4 additions & 1 deletion glue/formats/base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import sys
import json
import codecs
import plistlib
Expand Down Expand Up @@ -168,7 +169,9 @@ class BasePlistFormat(BaseTextFormat):

def render(self, *args, **kwargs):
context = self.get_context(*args, **kwargs)
return plistlib.writePlistToString(context)
if sys.version < '3':
return plistlib.writePlistToString(context)
return plistlib.writePlistToBytes(context).decode('unicode_escape')

def needs_rebuild(self):
for ratio in self.sprite.config['ratios']:
Expand Down
2 changes: 1 addition & 1 deletion glue/formats/css.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class CssFormat(JinjaTextFormat):
width: {{ image.width }}px;
height: {{ image.height }}px;
}
{% endfor %}{% for r, ratio in ratios.iteritems() %}
{% endfor %}{% for r, ratio in ratios.items() %}
@media screen and (-webkit-min-device-pixel-ratio: {{ ratio.ratio }}), screen and (min--moz-device-pixel-ratio: {{ ratio.ratio }}), screen and (-o-min-device-pixel-ratio: {{ ratio.fraction }}), screen and (min-device-pixel-ratio: {{ ratio.ratio }}), screen and (min-resolution: {{ ratio.ratio }}dppx) {
{% for image in images %}.{{ image.label }}{{ image.pseudo }}{% if not image.last %},{{"\n"}} {% endif %}{% endfor %} {
background-image: url('{{ ratio.sprite_path }}');
Expand Down
2 changes: 1 addition & 1 deletion glue/formats/less.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class LessFormat(CssFormat):
-webkit-background-size: {{ width }}px {{ height }}px;
-moz-background-size: {{ width }}px {{ height }}px;
background-size: {{ width }}px {{ height }}px;
{% for r, ratio in ratios.iteritems() %}
{% for r, ratio in ratios.items() %}
@media screen and (-webkit-min-device-pixel-ratio: {{ ratio.ratio }}), screen and (min--moz-device-pixel-ratio: {{ ratio.ratio }}),screen and (-o-min-device-pixel-ratio: {{ ratio.fraction }}),screen and (min-device-pixel-ratio: {{ ratio.ratio }}),screen and (min-resolution: {{ ratio.ratio }}dppx){
background-image:url('{{ ratio.sprite_path }}');
}
Expand Down
6 changes: 4 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,20 @@
tests_require=tests_require,
test_suite='tests',
classifiers=[
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 3',
'Development Status :: 4 - Beta',
'Environment :: Web Environment',
'Intended Audience :: Developers',
'License :: OSI Approved :: BSD License',
'Operating System :: OS Independent',
'Programming Language :: Python',
'Topic :: Utilities'
],
entry_points = {
'console_scripts': [
'glue = glue.bin:main',
]
},
zip_safe = False
zip_safe = False,
use_2to3=True
)
21 changes: 15 additions & 6 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,21 @@
import shutil
import unittest
import logging
from StringIO import StringIO

try:
from StringIO import StringIO
except ImportError:
from io import StringIO

from plistlib import readPlist

from PIL import Image as PILImage
import cssutils
from mock import patch, Mock

try:
from mock import patch, Mock
except ImportError:
from unittest.mock import patch, Mock

from glue.bin import main
from glue.core import Image
Expand Down Expand Up @@ -100,8 +109,8 @@ def create_image(self, path, color=RED, size=(64, 64), margin=0, margin_color=TR
os.makedirs(dirname)
image = PILImage.new('RGB', size, color)
if margin:
background = PILImage.new('RGBA', map(lambda x: x + margin, size), margin_color)
background.paste(image, (margin / 2, margin / 2))
background = PILImage.new('RGBA', list(map(lambda x: x + margin, size)), margin_color)
background.paste(image, tuple(map(int, (margin / 2, margin / 2))))
background.save(path)
else:
image.save(path)
Expand Down Expand Up @@ -1559,8 +1568,8 @@ def test_cocos2d(self):
self.assertColor("output/simple.png", BLUE, ((64, 0), (127, 63)))

meta = readPlist("output/simple.plist")
self.assertEqual(meta.keys(), ['frames', 'metadata'])
self.assertEqual(meta['frames'].keys(), ['blue.png', 'red.png'])
self.assertEqual(set(meta.keys()), set(['frames', 'metadata']))
self.assertEqual(set(meta['frames'].keys()), set(['blue.png', 'red.png']))

code = self.call("glue simple output --cocos2d")
self.assertEqual(code, 0)
Expand Down

0 comments on commit 4d4110d

Please sign in to comment.