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