Code: Select all
#!/usr/bin/python
# Convert plist from XML to NextStep format
# 'with' and 'str.format()' require python 2.6
from plistlib import readPlist
filename = 'shipyard.plist'
newsuffix = '-ns'
def writeDict(fs, dct, lvl):
fs.write('{\n')
for k, v in sorted(dct.iteritems()):
fs.write((lvl+1)*'\t'+'"{key}" = '.format(key=k))
if isinstance(v, list):
writeList(fs, v, lvl+1)
elif isinstance(v, dict):
writeDict(fs, v, lvl+1)
else:
q = '"' if isinstance(v, str) else ''
fs.write('{quote}{val}{quote}'.format(quote=q,
val=v if not isinstance(v, bool) else 'yes' if v else 'no'))
fs.write(';\n')
fs.write(lvl*'\t'+'}')
def writeList(fs, lst, lvl):
fs.write('(\n')
for i in lst:
fs.write((lvl+1)*'\t')
if isinstance(i, list):
writeList(fs, i, lvl+1)
elif isinstance(i, dict):
writeDict(fs, i, lvl+1)
else:
q = '"' if isinstance(i, str) else ''
fs.write('{quote}{item}{quote}'.format(quote=q, item=i))
fs.write(',\n')
fs.write(lvl*'\t'+')')
pl = readPlist(filename)
level = 0
with open(filename + newsuffix, 'w') as f:
if isinstance(pl, list):
writeList(f, pl, level)
elif isinstance(pl, dict):
writeDict(f, pl, level)
else:
f.write('// Parsing error!')
f.write('\n')