# HG changeset patch # User palgarvio # Date 1229994220 0 # Node ID 8f91314df0b94dca5859a472eb0ce1a5062805e9 # Parent 3dd226bb3ec3ab74284c5ac83f8dadb1c8c1b4aa Now, the `--width` option, although with a default value of 76, it's not set to any value initially so that the `--no-wrap` option can be passed without throwing an error. Fixes #145. With the above bug, another one was found where Babel was not mimic'ing xgtettext's behaviour regarding the `--no-wrap` option where comments are wrapped anyway to the width of 76, and, if `--width` is passed then that value is used to wrap the comments too. diff --git a/babel/messages/frontend.py b/babel/messages/frontend.py --- a/babel/messages/frontend.py +++ b/babel/messages/frontend.py @@ -243,7 +243,7 @@ self.omit_header = False self.output_file = None self.input_dirs = None - self.width = 76 + self.width = None self.no_wrap = False self.sort_output = False self.sort_by_file = False @@ -267,9 +267,9 @@ if self.no_wrap and self.width: raise DistutilsOptionError("'--no-wrap' and '--width' are mutually " "exclusive") - if self.no_wrap: - self.width = None - else: + if not self.no_wrap and not self.width: + self.width = 76 + elif self.width is not None: self.width = int(self.width) if self.sort_output and self.sort_by_file: @@ -803,7 +803,7 @@ parser.add_option('-o', '--output', dest='output', help='path to the output POT file') parser.add_option('-w', '--width', dest='width', type='int', - help="set output line width (default %default)") + help="set output line width (default 76)") 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') @@ -829,7 +829,7 @@ parser.set_defaults(charset='utf-8', keywords=[], no_default_keywords=False, no_location=False, - omit_header = False, width=76, no_wrap=False, + omit_header = False, width=None, no_wrap=False, sort_output=False, sort_by_file=False, comment_tags=[], strip_comment_tags=False) options, args = parser.parse_args(argv) @@ -864,8 +864,6 @@ parser.error("'--no-wrap' and '--width' are mutually exclusive.") elif not options.width and not options.no_wrap: options.width = 76 - elif not options.width and options.no_wrap: - options.width = 0 if options.sort_output and options.sort_by_file: parser.error("'--sort-output' and '--sort-by-file' are mutually " diff --git a/babel/messages/pofile.py b/babel/messages/pofile.py --- a/babel/messages/pofile.py +++ b/babel/messages/pofile.py @@ -388,10 +388,13 @@ fileobj.write(text) def _write_comment(comment, prefix=''): - lines = comment + # xgettext always wraps comments even if --no-wrap is passed; + # provide the same behaviour if width and width > 0: - lines = wraptext(comment, width) - for line in lines: + _width = width + else: + _width = 76 + for line in wraptext(comment, _width): _write('#%s %s\n' % (prefix, line.strip())) def _write_message(message, prefix=''): diff --git a/babel/messages/tests/pofile.py b/babel/messages/tests/pofile.py --- a/babel/messages/tests/pofile.py +++ b/babel/messages/tests/pofile.py @@ -284,6 +284,37 @@ #: doupy/templates/job-offers/helpers.html:22 msgid "foo" msgstr ""''', buf.getvalue().strip()) + + def test_no_wrap_and_width_behaviour_on_comments(self): + catalog = Catalog() + catalog.add("Pretty dam long message id, which must really be big " + "to test this wrap behaviour, if not it won't work.", + locations=[("fake.py", n) for n in range(1, 30)]) + buf = StringIO() + pofile.write_po(buf, catalog, width=None, omit_header=True) + self.assertEqual("""\ +#: fake.py:1 fake.py:2 fake.py:3 fake.py:4 fake.py:5 fake.py:6 fake.py:7 +#: fake.py:8 fake.py:9 fake.py:10 fake.py:11 fake.py:12 fake.py:13 fake.py:14 +#: fake.py:15 fake.py:16 fake.py:17 fake.py:18 fake.py:19 fake.py:20 fake.py:21 +#: fake.py:22 fake.py:23 fake.py:24 fake.py:25 fake.py:26 fake.py:27 fake.py:28 +#: fake.py:29 +msgid "pretty dam long message id, which must really be big to test this wrap behaviour, if not it won't work." +msgstr "" + +""", buf.getvalue().lower()) + buf = StringIO() + pofile.write_po(buf, catalog, width=100, omit_header=True) + self.assertEqual("""\ +#: fake.py:1 fake.py:2 fake.py:3 fake.py:4 fake.py:5 fake.py:6 fake.py:7 fake.py:8 fake.py:9 fake.py:10 +#: fake.py:11 fake.py:12 fake.py:13 fake.py:14 fake.py:15 fake.py:16 fake.py:17 fake.py:18 fake.py:19 +#: fake.py:20 fake.py:21 fake.py:22 fake.py:23 fake.py:24 fake.py:25 fake.py:26 fake.py:27 fake.py:28 +#: fake.py:29 +msgid "" +"pretty dam long message id, which must really be big to test this wrap behaviour, if not it won't" +" work." +msgstr "" + +""", buf.getvalue().lower()) def test_pot_with_translator_comments(self): catalog = Catalog()