From 339c85ea658efd68892135b3c3773f213b33447d Mon Sep 17 00:00:00 2001 From: Nicolas F Date: Sat, 20 Feb 2016 21:05:01 +0100 Subject: [PATCH] Add python3 support Fixes issue #34 --- bin/swf2svg.py | 1 + setup.py | 3 ++- swf/actions.py | 4 +++- swf/data.py | 11 +++++++---- swf/export.py | 22 +++++++++++----------- swf/filters.py | 5 ++++- swf/geom.py | 1 + swf/movie.py | 12 +++++------- swf/sound.py | 7 ++++--- swf/stream.py | 15 +++++++++------ swf/tag.py | 21 ++++++++++----------- swf/utils.py | 3 ++- test/test_swf.py | 1 + 13 files changed, 60 insertions(+), 46 deletions(-) diff --git a/bin/swf2svg.py b/bin/swf2svg.py index c57d9c1..78430d6 100644 --- a/bin/swf2svg.py +++ b/bin/swf2svg.py @@ -1,3 +1,4 @@ +from __future__ import absolute_import import argparse from swf.movie import SWF from swf.export import SVGExporter diff --git a/setup.py b/setup.py index 9de9b8e..718dcca 100644 --- a/setup.py +++ b/setup.py @@ -1,3 +1,4 @@ +from __future__ import absolute_import import os from setuptools import setup, find_packages @@ -19,7 +20,7 @@ def read(fname): author_email='tim@floorplanner.com', url='https://github.com/timknip/pyswf', - install_requires = ["lxml>=3.3.0", "Pillow>=2.3.0", "pylzma>=0.4.6"], + install_requires = ["lxml>=3.3.0", "Pillow>=2.3.0", "pylzma>=0.4.6", "six"], packages=find_packages(), license = "MIT", classifiers=[ diff --git a/swf/actions.py b/swf/actions.py index 0e2ecc3..0158fad 100644 --- a/swf/actions.py +++ b/swf/actions.py @@ -1,4 +1,6 @@ +from __future__ import absolute_import +import six class Action(object): def __init__(self, code, length): self._code = code @@ -177,7 +179,7 @@ def __init__(self, code, length): # urgh! some 100 to go... ActionTable = {} -for name, value in dict(locals()).iteritems(): +for name, value in six.iteritems(dict(locals())): if type(value) == type and issubclass(value, Action) and hasattr(value, 'CODE'): ActionTable[value.CODE] = value diff --git a/swf/data.py b/swf/data.py index 6c679b5..1d20d60 100644 --- a/swf/data.py +++ b/swf/data.py @@ -1,5 +1,8 @@ -from consts import * -from utils import * +from __future__ import absolute_import +from .consts import * +from .utils import * +from six.moves import map +from six.moves import range class _dumb_repr(object): def __repr__(self): @@ -70,7 +73,7 @@ def parse(self, data, level=1): def export(self, handler=None): self._create_edge_maps() if handler is None: - from export import SVGShapeExporter + from .export import SVGShapeExporter handler = SVGShapeExporter() handler.begin_shape() for i in range(0, self.num_groups): @@ -1340,7 +1343,7 @@ def parse(self, data): self.outPoint = data.readUI32() if self.hasOutPoint else None self.loopCount = data.readUI16() if self.hasLoops else None self.envPointCount = data.readUI8() if self.hasEnvelope else None - self.envelopePoints = [data.readSOUNDENVELOPE() for x in xrange(self.envPointCount)] if self.hasEnvelope else None + self.envelopePoints = [data.readSOUNDENVELOPE() for x in range(self.envPointCount)] if self.hasEnvelope else None def __str__(self): return "[SWFSoundInfo]" diff --git a/swf/export.py b/swf/export.py index cbb0b3f..727dc96 100644 --- a/swf/export.py +++ b/swf/export.py @@ -1,23 +1,23 @@ """ This module defines exporters for the SWF fileformat. """ -from consts import * -from geom import * -from utils import * -from data import * -from tag import * -from filters import * +from __future__ import absolute_import +from .consts import * +from .geom import * +from .utils import * +from .data import * +from .tag import * +from .filters import * from lxml import objectify from lxml import etree import base64 +from six.moves import map +from six.moves import range try: import Image except ImportError: from PIL import Image -try: - from cStringIO import StringIO -except ImportError: - from StringIO import StringIO +from six.moves import cStringIO import math import re import copy @@ -992,7 +992,7 @@ def _parse(self, element): def _build_matrix(self, transform): if transform.find("matrix") >= 0: raw = str(transform).replace("matrix(", "").replace(")", "") - f = map(float, re.split("\s+|,", raw)) + f = list(map(float, re.split("\s+|,", raw))) return Matrix2(f[0], f[1], f[2], f[3], f[4], f[5]) def _calc_combined_matrix(self): diff --git a/swf/filters.py b/swf/filters.py index 887f29a..cd59637 100644 --- a/swf/filters.py +++ b/swf/filters.py @@ -1,4 +1,7 @@ -from utils import ColorUtils +from __future__ import absolute_import +from .utils import ColorUtils +from six.moves import map +from six.moves import range class Filter(object): """ diff --git a/swf/geom.py b/swf/geom.py index 5875847..6406d4a 100644 --- a/swf/geom.py +++ b/swf/geom.py @@ -1,3 +1,4 @@ +from __future__ import absolute_import import math SNAP = 0.001 diff --git a/swf/movie.py b/swf/movie.py index 3fd9746..98293a9 100644 --- a/swf/movie.py +++ b/swf/movie.py @@ -1,13 +1,11 @@ """ SWF """ -from tag import SWFTimelineContainer -from stream import SWFStream -from export import SVGExporter -try: - import cStringIO as StringIO -except ImportError: - import StringIO +from __future__ import absolute_import +from .tag import SWFTimelineContainer +from .stream import SWFStream +from .export import SVGExporter +from six.moves import cStringIO class SWFHeaderException(Exception): """ Exception raised in case of an invalid SWFHeader """ diff --git a/swf/sound.py b/swf/sound.py index ce4d2bc..dc62c0b 100644 --- a/swf/sound.py +++ b/swf/sound.py @@ -1,7 +1,8 @@ -import consts -import tag +from __future__ import absolute_import +from . import consts +from . import tag import wave -import stream +from . import stream supportedCodecs = ( consts.AudioCodec.MP3, diff --git a/swf/stream.py b/swf/stream.py index 3e83c41..894fc65 100644 --- a/swf/stream.py +++ b/swf/stream.py @@ -1,7 +1,10 @@ +from __future__ import absolute_import import struct, math -from data import * -from actions import * -from filters import SWFFilterFactory +from .data import * +from .actions import * +from .filters import SWFFilterFactory +from six.moves import range +from functools import reduce class SWFStream(object): """ @@ -381,7 +384,7 @@ def readFILTER(self): def readFILTERLIST(self): """ Read a length-prefixed list of FILTERs """ number = self.readUI8() - return [self.readFILTER() for _ in xrange(number)] + return [self.readFILTER() for _ in range(number)] def readZONEDATA(self): """ Read a SWFZoneData """ @@ -440,14 +443,14 @@ def readMORPHFILLSTYLEARRAY(self): count = self.readUI8() if count == 0xff: count = self.readUI16() - return [self.readMORPHFILLSTYLE() for _ in xrange(count)] + return [self.readMORPHFILLSTYLE() for _ in range(count)] def readMORPHLINESTYLEARRAY(self, version): count = self.readUI8() if count == 0xff: count = self.readUI16() kind = self.readMORPHLINESTYLE if version == 1 else self.readMORPHLINESTYLE2 - return [kind() for _ in xrange(count)] + return [kind() for _ in range(count)] def readraw_tag(self): """ Read a SWFRawTag """ diff --git a/swf/tag.py b/swf/tag.py index a6f20a9..eb3898c 100644 --- a/swf/tag.py +++ b/swf/tag.py @@ -1,18 +1,17 @@ -from consts import * -from data import * -from utils import * -from stream import * +from __future__ import absolute_import +from .consts import * +from .data import * +from .utils import * +from .stream import * import datetime +from six.moves import range try: import Image except ImportError: from PIL import Image import struct -try: - import cStringIO as StringIO -except ImportError: - import StringIO +from six.moves import cStringIO class TagFactory(object): @classmethod @@ -883,7 +882,7 @@ def parse(self, data, length, version=1): # create the image buffer s = StringIO.StringIO() - for i in xrange(t): + for i in range(t): s.write(indexed_colors[ord(temp.read(1))]) self.image_buffer = s.getvalue() s.close() @@ -2220,7 +2219,7 @@ def type(self): def parse(self, data, length, version=1): self.count = data.readUI16() - self.exports = [data.readEXPORT() for i in xrange(self.count)] + self.exports = [data.readEXPORT() for i in range(self.count)] def __str__(self): s = super(TagExportAssets, self).__str__() @@ -2642,7 +2641,7 @@ def parse(self, data, length, version=1): if __name__ == '__main__': # some table checks - for x in xrange(256): + for x in range(256): y = TagFactory.create(x) if y: assert y.type == x, y.name + ' is misnamed' diff --git a/swf/utils.py b/swf/utils.py index aa02b33..964b237 100644 --- a/swf/utils.py +++ b/swf/utils.py @@ -1,4 +1,5 @@ -from consts import BitmapType +from __future__ import absolute_import +from .consts import BitmapType import math class NumberUtils(object): diff --git a/test/test_swf.py b/test/test_swf.py index 63c8c7c..6659e94 100644 --- a/test/test_swf.py +++ b/test/test_swf.py @@ -1,3 +1,4 @@ +from __future__ import absolute_import from swf.movie import SWF def test_header():