changeset 318:9186e4449927 stable

Ported [349] to 0.9.x branch.
author cmlenz
date Fri, 01 Feb 2008 15:25:20 +0000
parents 9121183df490
children cd588918443e
files 0.9.x/ChangeLog 0.9.x/babel/messages/frontend.py 0.9.x/babel/messages/pofile.py 0.9.x/babel/messages/tests/pofile.py 0.9.x/babel/util.py
diffstat 5 files changed, 52 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/0.9.x/ChangeLog
+++ b/0.9.x/ChangeLog
@@ -4,8 +4,10 @@
 
  * Fixed catalogs' charset values not being recognized (ticket #66).
  * Fixed fuzzy matching when updating message catalogs (ticket #82).
- * Fixed bug in catalog updating, that in some cases pulled in
-   translations from different catalogs based on the same template.
+ * Fixed bug in catalog updating, that in some cases pulled in translations
+   from different catalogs based on the same template.
+ * Location lines in PO files do no longer get wrapped at hyphens in file
+   names (ticket #79).
 
 
 Version 0.9.1
--- a/0.9.x/babel/messages/frontend.py
+++ b/0.9.x/babel/messages/frontend.py
@@ -28,7 +28,6 @@
 from StringIO import StringIO
 import sys
 import tempfile
-import textwrap
 
 from babel import __version__ as VERSION
 from babel import Locale, localedata
--- a/0.9.x/babel/messages/pofile.py
+++ b/0.9.x/babel/messages/pofile.py
@@ -25,11 +25,10 @@
     set
 except NameError:
     from sets import Set as set
-from textwrap import wrap
 
 from babel import __version__ as VERSION
 from babel.messages.catalog import Catalog, Message
-from babel.util import LOCALTZ
+from babel.util import wraptext, LOCALTZ
 
 __all__ = ['read_po', 'write_po']
 __docformat__ = 'restructuredtext en'
@@ -370,7 +369,7 @@
     def _write_comment(comment, prefix=''):
         lines = comment
         if width and width > 0:
-            lines = wrap(comment, width, break_long_words=False)
+            lines = wraptext(comment, width)
         for line in lines:
             _write('#%s %s\n' % (prefix, line.strip()))
 
@@ -404,8 +403,8 @@
             if width and width > 0:
                 lines = []
                 for line in comment_header.splitlines():
-                    lines += wrap(line, width=width, subsequent_indent='# ',
-                                  break_long_words=False)
+                    lines += wraptext(line, width=width,
+                                      subsequent_indent='# ')
                 comment_header = u'\n'.join(lines) + u'\n'
             _write(comment_header)
 
--- a/0.9.x/babel/messages/tests/pofile.py
+++ b/0.9.x/babel/messages/tests/pofile.py
@@ -220,6 +220,21 @@
 #
 #, fuzzy''', '\n'.join(buf.getvalue().splitlines()[:7]))
 
+    def test_wrap_locations_with_hyphens(self):
+        catalog = Catalog()
+        catalog.add(u'foo', locations=[
+            ('doupy/templates/base/navmenu.inc.html.py', 60)
+        ])
+        catalog.add(u'foo', locations=[
+            ('doupy/templates/job-offers/helpers.html', 22)
+        ])
+        buf = StringIO()
+        pofile.write_po(buf, catalog, omit_header=True)
+        self.assertEqual('''#: doupy/templates/base/navmenu.inc.html.py:60
+#: doupy/templates/job-offers/helpers.html:22
+msgid "foo"
+msgstr ""''', buf.getvalue().strip())
+
     def test_pot_with_translator_comments(self):
         catalog = Catalog()
         catalog.add(u'foo', locations=[('main.py', 1)],
--- a/0.9.x/babel/util.py
+++ b/0.9.x/babel/util.py
@@ -22,9 +22,11 @@
     set
 except NameError:
     from sets import Set as set
+import textwrap
 import time
 
-__all__ = ['distinct', 'pathmatch', 'relpath', 'odict', 'UTC', 'LOCALTZ']
+__all__ = ['distinct', 'pathmatch', 'relpath', 'wraptext', 'odict', 'UTC',
+           'LOCALTZ']
 __docformat__ = 'restructuredtext en'
 
 def distinct(iterable):
@@ -145,6 +147,32 @@
     return match is not None
 
 
+class TextWrapper(textwrap.TextWrapper):
+    wordsep_re = re.compile(
+        r'(\s+|'                                  # any whitespace
+        r'(?<=[\w\!\"\'\&\.\,\?])-{2,}(?=\w))'    # em-dash
+    )
+
+
+def wraptext(text, width=70, initial_indent='', subsequent_indent=''):
+    """Simple wrapper around the ``textwrap.wrap`` function in the standard
+    library. This version does not wrap lines on hyphens in words.
+    
+    :param text: the text to wrap
+    :param width: the maximum line width
+    :param initial_indent: string that will be prepended to the first line of
+                           wrapped output
+    :param subsequent_indent: string that will be prepended to all lines save
+                              the first of wrapped output
+    :return: a list of lines
+    :rtype: `list`
+    """
+    wrapper = TextWrapper(width=width, initial_indent=initial_indent,
+                          subsequent_indent=subsequent_indent,
+                          break_long_words=False)
+    return wrapper.wrap(text)
+
+
 class odict(dict):
     """Ordered dict implementation.
     
Copyright (C) 2012-2017 Edgewall Software