Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add python3 support #40

Merged
merged 3 commits into from
Feb 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions bin/swf2svg.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import absolute_import
import argparse
from swf.movie import SWF
from swf.export import SVGExporter
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import absolute_import
import os
from setuptools import setup, find_packages

Expand All @@ -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=[
Expand Down
4 changes: 3 additions & 1 deletion swf/actions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

from __future__ import absolute_import
import six
class Action(object):
def __init__(self, code, length):
self._code = code
Expand Down Expand Up @@ -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

Expand Down
11 changes: 7 additions & 4 deletions swf/data.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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]"
Expand Down
33 changes: 17 additions & 16 deletions swf/export.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
"""
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 io import BytesIO
from six.moves import cStringIO
import math
import re
import copy
Expand Down Expand Up @@ -405,7 +406,7 @@ def export(self, swf, force_stroke=False):
self.export_display_list(self.get_display_tags(swf.tags))

def export_define_bits(self, tag):
png_buffer = StringIO()
png_buffer = BytesIO()
image = None
if isinstance(tag, TagDefineBitsJPEG3):

Expand All @@ -425,14 +426,14 @@ def export_define_bits(self, tag):
alpha = ord(tag.bitmapAlphaData.read(1))
rgb = list(image_data[i])
buff += struct.pack("BBBB", rgb[0], rgb[1], rgb[2], alpha)
image = Image.fromstring("RGBA", (image_width, image_height), buff)
image = Image.frombytes("RGBA", (image_width, image_height), buff)
elif isinstance(tag, TagDefineBitsJPEG2):
tag.bitmapData.seek(0)
image = Image.open(tag.bitmapData)
else:
tag.bitmapData.seek(0)
if self.jpegTables is not None:
buff = StringIO()
buff = BytesIO()
self.jpegTables.seek(0)
buff.write(self.jpegTables.read())
buff.write(tag.bitmapData.read())
Expand Down Expand Up @@ -545,7 +546,7 @@ def export(self, swf, force_stroke=False):
return self._serialize()

def _serialize(self):
return StringIO(etree.tostring(self.svg,
return cStringIO(etree.tostring(self.svg,
encoding="UTF-8", xml_declaration=True))

def export_define_sprite(self, tag, parent=None):
Expand Down Expand Up @@ -794,7 +795,7 @@ def export_filter_dropshadow(self, swf_filter, svg_filter, blend_in=None, result

def export_image(self, tag, image=None):
if image is not None:
buff = StringIO()
buff = BytesIO()
image.save(buff, "PNG")
buff.seek(0)
data_url = _encode_png(buff.read())
Expand Down Expand Up @@ -992,7 +993,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):
Expand Down
5 changes: 4 additions & 1 deletion swf/filters.py
Original file line number Diff line number Diff line change
@@ -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):
"""
Expand Down
1 change: 1 addition & 0 deletions swf/geom.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import absolute_import
import math

SNAP = 0.001
Expand Down
15 changes: 7 additions & 8 deletions swf/movie.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
"""
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
from io import BytesIO

class SWFHeaderException(Exception):
""" Exception raised in case of an invalid SWFHeader """
Expand Down Expand Up @@ -144,7 +143,7 @@ def parse(self, data):
self._data = data = data if isinstance(data, SWFStream) else SWFStream(data)
self._header = SWFHeader(self._data)
if self._header.compressed:
temp = StringIO.StringIO()
temp = BytesIO()
if self._header.compressed_zlib:
import zlib
data = data.f.read()
Expand Down
7 changes: 4 additions & 3 deletions swf/sound.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
27 changes: 15 additions & 12 deletions swf/stream.py
Original file line number Diff line number Diff line change
@@ -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):
"""
Expand Down Expand Up @@ -48,7 +51,7 @@ def _make_masks(self):

def _read_bytes_aligned(self, bytes):
buf = self.f.read(bytes)
return reduce(lambda x, y: x << 8 | ord(y), buf, 0)
return reduce(lambda x, y: x << 8 | y, buf, 0)

def readbits(self, bits):
"""
Expand All @@ -61,7 +64,7 @@ def readbits(self, bits):

# fast byte-aligned path
if bits % 8 == 0 and self._bits_pending == 0:
return self._read_bytes_aligned(bits / 8)
return self._read_bytes_aligned(bits // 8)

out = 0
masks = self._masks
Expand Down Expand Up @@ -94,7 +97,7 @@ def transfer_bits(x, y, n, t):
continue

r = self.f.read(1)
if r == '':
if r == b'':
raise EOFError
self._partial_byte = ord(r)
self._bits_pending = 8
Expand Down Expand Up @@ -365,11 +368,11 @@ def readSYMBOL(self):
def readString(self):
""" Read a string """
s = self.f.read(1)
string = ""
string = b""
while ord(s) > 0:
string += s
s = self.f.read(1)
return string
return string.decode()

def readFILTER(self):
""" Read a SWFFilter """
Expand All @@ -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 """
Expand Down Expand Up @@ -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 """
Expand Down Expand Up @@ -494,4 +497,4 @@ def int32(x):
return -x
else:
return -2147483648
return x
return x
Loading