e3468cd530ea1be35970a5e4670c907bb5c7ff33
[naja.git] / tools / gen_json.py
1 #!/usr/bin/env python
2
3 import json
4 import os
5
6
7 def main(update=True):
8     data = os.path.join(os.path.dirname(__file__), '..', 'data')
9
10     deck_dir = os.path.join(data, 'location_decks')
11     convert_to_json(deck_dir, update)
12     puzzle_dir = os.path.join(deck_dir, 'puzzles')
13     convert_to_json(puzzle_dir, update)
14
15
16 def convert_to_json(directory, update=True):
17     for yaml_fn in os.listdir(directory):
18         basename, extension = os.path.splitext(yaml_fn)
19         if extension != '.yaml':
20             continue
21         json_fn = basename + '.json'
22
23         yaml_path = os.path.join(directory, yaml_fn)
24         json_path = os.path.join(directory, json_fn)
25
26         if not update and os.path.exists(json_path):
27             continue
28
29         import yaml
30         with open(yaml_path) as yaml_f:
31             obj = yaml.safe_load(yaml_f)
32         with open(json_path, 'w') as json_f:
33             json.dump(obj, json_f, indent=2)
34
35
36 if __name__ == '__main__':
37     main()