Created
March 1, 2016 17:57
-
-
Save bennokr/1cd349b518ef69463624 to your computer and use it in GitHub Desktop.
Convert an Apple Binary Property List (bplist) to json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Convert an Apple Binary Property List (bplist) to json | |
""" | |
import ccl_bplist # https://github.com/cclgroupltd/ccl-bplist | |
from datetime import datetime | |
def clean_archive(d): | |
if type(d) in [dict, ccl_bplist.NsKeyedArchiverDictionary]: | |
return {k:clean_archive(v) for k,v in d.iteritems() if not k.startswith('$')} | |
elif type(d) == ccl_bplist.NsKeyedArchiverList: | |
return [clean_archive(i) for i in d] | |
else: | |
return d | |
def bplist_dict(fobj): | |
"""Convert a bplist file object to python dict""" | |
plist = ccl_bplist.load(fobj) | |
ccl_bplist.set_object_converter(ccl_bplist.NSKeyedArchiver_common_objects_convertor) | |
archive = ccl_bplist.deserialise_NsKeyedArchiver(plist) | |
return clean_archive(archive) | |
if __name__ == '__main__': | |
import argparse, sys, json | |
parser = argparse.ArgumentParser(description=__doc__) | |
parser.add_argument('--input', '-i', nargs='?', type=argparse.FileType('r'), | |
default=sys.stdin, help='default: stdin') | |
args = parser.parse_args() | |
class DateTimeEncoder(json.JSONEncoder): | |
def default(self, o): | |
if isinstance(o, datetime): | |
return o.isoformat() | |
return json.JSONEncoder.default(self, o) | |
print json.dumps(bplist_dict(args.input), cls=DateTimeEncoder, indent=4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment