forked from grahampugh/plist-yaml-plist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yaml_plist.py
executable file
·63 lines (47 loc) · 1.56 KB
/
yaml_plist.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
#!/usr/bin/env python
"""
If this script is run directly, it takes an input file and an output file from
the command lineThe input file must be in YAML format.
The output file will be in PLIST format:
yaml_plist.py <input-file> <output-file>
The output file can be omitted, so long as the input file ends with .yaml.
In this case, the name of the output file is
taken from the input file, with .yaml removed from the end.
For best results, the input file should therefore be named with
"""
import sys
import yaml
from os import path
from plistlib import writePlistToString
def convert(yaml):
"""Do the conversion"""
lines = writePlistToString(yaml).splitlines()
lines.append('')
return "\n".join(lines)
def yaml_plist(in_path, out_path):
"""Convert yaml to plist"""
in_file = open(in_path, 'r')
out_file = open(out_path, 'w')
input = yaml.safe_load(in_file)
output = convert(input)
out_file.writelines(output)
def main():
"""Get the command line inputs if running this script directly"""
if len(sys.argv) < 2:
print("Usage: yaml_plist.py <input-file> <output-file>")
sys.exit(1)
in_path = sys.argv[1]
try:
sys.argv[2]
except Exception as e:
if in_path.endswith('.yaml'):
filename, file_extension = path.splitext(in_path)
out_path = filename
else:
print("Usage: yaml_plist.py <input-file> <output-file>")
sys.exit(1)
else:
out_path = sys.argv[2]
yaml_plist(in_path, out_path)
if __name__ == '__main__':
main()