[damned-lies: 7/9] Isolate pogrep logic in stats.utils



commit 522f0eac56efc5ee9329496c3de23ea0d18acaf5
Author: Claude Paroz <claude 2xlibre net>
Date:   Tue Mar 15 20:37:07 2011 +0100

    Isolate pogrep logic in stats.utils

 README                        |    3 +++
 stats/models.py               |    8 +++-----
 stats/utils.py                |   21 +++++++++++++++++++++
 templates/release_detail.html |    2 +-
 4 files changed, 28 insertions(+), 6 deletions(-)
---
diff --git a/README b/README
index dd57505..41229b2 100644
--- a/README
+++ b/README
@@ -42,6 +42,9 @@ Requirements
 
 9 - [Optional] python-pyicu for correct sorting in various languages
 
+10- [Optional] translate-toolkit >= 1.9.0-beta2 (--keeptranslations option for
+    pogrep) for reduced po files.
+
 Installation
 ============
 
diff --git a/stats/models.py b/stats/models.py
index 2000c74..9b177dd 100644
--- a/stats/models.py
+++ b/stats/models.py
@@ -1341,6 +1341,8 @@ class Statistics(models.Model):
         self.full_po.save()
         if self.domain.dtype == "ui":
             def part_po_equals_full_po():
+                if self.part_po == self.full_po:
+                    return
                 if self.part_po and self.part_po != self.full_po:
                     self.part_po.delete()
                 self.part_po = self.full_po
@@ -1352,11 +1354,7 @@ class Statistics(models.Model):
                     part_po_path = self.full_po.path[:-3] + "reduced.pot"
                 else:
                     part_po_path = self.full_po.path[:-3] + ".reduced.po"
-                cmd = "pogrep --invert-match --header --search=locations \"gschema.xml.in\" %(full_po)s %(part_po)s" % {
-                    'full_po': self.full_po.path,
-                    'part_po': part_po_path,
-                }
-                utils.run_shell_command(cmd)
+                utils.pogrep(self.full_po.path, part_po_path)
                 part_stats = utils.po_file_stats(part_po_path, msgfmt_checks=False, count_images=False)
                 if part_stats['translated'] + part_stats['fuzzy'] + part_stats['untranslated'] == translated + fuzzy + untranslated:
                     # No possible gain, set part_po = full_po so it is possible to compute complete stats at database level
diff --git a/stats/utils.py b/stats/utils.py
index 5ac82ac..747524c 100644
--- a/stats/utils.py
+++ b/stats/utils.py
@@ -25,6 +25,12 @@ from itertools import islice
 from subprocess import Popen, PIPE
 import errno
 
+try:
+    from translate.tools import pogrep
+    has_toolkit = True
+except ImportError:
+    has_toolkit = False
+
 from django.utils.translation import ugettext_noop
 from django.contrib.sites.models import Site
 from django.core.mail import send_mail
@@ -83,6 +89,21 @@ def check_program_presence(prog_name):
     status, output, err = run_shell_command("which %s" % prog_name)
     return status == 0
 
+def pogrep(in_file, out_file):
+    if not has_toolkit:
+        return
+    grepfilter = pogrep.GrepFilter("gschema.xml.in", "locations", invertmatch=True, keeptranslations=True)
+    out = open(out_file, "w")
+    pogrep.rungrep(in_file, out, None, grepfilter)
+    out.close()
+    # command-line variant:
+    """
+    cmd = "pogrep --invert-match --header --search=locations \"gschema.xml.in\" %(full_po)s %(part_po)s" % {
+        'full_po': in_file,
+        'part_po': out_file,
+    }
+    run_shell_command(cmd)"""
+
 def check_potfiles(po_path):
     """Check if there were any problems regenerating a POT file (intltool-update -m).
        Return a list of errors """
diff --git a/templates/release_detail.html b/templates/release_detail.html
index 31eb487..4b7d3bc 100644
--- a/templates/release_detail.html
+++ b/templates/release_detail.html
@@ -42,7 +42,7 @@
       {% endwith %}
      </div>
   </td>
-  <td class="stats_numb"><a href="{% url language_release lstats.lang_locale release.name "ui" %}">
+  <td class="stats_numb"><a href="{% url language_release lstats.lang_locale release.name "ui-part" %}">
       {{ lstats.ui_percent_part }}% ({{ lstats.ui_trans_part }}/{{ lstats.ui_fuzzy_part }}/{{ lstats.ui_untrans_part }})</a>
   </td>
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]