forked from grahampugh/plist-yaml-plist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plist_yaml.py
executable file
·76 lines (57 loc) · 1.85 KB
/
plist_yaml.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python
"""
If this script is run directly, it takes an input file and an output file from
the command line. The input file must be in PLIST format.
The output file will be in YAML format:
plist_yaml.py <input-file> <output-file>
The output file can be omitted. In this case, the name of the output file is
taken from the input file, with .yaml added to the end.
"""
import sys
import yaml
from os import path
from plistlib import readPlist, Data
def normalize_types(input):
"""
This allows YAML and JSON to store Data fields as strings. However, this
operation is irreversible. Only use if read-only access to the plist is
required.
"""
if isinstance(input, Data): return input.data
if isinstance(input, list):
retval = []
for child in input:
retval.append(normalize_types(child))
return retval
if isinstance(input, dict):
retval = {}
for key, child in input.iteritems():
retval[key] = normalize_types(child)
return retval
return input
def convert(xml):
"""Do the conversion"""
return yaml.dump(xml, width=float('inf'), default_flow_style=False)
def plist_yaml(in_path, out_path):
"""Convert plist to yaml"""
in_file = open(in_path, 'r')
input = readPlist(in_file)
normalized = normalize_types(input)
output = convert(normalized)
out_file = open(out_path, 'w')
out_file.writelines(output)
def main():
"""Get the command line inputs if running this script directly"""
if len(sys.argv) < 2:
print("Usage: plist_yaml.py <input-file> <output-file>")
sys.exit(1)
in_path = sys.argv[1]
try:
sys.argv[2]
except Exception as e:
out_path = "%s.yaml" % in_path
else:
out_path = sys.argv[2]
plist_yaml(in_path, out_path)
if __name__ == '__main__':
main()