[Checkins] SVN: zope.app.locales/trunk/src/zope/app/locales/ i18nextract: sort the file locations.

Marius Gedminas marius at pov.lt
Mon Dec 12 14:05:33 UTC 2011


Log message for revision 123772:
  i18nextract: sort the file locations.
  
  Previously the order of the entries in the pot could jump randomly between
  runs because the sort key was not stable (find_files returns the files in
  random order, they're then added to the catalog location list in the same
  order).
  
  Now the order is deterministic, although it is not lexicographical (.py files
  will come before .zcml files will come before .pt files; within each group the
  files will be sorted).
  
  

Changed:
  U   zope.app.locales/trunk/src/zope/app/locales/extract.py
  U   zope.app.locales/trunk/src/zope/app/locales/tests.py

-=-
Modified: zope.app.locales/trunk/src/zope/app/locales/extract.py
===================================================================
--- zope.app.locales/trunk/src/zope/app/locales/extract.py	2011-12-12 14:04:21 UTC (rev 123771)
+++ zope.app.locales/trunk/src/zope/app/locales/extract.py	2011-12-12 14:05:33 UTC (rev 123772)
@@ -139,6 +139,10 @@
     def __cmp__(self, other):
         return cmp(self.comments, other.comments)
 
+    def __repr__(self):
+        return '<POTEntry: %r>' % self.msgid
+
+
 class POTMaker(object):
     """This class inserts sets of strings into a POT file.
     """
@@ -156,7 +160,7 @@
             if msgid not in self.catalog:
                 self.catalog[msgid] = POTEntry(msgid)
 
-            for filename, lineno in locations:
+            for filename, lineno in sorted(locations):
                 if base_dir is not None:
                     filename = filename.replace(base_dir, '')
                 self.catalog[msgid].addLocationComment(filename, lineno)

Modified: zope.app.locales/trunk/src/zope/app/locales/tests.py
===================================================================
--- zope.app.locales/trunk/src/zope/app/locales/tests.py	2011-12-12 14:04:21 UTC (rev 123771)
+++ zope.app.locales/trunk/src/zope/app/locales/tests.py	2011-12-12 14:05:33 UTC (rev 123772)
@@ -67,9 +67,49 @@
         self.assertEqual(h_count, len(list(gsm.registeredHandlers())))
 
 
+def doctest_POTMaker_add():
+    """Test for POTMaker.add
 
+        >>> from zope.app.locales.extract import POTMaker
+        >>> pm = POTMaker('/dev/null', 'path')
+        >>> pm.add({'msgid1': [('file2.py', 2), ('file1.py', 3)],
+        ...         'msgid2': [('file1.py', 5)]})
+
+        >>> sorted(pm.catalog)
+        ['msgid1', 'msgid2']
+
+        >>> pm.catalog['msgid1']
+        <POTEntry: 'msgid1'>
+
+        >>> pm.catalog['msgid2']
+        <POTEntry: 'msgid2'>
+
+    The locations have been sorted
+
+        >>> print pm.catalog['msgid1'].comments
+        #: file1.py:3
+        #: file2.py:2
+        <BLANKLINE>
+
+    You can call add multiple times and it will merge the entries
+
+        >>> pm.add({'msgid1': [('file1.zcml', 4)],
+        ...         'msgid3': [('file2.zcml', 5)]})
+
+        >>> print pm.catalog['msgid1'].comments
+        #: file1.py:3
+        #: file2.py:2
+        #: file1.zcml:4
+        <BLANKLINE>
+
+    Unfortunately it doesn't re-sort the locations, which is arguably a bug.
+    Please feel free to fix and update this test.
+    """
+
+
 def test_suite():
     return unittest.TestSuite((
+        doctest.DocTestSuite(),
         doctest.DocTestSuite('zope.app.locales.extract',
             optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS,),
         unittest.makeSuite(TestIsUnicodeInAllCatalog),



More information about the checkins mailing list