-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuyaml
executable file
·44 lines (35 loc) · 969 Bytes
/
uyaml
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
#!/usr/bin/env python
"""
A YAML parser for the shell.
"""
import argparse
import sys
try:
import yaml
except ImportError:
sys.stderr.write("PyYAML module missing (http://pyyaml.org)!")
sys.exit(1)
def parse_args():
parser = argparse.ArgumentParser(description="YAML parser")
parser.add_argument("yaml", nargs=1, help="YAML file to parse")
parser.add_argument("path", nargs=1, help="Path to parse from file")
args = parser.parse_args(sys.argv[1:])
path = args.path[0].strip('/').split('/')
return (args.yaml[0], path)
def serialize(obj):
if isinstance(obj, list):
return " ".join(obj)
elif isinstance(obj, bool):
return int(obj)
else:
return obj
def main():
(yfile, ypath) = parse_args()
with open(yfile) as f:
data = yaml.load(f)
obj = data
for path in ypath:
obj = obj[path]
print serialize(obj)
if __name__ == '__main__':
main()