~ubuntu-branches/ubuntu/vivid/grass/vivid-proposed

« back to all changes in this revision

Viewing changes to locale/grass_po_stats.py

  • Committer: Package Import Robot
  • Author(s): Bas Couwenberg
  • Date: 2015-02-20 23:12:08 UTC
  • mfrom: (8.2.6 experimental)
  • Revision ID: package-import@ubuntu.com-20150220231208-1u6qvqm84v430b10
Tags: 7.0.0-1~exp1
* New upstream release.
* Update python-ctypes-ternary.patch to use if/else instead of and/or.
* Drop check4dev patch, rely on upstream check.
* Add build dependency on libpq-dev to grass-dev for libpq-fe.h.
* Drop patches applied upstream, refresh remaining patches.
* Update symlinks for images switched from jpg to png.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
#############################################################################
 
3
#
 
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
 
10
#
 
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.
 
14
#
 
15
#############################################################################
 
16
 
 
17
import os, sys
 
18
import subprocess
 
19
import json
 
20
import glob
 
21
import codecs
 
22
 
 
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)
 
27
    languages = {}
 
28
    for pofile in glob.glob("*.po"):
 
29
        lang = pofile.split('_')[1:]
 
30
        # check if are two definitions like pt_br
 
31
        if len(lang) == 2:
 
32
            lang = ['_'.join(lang)]
 
33
        lang = lang[0].split('.')[0]
 
34
 
 
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,]
 
38
        # add only files name
 
39
        else:
 
40
            languages[lang].append(pofile)
 
41
    os.chdir(originalpath)
 
42
    return languages
 
43
 
 
44
 
 
45
def read_msgfmt_statistics(msg, lgood, lfuzzy, lbad):
 
46
    """Return a dictionary, and the good, fuzzy and bad values from a string"""
 
47
    langdict = {}
 
48
    # split the output
 
49
    out = msg.split(',')
 
50
    # TODO maybe check using regex
 
51
    # check for each answer
 
52
    for o in out:
 
53
        o = o.lower().strip()
 
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])
 
58
            langdict['bad'] = val
 
59
            lbad += val
 
60
        elif 'fuzzy' in o:
 
61
            val = int(o.split(' ')[0])
 
62
            langdict['fuzzy'] = val
 
63
            lfuzzy += val
 
64
        else:
 
65
            val = int(o.split(' ')[0])
 
66
            langdict['good'] = val
 
67
            lgood += val
 
68
    return langdict, lgood, lfuzzy, lbad
 
69
 
 
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]
 
75
            break
 
76
    f.close()
 
77
    if len(lang) == 2:
 
78
        return " ".join(lang)
 
79
    elif len(lang) == 1:
 
80
        return lang[0]
 
81
    else:
 
82
        return ""
 
83
 
 
84
def get_stats(languages, directory):
 
85
    """Return a dictionay with the statistic for each language"""
 
86
    # output dictionary to transform in json file
 
87
    output = {}
 
88
    # TO DO TOTALS OF ENGLISH WORD FOR EACH FILE
 
89
    # all the total string in english
 
90
    output['totals'] = {}
 
91
    # all the information about each lang
 
92
    output['langs'] = {}
 
93
    # for each language
 
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
 
99
        lgood = 0
 
100
        lfuzzy = 0
 
101
        lbad = 0
 
102
        # for each file
 
103
        for flang in pofilelist:
 
104
 
 
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)
 
116
                continue
 
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
 
124
    return output
 
125
 
 
126
 
 
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()])
 
132
    # write out file
 
133
    fout = open(outfile,'w')
 
134
    fout.write(outjson)
 
135
    fout.write(os.linesep)
 
136
    fout.close()
 
137
    try:
 
138
        os.remove("messages.mo")
 
139
    except:
 
140
        pass
 
141
 
 
142
def main(in_dirpath, out_josonpath):
 
143
    languages = read_po_files(in_dirpath)
 
144
    stats = get_stats(languages, in_dirpath)
 
145
 
 
146
    if os.path.exists(out_josonpath):
 
147
        os.remove(out_josonpath)
 
148
    writejson(stats, out_josonpath)
 
149
 
 
150
 
 
151
if __name__ == "__main__":
 
152
    directory = 'po/'
 
153
    outfile = os.path.join(os.environ['GISBASE'],'translation_status.json')
 
154
    sys.exit(main(directory, outfile))