filter by tags
[wolnelektury.git] / scripts / save-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 try:
10     lang = sys.argv[2]
11 except IndexError:
12     lang = None
13
14 def mkdir(n):
15     if not os.path.exists(n): os.mkdir(n)
16
17 dst = os.path.dirname(fname)
18 name = os.path.basename(fname).split('.')[0]
19
20 dst2 = os.path.join(dst, name) 
21
22 data = json.load(open(fname))
23 for ip in data:
24     dst3 = os.path.join(dst2, ip['fields']['slug'])
25     for fld in ip['fields'].keys():
26         if filter(lambda x: fld.startswith(x),
27                   ['title', 'left_column', 'right_column']):
28             if lang and not fld.endswith("_"+lang):
29                 continue
30             try:
31                 o = open(os.path.join(dst3, fld+".txt"), 'r')
32                 try:
33                     print dst3,fld
34                     txt = o.read()
35                     try:
36                         txt = txt.decode('utf-8')
37                     except UnicodeError:
38                         txt = txt.decode('utf-16')
39                         
40                     ip['fields'][fld] = txt
41
42                 finally:
43                     o.close()
44             except IOError:
45                 print "no infopages file for field %s" % fld
46
47
48 out = open(fname, 'w')
49 try:
50     out.write(json.dumps(data,indent=4))
51 finally:
52     out.close()