changeset 574:99c48a6ca1d6 trunk

add "--no-wrap" option for init/update commands (#289)
author fschwarz
date Tue, 24 Jul 2012 07:33:43 +0000
parents a95f6e35246c
children 8ce41e60f90d
files ChangeLog babel/messages/frontend.py babel/messages/pofile.py babel/messages/tests/frontend.py
diffstat 4 files changed, 92 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,6 @@
 Version 1.0
 http://svn.edgewall.org/repos/babel/tags/1.0.0/
-(? ? 2011 from trunk)
+(? ? 2012 from trunk)
 
  * Added support for the locale plural rules defined by the CLDR.
  * Added `format_timedelta` function to support localized formatting of
@@ -38,6 +38,7 @@
    Algarvio)
  * Support for context-aware methods during message extraction (#229, patch
    from David Rios)
+ * "init" and "update" commands support "--no-wrap" option (#289)
 
 Version 0.9.6
 http://svn.edgewall.org/repos/babel/tags/0.9.6/
--- a/babel/messages/frontend.py
+++ b/babel/messages/frontend.py
@@ -402,7 +402,11 @@
          "'<output_dir>/<locale>/LC_MESSAGES/<domain>.po')"),
         ('locale=', 'l',
          'locale for the new localized catalog'),
+        ('no-wrap', None,
+         'do not break long message lines, longer than the output line width, '
+         'into several lines'),
     ]
+    boolean_options = ['no-wrap']
 
     def initialize_options(self):
         self.output_dir = None
@@ -410,6 +414,8 @@
         self.input_file = None
         self.locale = None
         self.domain = 'messages'
+        self.no_wrap = False
+        self.width = None
 
     def finalize_options(self):
         if not self.input_file:
@@ -431,6 +437,8 @@
 
         if not os.path.exists(os.path.dirname(self.output_file)):
             os.makedirs(os.path.dirname(self.output_file))
+        if not self.no_wrap:
+            self.width = 76
 
     def run(self):
         log.info('creating catalog %r based on %r', self.output_file,
@@ -449,7 +457,7 @@
 
         outfile = open(self.output_file, 'w')
         try:
-            write_po(outfile, catalog)
+            write_po(outfile, catalog, width=self.width)
         finally:
             outfile.close()
 
@@ -486,6 +494,9 @@
          "'<output_dir>/<locale>/LC_MESSAGES/<domain>.po')"),
         ('locale=', 'l',
          'locale of the catalog to compile'),
+        ('no-wrap', None,
+         'do not break long message lines, longer than the output line width, '
+         'into several lines'),
         ('ignore-obsolete=', None,
          'whether to omit obsolete messages from the output'),
         ('no-fuzzy-matching', 'N',
@@ -501,6 +512,7 @@
         self.output_dir = None
         self.output_file = None
         self.locale = None
+        self.no_wrap = False
         self.ignore_obsolete = False
         self.no_fuzzy_matching = False
         self.previous = False
@@ -547,6 +559,10 @@
         if not po_files:
             raise DistutilsOptionError('no message catalogs found')
 
+        extra_params = {}
+        if self.no_wrap:
+            extra_params['width'] = None
+
         for locale, filename in po_files:
             log.info('updating catalog %r based on %r', filename,
                      self.input_file)
@@ -566,7 +582,7 @@
                 try:
                     write_po(tmpfile, catalog,
                              ignore_obsolete=self.ignore_obsolete,
-                             include_previous=self.previous)
+                             include_previous=self.previous, **extra_params)
                 finally:
                     tmpfile.close()
             except:
@@ -943,6 +959,9 @@
                                "<domain>.po')")
         parser.add_option('--locale', '-l', dest='locale', metavar='LOCALE',
                           help='locale for the new localized catalog')
+        parser.add_option('--no-wrap', dest='no_wrap', action='store_true',
+                          help='do not break long message lines, longer than '
+                               'the output line width, into several lines')
 
         parser.set_defaults(domain='messages')
         options, args = parser.parse_args(argv)
@@ -966,6 +985,9 @@
                                                options.domain + '.po')
         if not os.path.exists(os.path.dirname(options.output_file)):
             os.makedirs(os.path.dirname(options.output_file))
+        width = 76
+        if options.no_wrap:
+            width = None
 
         infile = open(options.input_file, 'r')
         try:
@@ -983,7 +1005,7 @@
 
         outfile = open(options.output_file, 'w')
         try:
-            write_po(outfile, catalog)
+            write_po(outfile, catalog, width=width)
         finally:
             outfile.close()
 
@@ -1008,6 +1030,9 @@
                                "<domain>.po')")
         parser.add_option('--locale', '-l', dest='locale', metavar='LOCALE',
                           help='locale of the translations catalog')
+        parser.add_option('--no-wrap', dest='no_wrap', action = 'store_true',
+                          help='do not break long message lines, longer than '
+                               'the output line width, into several lines')
         parser.add_option('--ignore-obsolete', dest='ignore_obsolete',
                           action='store_true',
                           help='do not include obsolete messages in the output '
@@ -1062,6 +1087,9 @@
         if not po_files:
             parser.error('no message catalogs found')
 
+        extra_params = {}
+        if options.no_wrap:
+            extra_params['width'] = None
         for locale, filename in po_files:
             self.log.info('updating catalog %r based on %r', filename,
                           options.input_file)
@@ -1081,7 +1109,7 @@
                 try:
                     write_po(tmpfile, catalog,
                              ignore_obsolete=options.ignore_obsolete,
-                             include_previous=options.previous)
+                             include_previous=options.previous, **extra_params)
                 finally:
                     tmpfile.close()
             except:
--- a/babel/messages/pofile.py
+++ b/babel/messages/pofile.py
@@ -443,6 +443,8 @@
                     lines += wraptext(line, width=width,
                                       subsequent_indent='# ')
                 comment_header = u'\n'.join(lines) + u'\n'
+            else:
+                comment_header += u'\n'
             _write(comment_header)
 
         for comment in message.user_comments:
--- a/babel/messages/tests/frontend.py
+++ b/babel/messages/tests/frontend.py
@@ -509,6 +509,62 @@
        'date': format_datetime(datetime.now(LOCALTZ), 'yyyy-MM-dd HH:mmZ',
                                tzinfo=LOCALTZ, locale='ja_JP')},
        open(po_file, 'U').read())
+   
+    def test_supports_no_wrap(self):
+        self.cmd.input_file = 'project/i18n/long_messages.pot'
+        self.cmd.locale = 'en_US'
+        self.cmd.output_dir = 'project/i18n'
+        
+        long_message = '"'+ 'xxxxx '*15 + '"'
+        
+        pot_contents = open('project/i18n/messages.pot', 'U').read()
+        pot_with_very_long_line = pot_contents.replace('"bar"', long_message)
+        open(self.cmd.input_file, 'wb').write(pot_with_very_long_line)
+        self.cmd.no_wrap = True
+
+        self.cmd.finalize_options()
+        self.cmd.run()
+
+        po_file = self._po_file('en_US')
+        assert os.path.isfile(po_file)
+        self.assertEqual(
+r"""# English (United States) translations for TestProject.
+# Copyright (C) 2007 FooBar, Inc.
+# This file is distributed under the same license as the TestProject
+# project.
+# FIRST AUTHOR <EMAIL@ADDRESS>, 2007.
+# 
+msgid ""
+msgstr ""
+"Project-Id-Version: TestProject 0.1\n"
+"Report-Msgid-Bugs-To: bugs.address@email.tld\n"
+"POT-Creation-Date: 2007-04-01 15:30+0200\n"
+"PO-Revision-Date: %(date)s\n"
+"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
+"Language-Team: en_US <LL@li.org>\n"
+"Plural-Forms: nplurals=2; plural=(n != 1)\n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=utf-8\n"
+"Content-Transfer-Encoding: 8bit\n"
+"Generated-By: Babel %(version)s\n"
+
+#. This will be a translator coment,
+#. that will include several lines
+#: project/file1.py:8
+msgid %(long_message)s
+msgstr ""
+
+#: project/file2.py:9
+msgid "foobar"
+msgid_plural "foobars"
+msgstr[0] ""
+msgstr[1] ""
+
+""" % {'version': VERSION,
+       'date': format_datetime(datetime.now(LOCALTZ), 'yyyy-MM-dd HH:mmZ',
+                               tzinfo=LOCALTZ, locale='en_US'),
+       'long_message': long_message},
+       open(po_file, 'U').read())
 
 
 class CommandLineInterfaceTestCase(unittest.TestCase):
Copyright (C) 2012-2017 Edgewall Software