Path: blob/main/Mac/Tools/plistlib_generate_testdata.py
12 views
#!/usr/bin/env python312from Cocoa import NSMutableDictionary, NSMutableArray, NSString, NSDate, NSNumber3from Cocoa import NSPropertyListSerialization, NSPropertyListOpenStepFormat4from Cocoa import NSPropertyListXMLFormat_v1_0, NSPropertyListBinaryFormat_v1_05from Cocoa import CFUUIDCreateFromString, NSNull, NSUUID, CFPropertyListCreateData6from Cocoa import NSURL7from Cocoa import NSKeyedArchiver89import datetime10from collections import OrderedDict11import binascii1213FORMATS=[14# ('openstep', NSPropertyListOpenStepFormat),15('plistlib.FMT_XML', NSPropertyListXMLFormat_v1_0),16('plistlib.FMT_BINARY', NSPropertyListBinaryFormat_v1_0),17]1819def nsstr(value):20return NSString.alloc().initWithString_(value)212223def main():24pl = OrderedDict()2526# Note: pl is an OrderedDict to control the order27# of keys, and hence have some control on the structure28# of the output file.29# New keys should be added in alphabetical order.3031seconds = datetime.datetime(2004, 10, 26, 10, 33, 33, tzinfo=datetime.timezone(datetime.timedelta(0))).timestamp()32pl[nsstr('aBigInt')] = 2 ** 63 - 4433pl[nsstr('aBigInt2')] = NSNumber.numberWithUnsignedLongLong_(2 ** 63 + 44)34pl[nsstr('aDate')] = NSDate.dateWithTimeIntervalSince1970_(seconds)3536pl[nsstr('aDict')] = d = OrderedDict()37d[nsstr('aFalseValue')] = False38d[nsstr('aTrueValue')] = True39d[nsstr('aUnicodeValue')] = "M\xe4ssig, Ma\xdf"40d[nsstr('anotherString')] = "<hello & 'hi' there!>"41d[nsstr('deeperDict')] = dd = OrderedDict()42dd[nsstr('a')] = 1743dd[nsstr('b')] = 32.544dd[nsstr('c')] = a = NSMutableArray.alloc().init()45a.append(1)46a.append(2)47a.append(nsstr('text'))4849pl[nsstr('aFloat')] = 0.55051pl[nsstr('aList')] = a = NSMutableArray.alloc().init()52a.append(nsstr('A'))53a.append(nsstr('B'))54a.append(12)55a.append(32.5)56aa = NSMutableArray.alloc().init()57a.append(aa)58aa.append(1)59aa.append(2)60aa.append(3)6162pl[nsstr('aNegativeBigInt')] = -8000000000063pl[nsstr('aNegativeInt')] = -564pl[nsstr('aString')] = nsstr('Doodah')6566pl[nsstr('anEmptyDict')] = NSMutableDictionary.alloc().init()6768pl[nsstr('anEmptyList')] = NSMutableArray.alloc().init()6970pl[nsstr('anInt')] = 7287172pl[nsstr('nestedData')] = a = NSMutableArray.alloc().init()73a.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''')747576pl[nsstr('someData')] = b'<binary gunk>'7778pl[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'''7980pl[nsstr('\xc5benraa')] = nsstr("That was a unicode key.")8182print("TESTDATA={")83for fmt_name, fmt_key in FORMATS:84data, error = NSPropertyListSerialization.dataWithPropertyList_format_options_error_(85pl, fmt_key, 0, None)86if data is None:87print("Cannot serialize", fmt_name, error)8889else:90print(" %s: binascii.a2b_base64(b'''\n %s'''),"%(fmt_name, _encode_base64(bytes(data)).decode('ascii')[:-1]))9192keyed_archive_data = NSKeyedArchiver.archivedDataWithRootObject_("KeyArchive UID Test")93print(" 'KEYED_ARCHIVE': binascii.a2b_base64(b'''\n %s''')," % (_encode_base64(bytes(keyed_archive_data)).decode('ascii')[:-1]))94print("}")95print()9697def _encode_base64(s, maxlinelength=60):98maxbinsize = (maxlinelength//4)*399pieces = []100for i in range(0, len(s), maxbinsize):101chunk = s[i : i + maxbinsize]102pieces.append(binascii.b2a_base64(chunk))103return b' '.join(pieces)104105main()106107108