[damned-lies] Take into account the msgctxt extra line generated by itstool



commit 01234d5c45351207b00d3185d9bb4947c814537b
Author: Claude Paroz <claude 2xlibre net>
Date:   Thu Aug 4 14:27:12 2011 +0200

    Take into account the msgctxt extra line generated by itstool

 stats/models.py                                 |   11 ++--
 stats/tests/__init__.py                         |    6 ++-
 stats/tests/help_mallard/gnome-help-itstool.pot |   68 +++++++++++++++++++++++
 stats/utils.py                                  |   16 +++--
 4 files changed, 89 insertions(+), 12 deletions(-)
---
diff --git a/stats/models.py b/stats/models.py
index e1a3e80..311e62e 100644
--- a/stats/models.py
+++ b/stats/models.py
@@ -1356,11 +1356,12 @@ class Statistics(models.Model):
 
     def fig_stats(self):
         stats = {'fuzzy':0, 'translated':0, 'total':0, 'prc':0}
-        for fig in self.full_po.figures:
-            stats['total'] += 1
-            if fig.get('fuzzy', 0): stats['fuzzy'] += 1
-            else:
-                if fig.get('translated', 0): stats['translated'] += 1
+        if self.full_po and self.full_po.figures:
+            for fig in self.full_po.figures:
+                stats['total'] += 1
+                if fig.get('fuzzy', 0): stats['fuzzy'] += 1
+                else:
+                    if fig.get('translated', 0): stats['translated'] += 1
         stats['untranslated'] = stats['total'] - (stats['translated'] + stats['fuzzy'])
         if stats['total'] > 0:
             stats['prc'] = 100*stats['translated']/stats['total']
diff --git a/stats/tests/__init__.py b/stats/tests/__init__.py
index b4f97aa..5b06b12 100644
--- a/stats/tests/__init__.py
+++ b/stats/tests/__init__.py
@@ -169,7 +169,11 @@ class ModuleTestCase(TestCase):
         res = get_fig_stats(pot_path, image_method='xml2po')
         self.assertEqual(len(res), 1)
         os.remove(pot_path)
-        # TODO: Mallard-style help
+        # Mallard-style help (with itstool)
+        pot_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "help_mallard", "gnome-help-itstool.pot")
+        res = get_fig_stats(pot_path, image_method='itstool')
+        self.assertEqual(len(res), 2)
+        self.assertEqual(res[0]['path'], "figures/gnome.png")
 
     def testIdenticalFigureWarning(self):
         """ Detect warning if translated figure is identical to original figure """
diff --git a/stats/tests/help_mallard/gnome-help-itstool.pot b/stats/tests/help_mallard/gnome-help-itstool.pot
new file mode 100644
index 0000000..aad1c03
--- /dev/null
+++ b/stats/tests/help_mallard/gnome-help-itstool.pot
@@ -0,0 +1,68 @@
+msgid ""
+msgstr ""
+"Project-Id-Version: PACKAGE VERSION\n"
+"POT-Creation-Date: 2011-07-08 20:51+0000\n"
+"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
+"Last-Translator: FULL NAME <EMAIL ADDRESS>\n"
+"Language-Team: LANGUAGE <LL li org>\n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=UTF-8\n"
+"Content-Transfer-Encoding: 8bit\n"
+
+#. Put one translator per line, in the form NAME <EMAIL>, YEAR1, YEAR2
+msgctxt "_"
+msgid "translator-credits"
+msgstr ""
+
+#. This is a reference to an external file such as an image or video. When
+#. the file changes, the md5 hash will change to let you know you need to
+#. update your localized copy. The msgstr is not used at all. Set it to
+#. whatever you like once you have updated your copy of the file.
+#.
+#. This is a reference to an external file such as an image or video. When
+#. the file changes, the md5 hash will change to let you know you need to
+#. update your localized copy. The msgstr is not used at all. Set it to
+#. whatever you like once you have updated your copy of the file.
+#: C/index.page:10(media)
+#: C/index.page:10(media)
+msgctxt "_"
+msgid "external ref='figures/gnome.png' md5='85ec62374d1dc2168674a56999647b05'"
+msgstr ""
+
+#. This is a reference to an external file such as an image or video. When
+#. the file changes, the md5 hash will change to let you know you need to
+#. update your localized copy. The msgstr is not used at all. Set it to
+#. whatever you like once you have updated your copy of the file.
+#.
+#. This is a reference to an external file such as an image or video. When
+#. the file changes, the md5 hash will change to let you know you need to
+#. update your localized copy. The msgstr is not used at all. Set it to
+#. whatever you like once you have updated your copy of the file.
+#: C/index.page:17(media)
+#: C/index.page:17(media)
+msgctxt "_"
+msgid "external ref='figures/yelp-icon-big.png' md5='292c209957ac50bb031fcec6cddfa47f'"
+msgstr ""
+
+#: C/index.page:6(info/desc)
+#: C/index.page:8(info/title)
+#: C/index.page:9(info/title)
+#: C/index.page:6(info/desc)
+#: C/index.page:8(info/title)
+#: C/index.page:9(info/title)
+msgid "Desktop Help"
+msgstr ""
+
+#: C/index.page:10(info/title)
+#: C/index.page:10(info/title)
+msgid "<media type=\"image\" mime=\"image/png\" src=\"figures/gnome.png\">Help</media>"
+msgstr ""
+
+#: C/index.page:17(page/title)
+#: C/index.page:17(page/title)
+msgid "<media type=\"image\" src=\"figures/yelp-icon-big.png\">Yelp logo</media> Desktop Help"
+msgstr ""
+
+#: C/legal.xml:2(license/p)
+msgid "Creative Commons Share Alike 3.0"
+msgstr ""
diff --git a/stats/utils.py b/stats/utils.py
index 89152d0..c780d4f 100644
--- a/stats/utils.py
+++ b/stats/utils.py
@@ -53,6 +53,8 @@ extract_tools = {
         'mod_var' : "DOC_ID",
         'incl_var': "DOC_PAGES",
         'img_grep': "^msgid \"@@image:",
+        # Lines to keep before matched grep to catch the ,fuzzy or #|msgid line
+        'bef_line': 1,
         'img_regex': re.compile("^msgid \"@@image: \'(?P<path>[^\']*)\'; md5=(?P<hash>[^\"]*)\""),
     },
     'itstool': {
@@ -60,7 +62,8 @@ extract_tools = {
         'mod_var' : "HELP_ID",
         'incl_var': "HELP_FILES",
         'img_grep': "^msgid \"external ref=",
-        'img_regex': re.compile("^msgid \"external ref=\'(?P<path>[^\']*)\'; md5=\'(?P<hash>[^\']*)\'\""),
+        'bef_line': 2,
+        'img_regex': re.compile("^msgid \"external ref=\'(?P<path>[^\']*)\' md5=\'(?P<hash>[^\']*)\'\""),
     },
 }
 
@@ -400,8 +403,9 @@ def get_fig_stats(pofile, image_method, trans_stats=True):
     if image_method not in ('xml2po', 'itstool'):
         return []
     # Extract image strings: beforeline/msgid/msgstr/grep auto output a fourth line
-    command = "msgcat --no-wrap %(pofile)s| grep -A 1 -B 1 '%(grep)s'" % {
-        'pofile': pofile, 'grep': extract_tools[image_method]['img_grep']
+    before_lines = extract_tools[image_method]['bef_line']
+    command = "msgcat --no-wrap %(pofile)s| grep -A 1 -B %(before)s '%(grep)s'" % {
+        'pofile': pofile, 'grep': extract_tools[image_method]['img_grep'], 'before': before_lines,
     }
     (status, output, errs) = run_shell_command(command)
     if status != STATUS_OK:
@@ -412,16 +416,16 @@ def get_fig_stats(pofile, image_method, trans_stats=True):
         lines = lines[1:] # skip warning messages at the top of the output
 
     figures = []
-    for i, line in islice(enumerate(lines), 0, None, 4):
+    for i, line in islice(enumerate(lines), 0, None, 3+before_lines):
         # TODO: add image size
         fig = {"path": '', "hash": ''}
-        m = extract_tools[image_method]['img_regex'].match(lines[i+1])
+        m = extract_tools[image_method]['img_regex'].match(lines[i+before_lines])
         if m:
             fig["path"] = m.group('path')
             fig["hash"] = m.group('hash')
         if trans_stats:
             fig["fuzzy"] = (line=='#, fuzzy' or line[:8]=='#| msgid')
-            fig["translated"] = len(lines[i+2])>9 and not fig['fuzzy']
+            fig["translated"] = len(lines[i+before_lines+1])>9 and not fig['fuzzy']
         figures.append(fig)
     return figures
 



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