1#!/usr/bin/python3
2"""
3Copyright (C) 2015 Johan Mattsson
4License: LGPL
5"""
6
7import glob
8from optparse import OptionParser
9
10try:
11    from scripts.run import run
12except ImportError:
13    from run import run
14
15def completeness (pofile):
16    """ Returns the completeness of the translation in percent """
17    with open (pofile, 'r', encoding='utf-8') as f:
18        content = f.readlines ()
19
20    translated = 0.0
21    untranslated = 0.0
22    msg = ""
23    original = ""
24    for line in content:
25        if line.startswith ("#"):
26            continue
27        if line.strip () == "":
28            continue
29        if line.startswith ("msgid"):
30            if not original == "msgstr \"\"":
31                if msg.strip () == "" and not original.strip () == "":
32                    untranslated = untranslated + 1
33                else:
34                    translated = translated + 1
35            original = line
36            msg = ""
37        if line.startswith ("msgstr") or line.startswith ("\""):
38            msg += line.replace ("msgstr", "").replace ("\"", "")
39
40    if msg == "" and not original == "":
41        untranslated = untranslated + 1
42    else:
43        translated = translated + 1
44
45    total = translated + untranslated
46
47    if total == 0:
48        return 0
49
50    return (translated / total) * 100;
51
52
53parser = OptionParser()
54parser.add_option("-t", "--threshold", dest="threshold", help="completeness threshold in percens", metavar="THRESHOLD")
55parser.add_option("-i", "--incomplete", dest="incomplete", action="store_true", default=False, help="move incomplete translations to the folder for incomplete translations", metavar="MOVE_INCOMPLETE")
56parser.add_option("-r", "--remove-compiled", dest="compiled", action="store_true", default=False, help="remove compiled incomplete translations", metavar="MOVE_COMPILED_INCOMPLETE")
57(options, args) = parser.parse_args()
58
59if not options.threshold:
60    for pofile in glob.glob('po/*.po'):
61        completed = completeness (pofile)
62        print (pofile + " " + str (completed) + "%")
63elif options.compiled:
64    for pofile in glob.glob('po/*.po'):
65        completed = completeness (pofile)
66        podir = pofile.replace ("po/", "")
67        podir = podir.replace (".po", "")
68        podir = podir.replace ("\\", "/")
69        if completed >= float (options.threshold):
70            print ("Releasing " + podir)
71        else:
72            print ("Removing incomplete translation " + podir)
73            run ("mkdir -p build/incomplete")
74            run ("rm -r build/locale/" + podir)
75elif options.incomplete:
76    for pofile in glob.glob('po/*.po'):
77        completed = completeness (pofile)
78        if completed >= float (options.threshold):
79            print ("Releasing " + pofile)
80        else:
81            print ("Moving incomplete translation " + pofile + " to po/incomplete/")
82            run ("mkdir -p po/incomplete")
83            run ("mv " + pofile + " po/incomplete/")
84else:
85    for pofile in glob.glob('po/*.po'):
86        completed = completeness (pofile)
87        if completed >= float (options.threshold):
88            print (pofile)
89