forked from bids-standard/bids-matlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_schema.py
45 lines (31 loc) · 997 Bytes
/
convert_schema.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
"""
This script runs through the bids-schema YAML files of the BIDS-specification
and converts them to JSON.
Created by Remi Gau
"""
from ruamel.yaml import YAML
import json
import glob
import os
input_dir = "bids-specification/src/schema"
output_dir = "schema"
print("\n\nCONVERTING SCHEMA\n\n")
# create output directories
if not os.path.exists(output_dir):
os.makedirs(output_dir)
yaml = YAML(typ="safe")
# list all yaml files in an iterator
file_ls = glob.glob(os.path.join(input_dir, "**", "*.yaml"), recursive=True)
for in_file in file_ls:
print(in_file)
# create output directory and filename
path, fname = os.path.split(in_file)
path = path.replace(input_dir, output_dir)
if not os.path.exists(path):
os.makedirs(path)
out_file = os.path.join(path, fname.replace("yaml", "json"))
# convert to json
with open(in_file) as fpi:
data = yaml.load(fpi)
with open(out_file, "w") as fpo:
json.dump(data, fpo, indent=2)