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
63
64
65
66
67
68
|
import codecs, os, subprocess
SRC = [
'index',
'credits',
'prolog',
'intro',
'section_1',
'chapter_1',
'chapter_2',
'chapter_3',
'chapter_4',
'chapter_5',
'chapter_6',
'chapter_7',
'chapter_8',
'section_2',
'chapter_9',
'chapter_10',
'chapter_11',
'chapter_12',
'annex',
'credits_photo',
]
OUT = 'ceibal'
FILE_SEP = '^-^_^-^_^-^_^-^_^-^_^-^_^-^_^-^_'
path_in_po = os.path.join('tmp', '%s-fr.po' % OUT)
path_in_txt = os.path.join('tmp', '%s.txt' % OUT)
# split po in blocks to sort them
block_list = dict()
with codecs.open(path_in_po, 'rb', 'utf-8') as f:
# init block and key
block = ''
key = 0
for r in f.readlines():
if r.startswith('#'):
if ':' in r:
# add new block
block_list[key] = block
# reset block and key
block = ''
key = int(r.strip().split(':')[-1])
# add current row
block += r
# add last block
block_list[key] = block
# sort blocks and write in po
with codecs.open(path_in_po, 'wb', 'utf-8') as f:
keys = block_list.keys()
keys.sort()
for k in keys:
f.write(block_list[k])
subprocess.call('po2txt --encoding utf-8 %s %s' % (path_in_po, path_in_txt),
shell=True)
with codecs.open(path_in_txt, 'rb', 'utf-8') as file_txt:
for idx, rst in enumerate(file_txt.read().split(FILE_SEP)):
if idx == len(SRC):
break
path_dst = os.path.join('source', 'fr', '%s.rst' % SRC[idx])
with codecs.open(path_dst, 'wb', 'utf-8') as file_dst:
file_dst.write(rst.strip())
|