-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnestedfacts.py
executable file
·62 lines (46 loc) · 1.5 KB
/
nestedfacts.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
#!/usr/bin/env python
import os
import os.path
import json
import sys
import yaml
def _load_yml_filedir(path):
"""
Internal function. Do not use.
Loads all YML-files from the given directory, recursively.
"""
YML_FILE_SUFFIX = '.yml'
bpath = os.path.basename(path)
if os.path.isdir(path):
result = {}
for entry in os.listdir(path):
epath = os.path.join(path, entry)
key, value = _load_yml_filedir(epath)
if not key:
continue
result[key] = value
return bpath, result
elif os.path.isfile(path):
if os.path.abspath(path) == os.path.abspath(sys.argv[0]):
return None, None # ignore script itself
if path.endswith(YML_FILE_SUFFIX):
bpath = bpath[:-len(YML_FILE_SUFFIX)]
try:
return bpath, yaml.safe_load(open(path))
except:
return bpath, None
else:
return None, None
else: # not a normal file or not existant
return None, None
def load_yml_filedir(root_dir):
""" load the given directory and return the data as a dict """
data = _load_yml_filedir(root_dir)[1]
return data
def dump_yml_filedir(root_dir):
""" load the given directory and print the data as formatted json """
result = load_yml_filedir(root_dir)
json.dump(result, sys.stdout, indent=2)
if __name__ == "__main__":
root_dir = sys.argv[1] if len(sys.argv) > 1 else '.'
dump_yml_filedir(root_dir)