Skip to content

Commit 44db7da

Browse files
committed
Merge branch 'release/0.4.3'
2 parents af6159a + 04a0a7b commit 44db7da

13 files changed

+101
-33
lines changed

.travis.yml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
language: python
2+
#python:
3+
# - "2.7"
4+
# - "3.4"
5+
install:
6+
- pip install tox
7+
script:
8+
- tox

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
setup(
55
name = "wcf-binary parser",
6-
version = "0.4.2",
6+
version = "0.4.3",
77
author = "Timo Schmid",
88
author_email = "[email protected]",
99
description = ("A library for transforming wcf-binary data from and to xml"),

tests/__init__.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1+
from __future__ import absolute_import
2+
3+
from . import alltests
4+
15
__all__ = ['alltests']
2-
import alltests

tests/alltests.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
import doctest
77
import unittest
88
import sys
9+
from codecs import decode
910

1011
sys.path.append('..')
1112

1213
from wcf.records import *
1314

14-
test_bin = (
15+
test_bin = decode(
1516
"56020b0173040b0161065608440a1e0082993a687474703a2f2f646f6373"
1617
"2e6f617369732d6f70656e2e6f72672f77732d73782f77732d7472757374"
1718
"2f3230303531322f5253542f4973737565441aad5db293d4bc0ba547b9dc"
@@ -30,7 +31,7 @@
3031
"757374074b657953697a658b0001410574727573740e42696e6172794578"
3132
"6368616e67650674aaa60306d402aad8029e364e544c4d53535000010000"
3233
"00b7b218e20a000a002d00000005000500280000000601b11d0000000f43"
33-
"4c5753315745425345525649439f0145010101").decode('hex')
34+
"4c5753315745425345525649439f0145010101", "hex")
3435

3536
class TransformTest(unittest.TestCase):
3637

tox.ini

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[tox]
2+
#envlist = py27,py34
3+
envlist = py27
4+
[testenv]
5+
deps=pytest
6+
commands=py.test --doctest-modules tests wcf

wcf/MyHTMLParser.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010
# data -- only char and entity references and end tags are special)
1111
# and CDATA (character data -- only end tags are special).
1212

13+
try:
14+
import markupbase
15+
except ImportError:
16+
import _markupbase as markupbase
1317

14-
import markupbase
1518
import re
1619

1720
# Regular expressions used for parsing

wcf/datatypes.py

+24-11
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import struct
3131
import logging
32+
import sys
3233

3334
log = logging.getLogger(__name__)
3435

@@ -105,6 +106,12 @@ class Utf8String(object):
105106

106107
def __init__(self, *args):
107108
self.value = args[0] if len(args) else None
109+
if sys.version_info >= (3,0,0):
110+
if not isinstance(self.value, str):
111+
self.value = self.value.decode('utf-8')
112+
else:
113+
if not isinstance(self.value, unicode):
114+
self.value = self.value.decode('utf-8')
108115

109116
def to_bytes(self):
110117
"""
@@ -115,28 +122,28 @@ def to_bytes(self):
115122
>>> Utf8String("\\xc3\\xbcber".decode('utf-8')).to_bytes()
116123
'\\x05\\xc3\\xbcber'
117124
"""
118-
data = self.value.encode('utf-8')
125+
data = self.value.encode('utf-8')
119126
strlen = len(data)
120127

121128
return MultiByteInt31(strlen).to_bytes() + data
122129

123130
def __str__(self):
124-
return self.value.decode('latin1')
131+
return self.value.encode('utf-8')
125132

126133
def __unicode__(self):
127-
return self.value
134+
return self.value
128135

129136
@classmethod
130137
def parse(cls, fp):
131-
"""
132-
>>> from StringIO import StringIO as io
133-
>>> fp = io("\\x05\\xc3\\xbcber")
134-
>>> s = Utf8String.parse(fp)
135-
>>> s.to_bytes()
138+
"""
139+
>>> from StringIO import StringIO as io
140+
>>> fp = io("\\x05\\xc3\\xbcber")
141+
>>> s = Utf8String.parse(fp)
142+
>>> s.to_bytes()
136143
'\\x05\\xc3\\xbcber'
137-
>>> print str(s)
138-
'über'
139-
"""
144+
>>> print str(s)
145+
über
146+
"""
140147
lngth = struct.unpack('<B', fp.read(1))[0]
141148

142149
return cls(fp.read(lngth).decode('utf-8'))
@@ -187,6 +194,12 @@ def __str__(self):
187194

188195
@classmethod
189196
def parse(cls, fp):
197+
"""
198+
>>> from StringIO import StringIO as io
199+
>>> buf = io('\\x00\\x00\\x06\\x00\\x00\\x00\\x00\\x00\\x80-N\\x00\\x00\\x00\\x00\\x00')
200+
>>> str(Decimal.parse(buf))
201+
'5.123456'
202+
"""
190203
log.warn('Possible false interpretation')
191204
fp.read(2)
192205
scale = struct.unpack('<B', fp.read(1))[0]

wcf/dictionary.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -516,4 +516,4 @@
516516
0x3CC : 'detail',
517517
}
518518

519-
inverted_dict = dict([(v, k) for (k, v) in dictionary.iteritems()])
519+
inverted_dict = dict([(v, k) for (k, v) in dictionary.items()])

wcf/records/__init__.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2727
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2828
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29-
from __future__ import absolute_import
29+
from __future__ import absolute_import, print_function
3030
import sys
3131
import logging
3232

@@ -81,7 +81,7 @@ def repr_records(records, skip=0):
8181
return
8282

8383
for r in records:
84-
print ' '*skip + str(r)
84+
print(' '*skip + str(r))
8585
if hasattr(r, 'childs'):
8686
repr_records(r.childs, skip+1)
8787

@@ -92,9 +92,9 @@ def dump_records(records):
9292
:param records: the record tree
9393
:type records: wcf.records.Record
9494
:returns: a bytestring
95-
:rtype: str
95+
:rtype: str|bytes
9696
"""
97-
out = ''
97+
out = b''
9898

9999
for r in records:
100100
msg = 'Write %s' % type(r).__name__

wcf/records/text.py

+24-7
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@
3333
import datetime
3434
import logging
3535
import uuid
36-
from htmlentitydefs import codepoint2name
36+
import sys
37+
38+
try:
39+
from htmlentitydefs import codepoint2name
40+
except ImportError:
41+
from html.entities import codepoint2name
3742

3843

3944
def escapecp(cp):
@@ -188,10 +193,16 @@ class UnicodeChars8TextRecord(Text):
188193
type = 0xB6
189194

190195
def __init__(self, string):
191-
if isinstance(string, unicode):
192-
self.value = string
196+
if sys.version_info >= (3, 0, 0):
197+
if isinstance(string, str):
198+
self.value = string
199+
else:
200+
self.value = str(string)
193201
else:
194-
self.value = unicode(string)
202+
if isinstance(string, unicode):
203+
self.value = string
204+
else:
205+
self.value = unicode(string)
195206

196207
def to_bytes(self):
197208
"""
@@ -423,10 +434,16 @@ class Chars8TextRecord(Text):
423434
type = 0x98
424435

425436
def __init__(self, value):
426-
if isinstance(value, unicode):
427-
self.value = value
437+
if sys.version_info >= (3, 0, 0):
438+
if isinstance(value, str):
439+
self.value = value
440+
else:
441+
self.value = str(value)
428442
else:
429-
self.value = unicode(value)
443+
if isinstance(value, unicode):
444+
self.value = value
445+
else:
446+
self.value = unicode(value)
430447

431448
def __str__(self):
432449
return escape(self.value)

wcf/xml2records.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
from __future__ import absolute_import
55

66
from wcf.MyHTMLParser import HTMLParser
7-
from htmlentitydefs import name2codepoint
7+
try:
8+
from htmlentitydefs import name2codepoint
9+
except ImportError:
10+
from html.entities import name2codepoint
11+
812
import re
913
import base64
1014
import logging
@@ -262,7 +266,11 @@ def handle_comment(self, comment):
262266
self.last_record.childs.append(CommentRecord(comment))
263267

264268
def parse_marked_section(self, i, report=1):
265-
from markupbase import _markedsectionclose, _msmarkedsectionclose
269+
try:
270+
from markupbase import _markedsectionclose, _msmarkedsectionclose
271+
except ImportError:
272+
from _markupbase import _markedsectionclose, _msmarkedsectionclose
273+
266274
rawdata= self.rawdata
267275
assert rawdata[i:i+3] == '<![', "unexpected call to parse_marked_section()"
268276
sectName, j = self._scan_name( i+3, i )

wcf2xml.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,12 @@
2929

3030
if __name__ == '__main__':
3131
import sys
32-
from wcf.records import Record,print_records
33-
fp = sys.stdin
32+
from wcf.records import Record, print_records
33+
34+
if sys.version_info >= (3, 0, ):
35+
fp = sys.stdin.buffer
36+
else:
37+
fp = sys.stdin
3438
if len(sys.argv) > 1:
3539
filename = sys.argv[1]
3640
fp = open(filename, 'rb')

xml2wcf.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,9 @@
1919

2020
with fp:
2121
r = XMLParser.parse(fp)
22-
sys.stdout.write(dump_records(r))
22+
data = dump_records(r)
23+
24+
if sys.version_info >= (3, 0, 0):
25+
sys.stdout.buffer.write(data)
26+
else:
27+
sys.stdout.write(data)

0 commit comments

Comments
 (0)