2
#############################################################################
4
# MODULE: Languages information and statistics (Python)
5
# AUTHOR(S): Luca Delucchi <lucadeluge@gmail.com>
6
# Pietro Zambelli <peter.zamb@gmail.com>
7
# PURPOSE: Create a json file containing languages translations
8
# information and statistics.
9
# COPYRIGHT: (C) 2012 by the GRASS Development Team
11
# This program is free software under the GNU General
12
# Public License (>=v2). Read the file COPYING that
13
# comes with GRASS for details.
15
#############################################################################
23
def read_po_files(inputdirpath):
24
"""Return a dictionary with for each language the list of *.po files"""
25
originalpath = os.getcwd()
26
os.chdir(inputdirpath)
28
for pofile in glob.glob("*.po"):
29
lang = pofile.split('_')[1:]
30
# check if are two definitions like pt_br
32
lang = ['_'.join(lang)]
33
lang = lang[0].split('.')[0]
35
# if keys is not in languages add it and the file's name
36
if not languages.has_key(lang):
37
languages[lang] = [pofile,]
40
languages[lang].append(pofile)
41
os.chdir(originalpath)
45
def read_msgfmt_statistics(msg, lgood, lfuzzy, lbad):
46
"""Return a dictionary, and the good, fuzzy and bad values from a string"""
50
# TODO maybe check using regex
51
# check for each answer
54
# each answer is written into dictionary and
55
# the value add to variable for the sum
56
if 'untranslated' in o:
57
val = int(o.split(' ')[0])
61
val = int(o.split(' ')[0])
62
langdict['fuzzy'] = val
65
val = int(o.split(' ')[0])
66
langdict['good'] = val
68
return langdict, lgood, lfuzzy, lbad
70
def langDefinition(fil):
71
f = codecs.open(fil,encoding = 'utf-8', errors='replace', mode = 'r')
72
for l in f.readlines():
73
if '"Language-Team:' in l:
74
lang = l.split(' ')[1:-1]
84
def get_stats(languages, directory):
85
"""Return a dictionay with the statistic for each language"""
86
# output dictionary to transform in json file
88
# TO DO TOTALS OF ENGLISH WORD FOR EACH FILE
89
# all the total string in english
91
# all the information about each lang
94
for lang, pofilelist in languages.iteritems():
95
output['langs'][lang] = {}
96
output['langs'][lang]['total'] = {}
97
output['langs'][lang]['name'] = langDefinition(os.path.join(directory,pofilelist[0]))
98
# variables to create sum for each language
103
for flang in pofilelist:
105
fpref = flang.split('_')[0]
106
# run msgfmt for statistics
107
# TODO check if it's working on windows
108
process = subprocess.Popen(['msgfmt', '--statistics',
109
os.path.join(directory,flang)],
110
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
111
msg = process.communicate()[1].strip()
112
# check if some errors occurs
113
if msg.find('error') != -1:
114
# TODO CHECK IF grass.warning()
115
print "WARNING: file <%s> has some problems: <%s>" % (flang, msg)
117
output['langs'][lang][fpref], lgood, lfuzzy, lbad = \
118
read_msgfmt_statistics(msg, lgood, lfuzzy, lbad)
119
# write the sum and total of each file
120
output['langs'][lang]['total']['good'] = lgood
121
output['langs'][lang]['total']['fuzzy'] = lfuzzy
122
output['langs'][lang]['total']['bad'] = lbad
123
output['langs'][lang]['total']['total'] = lgood + lfuzzy + lbad
127
def writejson(stats, outfile):
128
# load dictionary into json format
129
fjson = json.dumps(stats, sort_keys=True, indent=4)
130
# write a string with pretty style
131
outjson = os.linesep.join([l.rstrip() for l in fjson.splitlines()])
133
fout = open(outfile,'w')
135
fout.write(os.linesep)
138
os.remove("messages.mo")
142
def main(in_dirpath, out_josonpath):
143
languages = read_po_files(in_dirpath)
144
stats = get_stats(languages, in_dirpath)
146
if os.path.exists(out_josonpath):
147
os.remove(out_josonpath)
148
writejson(stats, out_josonpath)
151
if __name__ == "__main__":
153
outfile = os.path.join(os.environ['GISBASE'],'translation_status.json')
154
sys.exit(main(directory, outfile))