]> git.lyx.org Git - lyx.git/blobdiff - po/postats.py
de.po
[lyx.git] / po / postats.py
old mode 100644 (file)
new mode 100755 (executable)
index 75925f6..9c85e5c
@@ -1,4 +1,4 @@
-#! /usr/bin/env python
+#! /usr/bin/python3
 # -*- coding: utf-8 -*-
 # Copyright (C) 2007 Michael Gerz <michael.gerz@teststep.org>
 # Copyright (C) 2007 José Matos <jamatos@lyx.org>
@@ -24,21 +24,26 @@ to retrieve the number of translated/fuzzy/untranslated messages,
 and generates a PHP web page.
 
 Invocation:
-   postats.py po_files > "pathToWebPages"/i18n.inc
+   postats.py lyx_version po_files > "pathToWebPages"/i18n.inc
 """
+from __future__ import print_function
 
-# modify this when you change version
-# Note that an empty lyx_branch variable (ie svn trunk)
-# will "do the right thing".
-lyx_version="1.6.0svn"
+# Modify this when you change branch (e.g. stats for stable branch).
+# Note that an empty lyx_branch variable will "do the right thing" for master.
 lyx_branch=""
+# these po-files will be skipped:
+ommitted = ('en.po')
 
 import os
 import sys
+import codecs
+import subprocess
+from subprocess import Popen, PIPE
 
 # Reset the locale
 import locale
-locale.setlocale(locale.LC_ALL, '') 
+locale.setlocale(locale.LC_ALL, 'C')
+os.environ['LC_ALL'] = 'C'
 
 def extract_number(line, issues, prop):
     """
@@ -66,7 +71,7 @@ def read_pofile(pofile):
     """ Read the header of the pofile and return it as a dictionary"""
     header = {}
     read_header = False
-    for line in open(pofile):
+    for line in codecs.open(pofile, encoding='utf8'):
         line = line[:-1]
         if line[:5] == 'msgid':
             if read_header:
@@ -91,7 +96,7 @@ def run_msgfmt(pofile):
  The function runs msgfmt on it and returns corresponding php code.
 """
     if not pofile.endswith('.po'):
-        print >> sys.stderr, "%s is not a po file" % pofile
+        print("%s is not a po file" % pofile, file=sys.stderr)
         sys.exit(1)
 
     dirname = os.path.dirname(pofile)
@@ -107,16 +112,14 @@ def run_msgfmt(pofile):
     prop["email"] = header['Last-Translator'].split('<')[1][:-1]
     prop["email"] = prop["email"].replace("@", " () ")
     prop["email"] = prop["email"].replace(".", " ! ")
-    translator = header['Last-Translator'].split('<')[0].strip()
-    try:
-        prop["translator"] = translator.decode(charset).encode('ascii','xmlcharrefreplace')
-    except LookupError:
-        prop["translator"] = translator
-
-    p_in, p_out = os.popen4("msgfmt --statistics -o %s %s" % (gmofile, pofile))
-    extract_number(p_out.readline(),
-                   ('translated', 'fuzzy', 'untranslated'),
-                   prop)
+    prop["translator"] = header['Last-Translator'].split('<')[0].strip()
+
+    msg = subprocess.check_output(["msgfmt", "--statistics",
+            "-o", gmofile, # FIXME: do we really want a gmofile as side-effect?
+            pofile], stderr=subprocess.STDOUT)
+    if sys.version_info[0] > 2:
+        msg = msg.decode('utf8')
+    extract_number(msg, ('translated', 'fuzzy', 'untranslated'), prop)
     return """
 array ( 'langcode' => '%(langcode)s', "date" => "%(date)s",
 "msg_tr" => %(translated)d, "msg_fu" => %(fuzzy)d, "msg_nt" => %(untranslated)d,
@@ -125,12 +128,12 @@ array ( 'langcode' => '%(langcode)s', "date" => "%(date)s",
 
 if __name__ == "__main__":
     if lyx_branch:
-        branch_tag = "branches/%s" % lyx_branch
+        branch_tag = lyx_branch
     else:
-        branch_tag = "trunk"
+        branch_tag = "master"
 
 
-    print """<?php
+    print("""<?php
 // The current version
 $lyx_version = "%s";
 // The branch tag
@@ -138,5 +141,6 @@ $branch_tag = "%s";
 
 // The data itself
 $podata = array (%s
-)?>
-""" % (lyx_version, branch_tag, ",".join([run_msgfmt(po) for po in sys.argv[1:]]))
+)?>""" % (sys.argv[1], branch_tag, 
+          ",".join([run_msgfmt(po) for po in sys.argv[2:] 
+                                   if po not in ommitted])))