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