Skip to content

Commit

Permalink
Allow file locations without line numbers.
Browse files Browse the repository at this point in the history
  • Loading branch information
fschulze committed Nov 4, 2015
1 parent 5116c16 commit 79bc781
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
12 changes: 9 additions & 3 deletions babel/messages/pofile.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ def _process_message_line(lineno, line):
except ValueError:
continue
locations.append((location[:pos], lineno))
else:
locations.append((location, None))
elif line[1:].startswith(','):
for flag in line[2:].lstrip().split(','):
flags.append(flag.strip())
Expand Down Expand Up @@ -449,9 +451,13 @@ def _write_message(message, prefix=''):
_write_comment(comment, prefix='.')

if not no_location:
locs = u' '.join([u'%s:%d' % (filename.replace(os.sep, '/'), lineno)
for filename, lineno in message.locations])
_write_comment(locs, prefix=':')
locs = []
for filename, lineno in message.locations:
if lineno:
locs.append(u'%s:%d' % (filename.replace(os.sep, '/'), lineno))
else:
locs.append(u'%s' % filename.replace(os.sep, '/'))
_write_comment(' '.join(locs), prefix=':')
if message.flags:
_write('#%s\n' % ', '.join([''] + sorted(message.flags)))

Expand Down
17 changes: 16 additions & 1 deletion tests/messages/test_pofile.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,21 @@ def test_sorted_po(self):
msgstr[1] "Voeh"''' in value
assert value.find(b'msgid ""') < value.find(b'msgid "bar"') < value.find(b'msgid "foo"')

def test_file_with_no_lineno(self):
catalog = Catalog()
catalog.add(u'bar', locations=[('utils.py', None)],
user_comments=['Comment About `bar` with',
'multiple lines.'])
buf = BytesIO()
pofile.write_po(buf, catalog, sort_output=True)
value = buf.getvalue().strip()
assert b'''\
# Comment About `bar` with
# multiple lines.
#: utils.py
msgid "bar"
msgstr ""''' in value

def test_silent_location_fallback(self):
buf = BytesIO(b'''\
#: broken_file.py
Expand All @@ -523,7 +538,7 @@ def test_silent_location_fallback(self):
msgid "broken line number"
msgstr ""''')
catalog = pofile.read_po(buf)
self.assertEqual(catalog['missing line number'].locations, [])
self.assertEqual(catalog['missing line number'].locations, [(u'broken_file.py', None)])
self.assertEqual(catalog['broken line number'].locations, [])


Expand Down

0 comments on commit 79bc781

Please sign in to comment.