Created
November 2, 2016 18:07
-
-
Save jessepeterson/3467c5cf761a58848a7278586c667367 to your computer and use it in GitHub Desktop.
convert Apple plist to set of golang structs
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
import sys | |
import plistlib | |
pl = plistlib.readPlist(sys.argv[1]) | |
structs = [] | |
def struct_from_dict(name, vals): | |
global structs | |
ststr = "type %s struct {\n" % name | |
for key in vals.keys(): | |
camel_case_name = ''.join([x.capitalize() for x in key.split('_')]) | |
newname = name + camel_case_name | |
if isinstance(vals[key], dict): | |
ststr += " %s %s `plist:\"%s\"`\n" % (camel_case_name, newname, key) | |
struct_from_dict(newname, vals[key]) | |
# print vals[key] | |
elif isinstance(vals[key], str): | |
ststr += " %s string `plist:\"%s\"`\n" % (camel_case_name, key) | |
elif isinstance(vals[key], list): | |
if not len(vals[key]): | |
# can't really determine too much info here | |
ststr += " %s []interface{} `plist:\"%s\"`\n" % (camel_case_name, key) | |
continue | |
fe = vals[key][0] | |
if isinstance(fe, dict): | |
ststr += " %s []%s `plist:\"%s\"`\n" % (camel_case_name, newname, key) | |
struct_from_dict(newname, fe) | |
if isinstance(fe, str): | |
ststr += " %s []string `plist:\"%s\"`\n" % (camel_case_name, key) | |
ststr += "}\n" | |
structs.append(ststr) | |
struct_from_dict('plistRoot', pl) | |
for s in structs: | |
print s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment