| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
from Cocoa import NSMutableDictionary, NSMutableArray, NSString, NSDate, NSNumber |
| 4 |
from Cocoa import NSPropertyListSerialization, NSPropertyListOpenStepFormat |
| 5 |
from Cocoa import NSPropertyListXMLFormat_v1_0, NSPropertyListBinaryFormat_v1_0 |
| 6 |
from Cocoa import CFUUIDCreateFromString, NSNull, NSUUID, CFPropertyListCreateData |
| 7 |
from Cocoa import NSURL |
| 8 |
from Cocoa import NSKeyedArchiver |
| 9 |
|
| 10 |
import datetime |
| 11 |
from collections import OrderedDict |
| 12 |
import binascii |
| 13 |
|
| 14 |
FORMATS=[ |
| 15 |
# ('openstep', NSPropertyListOpenStepFormat), |
| 16 |
('plistlib.FMT_XML', NSPropertyListXMLFormat_v1_0), |
| 17 |
('plistlib.FMT_BINARY', NSPropertyListBinaryFormat_v1_0), |
| 18 |
] |
| 19 |
|
| 20 |
def nsstr(value): |
| 21 |
return NSString.alloc().initWithString_(value) |
| 22 |
|
| 23 |
|
| 24 |
def main(): |
| 25 |
pl = OrderedDict() |
| 26 |
|
| 27 |
# Note: pl is an OrderedDict to control the order |
| 28 |
# of keys, and hence have some control on the structure |
| 29 |
# of the output file. |
| 30 |
# New keys should be added in alphabetical order. |
| 31 |
|
| 32 |
seconds = datetime.datetime(2004, 10, 26, 10, 33, 33, tzinfo=datetime.timezone(datetime.timedelta(0))).timestamp() |
| 33 |
pl[nsstr('aBigInt')] = 2 ** 63 - 44 |
| 34 |
pl[nsstr('aBigInt2')] = NSNumber.numberWithUnsignedLongLong_(2 ** 63 + 44) |
| 35 |
pl[nsstr('aDate')] = NSDate.dateWithTimeIntervalSince1970_(seconds) |
| 36 |
|
| 37 |
pl[nsstr('aDict')] = d = OrderedDict() |
| 38 |
d[nsstr('aFalseValue')] = False |
| 39 |
d[nsstr('aTrueValue')] = True |
| 40 |
d[nsstr('aUnicodeValue')] = "M\xe4ssig, Ma\xdf" |
| 41 |
d[nsstr('anotherString')] = "<hello & 'hi' there!>" |
| 42 |
d[nsstr('deeperDict')] = dd = OrderedDict() |
| 43 |
dd[nsstr('a')] = 17 |
| 44 |
dd[nsstr('b')] = 32.5 |
| 45 |
dd[nsstr('c')] = a = NSMutableArray.alloc().init() |
| 46 |
a.append(1) |
| 47 |
a.append(2) |
| 48 |
a.append(nsstr('text')) |
| 49 |
|
| 50 |
pl[nsstr('aFloat')] = 0.5 |
| 51 |
|
| 52 |
pl[nsstr('aList')] = a = NSMutableArray.alloc().init() |
| 53 |
a.append(nsstr('A')) |
| 54 |
a.append(nsstr('B')) |
| 55 |
a.append(12) |
| 56 |
a.append(32.5) |
| 57 |
aa = NSMutableArray.alloc().init() |
| 58 |
a.append(aa) |
| 59 |
aa.append(1) |
| 60 |
aa.append(2) |
| 61 |
aa.append(3) |
| 62 |
|
| 63 |
pl[nsstr('aNegativeBigInt')] = -80000000000 |
| 64 |
pl[nsstr('aNegativeInt')] = -5 |
| 65 |
pl[nsstr('aString')] = nsstr('Doodah') |
| 66 |
|
| 67 |
pl[nsstr('anEmptyDict')] = NSMutableDictionary.alloc().init() |
| 68 |
|
| 69 |
pl[nsstr('anEmptyList')] = NSMutableArray.alloc().init() |
| 70 |
|
| 71 |
pl[nsstr('anInt')] = 728 |
| 72 |
|
| 73 |
pl[nsstr('nestedData')] = a = NSMutableArray.alloc().init() |
| 74 |
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''') |
| 75 |
|
| 76 |
|
| 77 |
pl[nsstr('someData')] = b'<binary gunk>' |
| 78 |
|
| 79 |
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''' |
| 80 |
|
| 81 |
pl[nsstr('\xc5benraa')] = nsstr("That was a unicode key.") |
| 82 |
|
| 83 |
print("TESTDATA={") |
| 84 |
for fmt_name, fmt_key in FORMATS: |
| 85 |
data, error = NSPropertyListSerialization.dataWithPropertyList_format_options_error_( |
| 86 |
pl, fmt_key, 0, None) |
| 87 |
if data is None: |
| 88 |
print("Cannot serialize", fmt_name, error) |
| 89 |
|
| 90 |
else: |
| 91 |
print(" %s: binascii.a2b_base64(b'''\n %s'''),"%(fmt_name, _encode_base64(bytes(data)).decode('ascii')[:-1])) |
| 92 |
|
| 93 |
keyed_archive_data = NSKeyedArchiver.archivedDataWithRootObject_("KeyArchive UID Test") |
| 94 |
print(" 'KEYED_ARCHIVE': binascii.a2b_base64(b'''\n %s''')," % (_encode_base64(bytes(keyed_archive_data)).decode('ascii')[:-1])) |
| 95 |
print("}") |
| 96 |
print() |
| 97 |
|
| 98 |
def _encode_base64(s, maxlinelength=60): |
| 99 |
maxbinsize = (maxlinelength//4)*3 |
| 100 |
pieces = [] |
| 101 |
for i in range(0, len(s), maxbinsize): |
| 102 |
chunk = s[i : i + maxbinsize] |
| 103 |
pieces.append(binascii.b2a_base64(chunk)) |
| 104 |
return b' '.join(pieces) |
| 105 |
|
| 106 |
main() |