Skip to content

Commit

Permalink
Support Python 3 without the 2to3 tool
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzogil committed Apr 30, 2016
1 parent 5c9cf65 commit 188ce87
Show file tree
Hide file tree
Showing 16 changed files with 95 additions and 57 deletions.
12 changes: 6 additions & 6 deletions glue/algorithms/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from diagonal import DiagonalAlgorithm
from horizontal import HorizontalAlgorithm
from horizontal_bottom import HorizontalBottomAlgorithm
from square import SquareAlgorithm
from vertical import VerticalAlgorithm
from vertical_right import VerticalRightAlgorithm
from .diagonal import DiagonalAlgorithm
from .horizontal import HorizontalAlgorithm
from .horizontal_bottom import HorizontalBottomAlgorithm
from .square import SquareAlgorithm
from .vertical import VerticalAlgorithm
from .vertical_right import VerticalRightAlgorithm

algorithms = {'diagonal': DiagonalAlgorithm,
'horizontal': HorizontalAlgorithm,
Expand Down
21 changes: 11 additions & 10 deletions glue/bin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from PIL import Image as PImage

from glue.compat import itervalues, iterkeys, text_type
from glue.formats import formats
from glue.helpers import redirect_stdout
from glue import exceptions
Expand All @@ -20,13 +21,13 @@ def main(argv=None):

parser.add_argument("--source", "-s",
dest="source",
type=unicode,
type=text_type,
default=os.environ.get('GLUE_SOURCE', None),
help="Source path")

parser.add_argument("--output", "-o",
dest="output",
type=unicode,
type=text_type,
default=os.environ.get('GLUE_OUTPUT', None),
help="Output path")

Expand Down Expand Up @@ -80,7 +81,7 @@ def main(argv=None):
group.add_argument("-a", "--algorithm",
dest="algorithm",
metavar='NAME',
type=unicode,
type=text_type,
default=os.environ.get('GLUE_ALGORITHM', 'square'),
choices=['square', 'vertical', 'horizontal',
'vertical-right', 'horizontal-bottom',
Expand All @@ -92,15 +93,15 @@ def main(argv=None):
group.add_argument("--ordering",
dest="algorithm_ordering",
metavar='NAME',
type=unicode,
type=text_type,
default=os.environ.get('GLUE_ORDERING', 'maxside'),
choices=['maxside', 'width', 'height', 'area', 'filename',
'-maxside', '-width', '-height', '-area', '-filename'],
help=("Ordering criteria: maxside, width, height, area or "
"filename (default: maxside)"))

# Populate the parser with options required by other formats
for format in formats.itervalues():
for format in itervalues(formats):
format.populate_argument_parser(parser)

#
Expand Down Expand Up @@ -146,7 +147,7 @@ def add_deprecated_argument(*args, **kwargs):
options.enabled_formats.remove('img')

# Fail if any of the deprecated arguments is used
for argument in deprecated_arguments.iterkeys():
for argument in iterkeys(deprecated_arguments):
if getattr(options, argument, None):
parser.error(("{0} argument is deprectated "
"since v0.3").format(deprecated_arguments[argument]))
Expand Down Expand Up @@ -233,16 +234,16 @@ def add_deprecated_argument(*args, **kwargs):
manager.process()
else:
manager.process()
except exceptions.ValidationError, e:
except exceptions.ValidationError as e:
sys.stderr.write(e.args[0])
return e.error_code
except exceptions.SourceImagesNotFoundError, e:
except exceptions.SourceImagesNotFoundError as e:
sys.stderr.write("Error: No images found in %s.\n" % e.args[0])
return e.error_code
except exceptions.NoSpritesFoldersFoundError, e:
except exceptions.NoSpritesFoldersFoundError as e:
sys.stderr.write("Error: No sprites folders found in %s.\n" % e.args[0])
return e.error_code
except exceptions.PILUnavailableError, e:
except exceptions.PILUnavailableError as e:
sys.stderr.write(("Error: PIL {0} decoder is unavailable"
"Please read the documentation and "
"install it before spriting this kind of "
Expand Down
29 changes: 29 additions & 0 deletions glue/compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import sys

PY3 = sys.version_info[0] == 3

if PY3: # pragma: no cover
text_type = str
binary_type = bytes

def itervalues(d):
return d.values()

def iterkeys(d):
return d.keys()

def iteritems(d):
return d.items()

else: # pragma: no cover
text_type = unicode
binary_type = str

def itervalues(d):
return d.itervalues()

def iterkeys(d):
return d.iterkeys()

def iteritems(d):
return d.iteritems()
32 changes: 18 additions & 14 deletions glue/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@
import sys
import copy
import hashlib
import StringIO
import ConfigParser
try:
from StringIO import StringIO
except ImportError:
from io import BytesIO as StringIO

try:
from ConfigParser import RawConfigParser, NoSectionError
except ImportError:
from configparser import RawConfigParser, NoSectionError

from PIL import Image as PILImage

from glue.algorithms import algorithms
from glue.compat import iteritems
from glue.helpers import cached_property, round_up
from glue.formats import ImageFormat
from glue.exceptions import SourceImagesNotFoundError, PILUnavailableError
Expand All @@ -23,11 +31,11 @@ def _get_config_from_file(self, filename, section):
def clean(value):
return {'true': True, 'false': False}.get(value.lower(), value)

config = ConfigParser.RawConfigParser()
config = RawConfigParser()
config.read(os.path.join(self.config_path, filename))
try:
keys = config.options(section)
except ConfigParser.NoSectionError:
except NoSectionError:
return {}
return dict([[k, clean(config.get(section, k))] for k in keys])

Expand All @@ -48,16 +56,13 @@ def __init__(self, path, config):
with open(self.path, "rb") as img:
self._image_data = img.read()

print "\t{0} added to sprite".format(self.filename)
print("\t{0} added to sprite".format(self.filename))

@cached_property
def image(self):
"""Return a Pil representation of this image """

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

try:
source_image = PILImage.open(imageio)
Expand All @@ -70,7 +75,7 @@ def image(self):
img.paste(source_image, (0, 0), mask=mask)
else:
img.paste(source_image, (0, 0))
except IOError, e:
except IOError as e:
raise PILUnavailableError(e.args[0].split()[1])
finally:
imageio.close()
Expand Down Expand Up @@ -105,7 +110,6 @@ def margin(self):
return self._generate_spacing_info(self.config['margin'])

def _generate_spacing_info(self, data):

data = data.split(',' if ',' in data else ' ')

if len(data) == 4:
Expand All @@ -119,7 +123,7 @@ def _generate_spacing_info(self, data):
else:
data = [0] * 4

return map(int, data)
return list(map(int, data))

@cached_property
def horizontal_spacing(self):
Expand Down Expand Up @@ -196,7 +200,7 @@ def __init__(self, path, config, name=None):
if ratio_output_key not in self.config:
self.config[ratio_output_key] = img_format.output_path(ratio)

print "Processing '{0}':".format(self.name)
print("Processing '{0}':".format(self.name))

# Generate sprite map
self.process()
Expand All @@ -220,7 +224,7 @@ def hash(self):
hash_list.append(os.path.relpath(image.path))
hash_list.append(image._image_data)

for key, value in self.config.iteritems():
for key, value in iteritems(self.config):
hash_list.append(key)
hash_list.append(value)

Expand Down
3 changes: 2 additions & 1 deletion glue/formats/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from glue.helpers import round_up, nearest_fration
from glue import __version__
from glue.compat import iteritems


class BaseFormat(object):
Expand Down Expand Up @@ -54,7 +55,7 @@ def validate(self):
@property
def format_label(self):
from glue.formats import formats
return dict((v,k) for k, v in formats.iteritems())[self.__class__]
return dict((v, k) for k, v in iteritems(formats))[self.__class__]

@classmethod
def populate_argument_parser(cls, parser):
Expand Down
2 changes: 1 addition & 1 deletion glue/formats/caat.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os

from base import BaseJSONFormat
from .base import BaseJSONFormat


class CAATFormat(BaseJSONFormat):
Expand Down
2 changes: 1 addition & 1 deletion glue/formats/cocos2d.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os

from base import BasePlistFormat
from .base import BasePlistFormat


class Cocos2dFormat(BasePlistFormat):
Expand Down
17 changes: 9 additions & 8 deletions glue/formats/css.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import codecs

from glue import __version__
from base import JinjaTextFormat
from glue.compat import iteritems, text_type
from .base import JinjaTextFormat

from ..exceptions import ValidationError

Expand Down Expand Up @@ -54,20 +55,20 @@ def populate_argument_parser(cls, parser):

group.add_argument("--namespace",
dest="css_namespace",
type=unicode,
type=text_type,
default=os.environ.get('GLUE_CSS_NAMESPACE', 'sprite'),
help="Namespace for all css classes (default: sprite)")

group.add_argument("--sprite-namespace",
dest="css_sprite_namespace",
type=unicode,
type=text_type,
default=os.environ.get('GLUE_CSS_SPRITE_NAMESPACE',
'{sprite_name}'),
help="Namespace for all sprites (default: {sprite_name})")

group.add_argument("-u", "--url",
dest="css_url",
type=unicode,
type=text_type,
default=os.environ.get('GLUE_CSS_URL', ''),
help="Prepend this string to the sprites path")

Expand All @@ -94,7 +95,7 @@ def populate_argument_parser(cls, parser):

group.add_argument("--separator",
dest="css_separator",
type=unicode,
type=text_type,
default=os.environ.get('GLUE_CSS_SEPARATOR', '-'),
metavar='SEPARATOR',
help=("Customize the separator used to join CSS class "
Expand All @@ -103,7 +104,7 @@ def populate_argument_parser(cls, parser):

group.add_argument("--pseudo-class-separator",
dest="css_pseudo_class_separator",
type=unicode,
type=text_type,
default=os.environ.get('GLUE_CSS_PSEUDO_CLASS_SEPARATOR', '__'),
metavar='SEPARATOR',
help=("Customize the separator glue will use in order "
Expand Down Expand Up @@ -163,7 +164,7 @@ def get_context(self, *args, **kwargs):
if self.sprite.config['css_url']:
context['sprite_path'] = '{0}{1}'.format(self.sprite.config['css_url'], context['sprite_filename'])

for r, ratio in context['ratios'].iteritems():
for r, ratio in iteritems(context['ratios']):
ratio['sprite_path'] = '{0}{1}'.format(self.sprite.config['css_url'], ratio['sprite_filename'])

# Add cachebuster if required
Expand All @@ -174,7 +175,7 @@ def apply_cachebuster(path):

context['sprite_path'] = apply_cachebuster(context['sprite_path'])

for r, ratio in context['ratios'].iteritems():
for r, ratio in iteritems(context['ratios']):
ratio['sprite_path'] = apply_cachebuster(ratio['sprite_path'])

return context
Expand Down
9 changes: 5 additions & 4 deletions glue/formats/img.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from PIL import PngImagePlugin

from glue import __version__
from glue.compat import text_type
from glue.helpers import round_up, cached_property
from .base import BaseFormat

Expand All @@ -19,7 +20,7 @@ def populate_argument_parser(cls, parser):

group.add_argument("--img",
dest="img_dir",
type=unicode,
type=text_type,
default=os.environ.get('GLUE_IMG', True),
metavar='DIR',
help="Output directory for img files")
Expand All @@ -38,13 +39,13 @@ def populate_argument_parser(cls, parser):

group.add_argument("-p", "--padding",
dest="padding",
type=unicode,
type=text_type,
default=os.environ.get('GLUE_PADDING', '0'),
help="Force this padding in all images")

group.add_argument("--margin",
dest="margin",
type=unicode,
type=text_type,
default=os.environ.get('GLUE_MARGIN', '0'),
help="Force this margin in all images")

Expand All @@ -57,7 +58,7 @@ def populate_argument_parser(cls, parser):

group.add_argument("--ratios",
dest="ratios",
type=unicode,
type=text_type,
default=os.environ.get('GLUE_RATIOS', '1'),
help="Create sprites based on these ratios")

Expand Down
Loading

0 comments on commit 188ce87

Please sign in to comment.