-
Notifications
You must be signed in to change notification settings - Fork 0
/
po2json.py
executable file
·131 lines (108 loc) · 3.89 KB
/
po2json.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/python3.10
import json, sys
from os import path as ospath
class Po2Json():
def __init__(self, FILE, ODIR):
self.file = FILE
self.ofile = ospath.basename(FILE).split('.po')[0]+ '.json'
self.ofile = ospath.join(ODIR, self.ofile)
self.dex = {
'meta' :{},
'context' :{},
'singular' :{},
'plural' :{}
}
self.read_file()
# self.get_plural()
self.process_lines()
def read_file(self):
self.lines = []
try:
with open(self.file, '+r') as f:
self.lines = f.readlines()
except Exception as e:
print('exception:', e)
sys.exit(1)
def get_plural(self):
"""
This function is obsolete
"""
delimiter = 'Plural-Forms: nplurals='
for line in self.lines:
if delimiter in line:
# get first char after = in delimiter
self.n = line.split(delimiter)[1][0]
# remove last 5 char form end of string
self.dex['meta']['nplurals'] = self.n
self.dex['meta']['plural'] = line[line.rfind('plural=')+7:-5].strip()
break
return
def process_lines(self):
cplines = self.lines.copy()
cplines = cplines[cplines.index('\n')+1:] # remove metadata
cplines.append('#') # to simulate existance of a new msgid
group_type = 'singular' # as default
group_data = {}
msgid = ''
contextid = ''
def get_line_key_n_value(line):
i = line.index(' ')
key = line[:i].strip()
val = str(line[i+2:-2].strip())
return key, val
for line in cplines:
if line[0] == '#' or line == '\n':
# if first char from line is #
# or line is \n
if not group_data:
# group_data empty, do nothing
continue
if group_type == 'singular' or group_type == 'plural':
self.dex[group_type].update(group_data)
elif group_type == 'context':
self.dex[group_type][contextid] = group_data
group_data = {}
group_type = 'singular'
continue
key, val = get_line_key_n_value(line)
if 'msgctxt' in key:
group_type = 'context'
self.dex['context'][val] = ""
contextid = val
if 'msgid_plural' in key:
group_type = 'plural'
group_data[msgid] = []
continue
if 'msgid' in key and 'msgid_plural' not in key:
msgid = val
group_data[msgid] = val
if group_type != 'context':
group_type = 'singular'
continue
if group_type == 'singular' or group_type=='context':
if 'msgstr' in key:
group_data[msgid] = val
continue
if group_type == 'plural':
if 'msgstr[' in key:
group_data[msgid].append(val)
# print('******* DEX *******')
# print(json.dumps(self.dex,indent=4))
def save_json2file(self):
with open(self.ofile, 'w') as f:
f.write(json.dumps(self.dex, indent=4))
print(f"File {self.ofile} has been created")
if __name__ == "__main__":
try:
FILE = sys.argv[1]
ODIR = './' if len(sys.argv) == 2 else sys.argv[2]
except:
print('First argument: file.po')
print('Second argument: output dir, default: ./')
sys.exit()
if '.po' not in FILE:
print('File does not have extension .po')
sys.exit()
p2j = Po2Json(FILE, ODIR)
p2j.save_json2file()
print("Done")