| 7 | | from wx.tools.img2py import * |
| | 7 | import zlib |
| | 8 | |
| | 9 | # crunch_data was removed from img2py as of wxPython 2.8.8 |
| | 10 | def crunch_data(data, compressed): |
| | 11 | # compress it? |
| | 12 | if compressed: |
| | 13 | data = zlib.compress(data, 9) |
| | 14 | |
| | 15 | # convert to a printable format, so it can be in a Python source file |
| | 16 | data = repr(data) |
| | 17 | |
| | 18 | # This next bit is borrowed from PIL. It is used to wrap the text intelligently. |
| | 19 | fp = StringIO() |
| | 20 | data += " " # buffer for the +1 test |
| | 21 | c = i = 0 |
| | 22 | word = "" |
| | 23 | octdigits = "01234567" |
| | 24 | hexdigits = "0123456789abcdef" |
| | 25 | while i < len(data): |
| | 26 | if data[i] != "\\": |
| | 27 | word = data[i] |
| | 28 | i += 1 |
| | 29 | else: |
| | 30 | if data[i+1] in octdigits: |
| | 31 | for n in xrange(2, 5): |
| | 32 | if data[i+n] not in octdigits: |
| | 33 | break |
| | 34 | word = data[i:i+n] |
| | 35 | i += n |
| | 36 | elif data[i+1] == 'x': |
| | 37 | for n in xrange(2, 5): |
| | 38 | if data[i+n] not in hexdigits: |
| | 39 | break |
| | 40 | word = data[i:i+n] |
| | 41 | i += n |
| | 42 | else: |
| | 43 | word = data[i:i+2] |
| | 44 | i += 2 |
| | 45 | |
| | 46 | l = len(word) |
| | 47 | if c + l >= 78-1: |
| | 48 | fp.write("\\\n") |
| | 49 | c = 0 |
| | 50 | fp.write(word) |
| | 51 | c += l |
| | 52 | |
| | 53 | # return the formatted compressed data |
| | 54 | return fp.getvalue() |