Skip to content

Commit

Permalink
When sorting locations, sort line numbers as ints, not as strings.
Browse files Browse the repository at this point in the history
  • Loading branch information
mgedmin committed Dec 12, 2011
1 parent 70a3a71 commit 49bafdf
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGES.txt
Expand Up @@ -7,6 +7,9 @@ CHANGES

- Handle Unicode msgids and default values.

- Consistent sorting of source filenames for each msgid. Also sort line
numbers numerically, not lexicographically.


3.7.1 (2011-12-07)
------------------
Expand Down
9 changes: 6 additions & 3 deletions src/zope/app/locales/extract.py
Expand Up @@ -113,13 +113,15 @@ class POTEntry(object):
def __init__(self, msgid, comments=None):
self.msgid = msgid
self.comments = comments or ''
self._location = []

def addComment(self, comment):
self.comments += comment + '\n'

def addLocationComment(self, filename, line):
self.comments += '#: %s:%s\n' % (
filename.replace(os.sep, '/'), line)
filename = filename.replace(os.sep, '/')
self.comments += '#: %s:%s\n' % (filename, line)
self._location.append((filename, line))

def write(self, file):
if self.comments:
Expand All @@ -137,7 +139,8 @@ def write(self, file):
file.write('\n')

def __cmp__(self, other):
return cmp(self.comments, other.comments)
return cmp((self._location, self.msgid),
(other._location, other.msgid))

def __repr__(self):
return '<POTEntry: %r>' % self.msgid
Expand Down
35 changes: 35 additions & 0 deletions src/zope/app/locales/tests.py
Expand Up @@ -67,6 +67,41 @@ def test_configure_should_register_n_components(self):
self.assertEqual(h_count, len(list(gsm.registeredHandlers())))


def doctest_POTEntry_sort_order():
"""Test for POTEntry.__cmp__
>>> from zope.app.locales.extract import POTEntry
'file1' comes before 'file2'
>>> pe1 = POTEntry('msgid1')
>>> pe1.addLocationComment('file1', 42)
>>> pe2 = POTEntry('msgid1')
>>> pe2.addLocationComment('file2', 42)
>>> pe1 < pe2
True
line 9 comes before line 42
>>> pe3 = POTEntry('msgid1')
>>> pe3.addLocationComment('file1', 9)
>>> pe3 < pe1
True
Finally, msgid1 comes before msgid2
>>> pe4 = POTEntry('msgid2')
>>> pe4.addLocationComment('file1', 42)
>>> pe1 < pe4
True
"""


def doctest_POTMaker_add():
"""Test for POTMaker.add
Expand Down

0 comments on commit 49bafdf

Please sign in to comment.