diff options
author | Ronald Oussoren <ronaldoussoren@mac.com> | 2013-11-21 14:46:49 (GMT) |
---|---|---|
committer | Ronald Oussoren <ronaldoussoren@mac.com> | 2013-11-21 14:46:49 (GMT) |
commit | c5cf7973422dce0ed59849aaf2d708d4c6b7320d (patch) | |
tree | 5d34eeec1f0af4bd2805464b305e18d27af63ca9 /Mac | |
parent | 8455723cfb0cdb0fc8d908210fa21b63b9d09a2b (diff) | |
download | cpython-c5cf7973422dce0ed59849aaf2d708d4c6b7320d.zip cpython-c5cf7973422dce0ed59849aaf2d708d4c6b7320d.tar.gz cpython-c5cf7973422dce0ed59849aaf2d708d4c6b7320d.tar.bz2 |
Issue #14455: plistlib now supports binary plists and has an updated API.
This patch adds support for binary plists on OSX to plistlib (based
on a patch by 'dpounces').
The patch also cleans up the API for the plistlib module.
Diffstat (limited to 'Mac')
-rw-r--r-- | Mac/Tools/plistlib_generate_testdata.py | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/Mac/Tools/plistlib_generate_testdata.py b/Mac/Tools/plistlib_generate_testdata.py new file mode 100644 index 0000000..da1d976 --- /dev/null +++ b/Mac/Tools/plistlib_generate_testdata.py @@ -0,0 +1,94 @@ +#!/usr/bin/env python3 + +from Cocoa import NSMutableDictionary, NSMutableArray, NSString, NSDate +from Cocoa import NSPropertyListSerialization, NSPropertyListOpenStepFormat +from Cocoa import NSPropertyListXMLFormat_v1_0, NSPropertyListBinaryFormat_v1_0 +from Cocoa import CFUUIDCreateFromString, NSNull, NSUUID, CFPropertyListCreateData +from Cocoa import NSURL + +import datetime +from collections import OrderedDict +import binascii + +FORMATS=[ +# ('openstep', NSPropertyListOpenStepFormat), + ('plistlib.FMT_XML', NSPropertyListXMLFormat_v1_0), + ('plistlib.FMT_BINARY', NSPropertyListBinaryFormat_v1_0), +] + +def nsstr(value): + return NSString.alloc().initWithString_(value) + + +def main(): + pl = OrderedDict() + + seconds = datetime.datetime(2004, 10, 26, 10, 33, 33, tzinfo=datetime.timezone(datetime.timedelta(0))).timestamp() + pl[nsstr('aDate')] = NSDate.dateWithTimeIntervalSince1970_(seconds) + + pl[nsstr('aDict')] = d = OrderedDict() + d[nsstr('aFalseValue')] = False + d[nsstr('aTrueValue')] = True + d[nsstr('aUnicodeValue')] = "M\xe4ssig, Ma\xdf" + d[nsstr('anotherString')] = "<hello & 'hi' there!>" + d[nsstr('deeperDict')] = dd = OrderedDict() + dd[nsstr('a')] = 17 + dd[nsstr('b')] = 32.5 + dd[nsstr('c')] = a = NSMutableArray.alloc().init() + a.append(1) + a.append(2) + a.append(nsstr('text')) + + pl[nsstr('aFloat')] = 0.5 + + pl[nsstr('aList')] = a = NSMutableArray.alloc().init() + a.append(nsstr('A')) + a.append(nsstr('B')) + a.append(12) + a.append(32.5) + aa = NSMutableArray.alloc().init() + a.append(aa) + aa.append(1) + aa.append(2) + aa.append(3) + + pl[nsstr('aString')] = nsstr('Doodah') + + pl[nsstr('anEmptyDict')] = NSMutableDictionary.alloc().init() + + pl[nsstr('anEmptyList')] = NSMutableArray.alloc().init() + + pl[nsstr('anInt')] = 728 + + pl[nsstr('nestedData')] = a = NSMutableArray.alloc().init() + a.append(b'''<lots of binary gunk>\x00\x01\x02\x03<lots of binary gunk>\x00\x01\x02\x03<lots of binary gunk>\x00\x01\x02\x03<lots of binary gunk>\x00\x01\x02\x03<lots of binary gunk>\x00\x01\x02\x03<lots of binary gunk>\x00\x01\x02\x03<lots of binary gunk>\x00\x01\x02\x03<lots of binary gunk>\x00\x01\x02\x03<lots of binary gunk>\x00\x01\x02\x03<lots of binary gunk>\x00\x01\x02\x03''') + + + pl[nsstr('someData')] = b'<binary gunk>' + + pl[nsstr('someMoreData')] = b'''<lots of binary gunk>\x00\x01\x02\x03<lots of binary gunk>\x00\x01\x02\x03<lots of binary gunk>\x00\x01\x02\x03<lots of binary gunk>\x00\x01\x02\x03<lots of binary gunk>\x00\x01\x02\x03<lots of binary gunk>\x00\x01\x02\x03<lots of binary gunk>\x00\x01\x02\x03<lots of binary gunk>\x00\x01\x02\x03<lots of binary gunk>\x00\x01\x02\x03<lots of binary gunk>\x00\x01\x02\x03''' + + pl[nsstr('\xc5benraa')] = nsstr("That was a unicode key.") + + print("TESTDATA={") + for fmt_name, fmt_key in FORMATS: + data, error = NSPropertyListSerialization.dataWithPropertyList_format_options_error_( + pl, fmt_key, 0, None) + if data is None: + print("Cannot serialize", fmt_name, error) + + else: + print(" %s: binascii.a2b_base64(b'''\n %s'''),"%(fmt_name, _encode_base64(bytes(data)).decode('ascii')[:-1])) + + print("}") + print() + +def _encode_base64(s, maxlinelength=60): + maxbinsize = (maxlinelength//4)*3 + pieces = [] + for i in range(0, len(s), maxbinsize): + chunk = s[i : i + maxbinsize] + pieces.append(binascii.b2a_base64(chunk)) + return b' '.join(pieces) + +main() |