mutagen 1.31 required
[wolnelektury.git] / scripts / make-locale-pack-json.py
1 #!/usr/bin/env python
2
3 import json
4 import sys
5 import os
6 import shutil
7
8 fname = sys.argv[1]
9 def mkdir(n):
10     if not os.path.exists(n): os.mkdir(n)
11
12 dst = os.path.dirname(fname)
13 name = os.path.basename(fname).split('.')[0]
14
15 dst2 = os.path.join(dst, name) 
16 mkdir(dst2)
17
18 data = json.load(open(fname))
19 for ip in data:
20     dst3 = os.path.join(dst2, ip['fields']['slug'])
21     mkdir(dst3)
22     for fld, val in ip['fields'].items():
23         if val is None:
24             val = ''
25         if filter(lambda x: fld.startswith(x),
26                   ['title', 'left_column', 'right_column']):
27             o = open(os.path.join(dst3, fld+".txt"),'w')
28             try:
29                 o.write(val.encode('utf-8'))
30             finally:
31                 o.close()
32