# HG changeset patch # User cmlenz # Date 1181945939 0 # Node ID 733cca7ff6a5ce1e1379bdf7e5ea759ead6bea2f # Parent 1a3b42b03e7a3619591c4a5463dec1ae5a3a6611 Added tests for `new_catalog` distutils command. diff --git a/babel/messages/catalog.py b/babel/messages/catalog.py --- a/babel/messages/catalog.py +++ b/babel/messages/catalog.py @@ -110,7 +110,7 @@ DEFAULT_HEADER = u"""\ # Translations template for PROJECT. -# Copyright (C) YEAR COPYRIGHT HOLDER +# Copyright (C) YEAR ORGANIZATION # This file is distributed under the same license as the PROJECT project. # FIRST AUTHOR , YEAR. #""" @@ -173,11 +173,12 @@ comment = comment.replace('PROJECT', self.project) \ .replace('VERSION', self.version) \ .replace('YEAR', self.revision_date.strftime('%Y')) \ - .replace('COPYRIGHT HOLDER', self.copyright_holder) + .replace('ORGANIZATION', self.copyright_holder) if self.locale: comment = comment.replace('Translations template', '%s translations' % self.locale.english_name) return comment + def _set_header_comment(self, string): self._header_comment = string @@ -193,6 +194,24 @@ # FIRST AUTHOR , 2007. # + The header can also be set from a string. Any known upper-case variables + will be replaced when the header is retrieved again: + + >>> catalog = Catalog(project='Foobar', version='1.0', + ... copyright_holder='Foo Company') + >>> catalog.header_comment = '''\\ + ... # The POT for my really cool PROJECT project. + ... # Copyright (C) 1990-2003 ORGANIZATION + ... # This file is distributed under the same license as the PROJECT + ... # project. + ... #''' + >>> print catalog.header_comment + # The POT for my really cool Foobar project. + # Copyright (C) 1990-2003 Foo Company + # This file is distributed under the same license as the Foobar + # project. + # + :type: `unicode` """) @@ -237,7 +256,8 @@ value, tzoffset, _ = re.split('[+-](\d{4})$', value, 1) tt = time.strptime(value, '%Y-%m-%d %H:%M') ts = time.mktime(tt) - tzoffset = FixedOffsetTimezone(int(tzoffset)) + tzoffset = FixedOffsetTimezone(int(tzoffset[:2]) * 60 + + int(tzoffset[2:])) self.creation_date = datetime.fromtimestamp(ts, tzoffset) mime_headers = property(_get_mime_headers, _set_mime_headers, doc="""\ @@ -411,6 +431,8 @@ # special treatment for the header message headers = message_from_string(message.string.encode(self.charset)) self.mime_headers = headers.items() + self.header_comment = '\n'.join(['# %s' % comment for comment + in message.user_comments]) else: if isinstance(id, (list, tuple)): assert isinstance(message.string, (list, tuple)) diff --git a/babel/messages/frontend.py b/babel/messages/frontend.py --- a/babel/messages/frontend.py +++ b/babel/messages/frontend.py @@ -309,7 +309,6 @@ infile.close() catalog.locale = self._locale - catalog.revision_date = datetime.now() outfile = open(self.output_file, 'w') try: diff --git a/babel/messages/pofile.py b/babel/messages/pofile.py --- a/babel/messages/pofile.py +++ b/babel/messages/pofile.py @@ -114,12 +114,9 @@ if comment: # Just check that we're not adding empty comments auto_comments.append(comment) - elif line[1:].startswith(' '): + else: # These are called user comments - comment = line[1:].strip() - if comment: - # Just check that we're not adding empty comments - user_comments.append(comment) + user_comments.append(line[1:].strip()) else: if line.startswith('msgid_plural'): in_msgid = True diff --git a/babel/messages/tests/data/project/i18n/messages.pot b/babel/messages/tests/data/project/i18n/messages.pot new file mode 100644 --- /dev/null +++ b/babel/messages/tests/data/project/i18n/messages.pot @@ -0,0 +1,32 @@ +# Translations template for TestProject. +# Copyright (C) 2007 FooBar, Inc. +# This file is distributed under the same license as the TestProject +# project. +# FIRST AUTHOR , 2007. +# +#, fuzzy +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: YEAR-MO-DA HO:MI+ZONE\n" +"Last-Translator: FULL NAME \n" +"Language-Team: LANGUAGE \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=utf-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Generated-By: Babel 0.1\n" + +#. This will be a translator coment, +#. that will include several lines +#: project/file1.py:8 +msgid "bar" +msgstr "" + +#: project/file2.py:9 +msgid "foobar" +msgid_plural "foobars" +msgstr[0] "" +msgstr[1] "" + diff --git a/babel/messages/tests/frontend.py b/babel/messages/tests/frontend.py --- a/babel/messages/tests/frontend.py +++ b/babel/messages/tests/frontend.py @@ -16,6 +16,7 @@ from distutils.log import _global_log import doctest import os +import shutil import time import unittest @@ -40,14 +41,14 @@ self.cmd.initialize_options() def tearDown(self): - pot_file = os.path.join(self.datadir, 'project', 'i18n', 'messages.pot') + pot_file = os.path.join(self.datadir, 'project', 'i18n', 'temp.pot') if os.path.isfile(pot_file): os.unlink(pot_file) os.chdir(self.olddir) def test_neither_default_nor_custom_keywords(self): - self.cmd.output_file = 'foobar' + self.cmd.output_file = 'dummy' self.cmd.no_default_keywords = True self.assertRaises(DistutilsOptionError, self.cmd.finalize_options) @@ -55,7 +56,7 @@ self.assertRaises(DistutilsOptionError, self.cmd.finalize_options) def test_both_sort_output_and_sort_by_file(self): - self.cmd.output_file = 'foobar' + self.cmd.output_file = 'dummy' self.cmd.sort_output = True self.cmd.sort_by_file = True self.assertRaises(DistutilsOptionError, self.cmd.finalize_options) @@ -63,13 +64,16 @@ def test_extraction_with_default_mapping(self): self.cmd.copyright_holder = 'FooBar, Inc.' self.cmd.msgid_bugs_address = 'bugs.address@email.tld' - self.cmd.output_file = 'project/i18n/messages.pot' + self.cmd.output_file = 'project/i18n/temp.pot' self.cmd.add_comments = 'TRANSLATOR:,TRANSLATORS:' self.cmd.finalize_options() self.cmd.run() - self.assertEqual(open(os.path.join(self.datadir, 'project', 'i18n', - 'messages.pot'), 'U').read(), + + pot_file = os.path.join(self.datadir, 'project', 'i18n', 'temp.pot') + assert os.path.isfile(pot_file) + + self.assertEqual( r"""# Translations template for TestProject. # Copyright (C) %(year)s FooBar, Inc. # This file is distributed under the same license as the TestProject @@ -110,19 +114,22 @@ """ % {'version': VERSION, 'year': time.strftime('%Y'), - 'date': time.strftime('%Y-%m-%d %H:%M%z')}) + 'date': time.strftime('%Y-%m-%d %H:%M%z')}, open(pot_file, 'U').read()) def test_extraction_with_mapping_file(self): self.cmd.copyright_holder = 'FooBar, Inc.' self.cmd.msgid_bugs_address = 'bugs.address@email.tld' self.cmd.mapping_file = 'mapping.cfg' - self.cmd.output_file = 'project/i18n/messages.pot' + self.cmd.output_file = 'project/i18n/temp.pot' self.cmd.add_comments = 'TRANSLATOR:,TRANSLATORS:' self.cmd.finalize_options() self.cmd.run() - self.assertEqual(open(os.path.join(self.datadir, 'project', 'i18n', - 'messages.pot'), 'r').read(), + + pot_file = os.path.join(self.datadir, 'project', 'i18n', 'temp.pot') + assert os.path.isfile(pot_file) + + self.assertEqual( r"""# Translations template for TestProject. # Copyright (C) %(year)s FooBar, Inc. # This file is distributed under the same license as the TestProject @@ -157,7 +164,7 @@ """ % {'version': VERSION, 'year': time.strftime('%Y'), - 'date': time.strftime('%Y-%m-%d %H:%M%z')}) + 'date': time.strftime('%Y-%m-%d %H:%M%z')}, open(pot_file, 'U').read()) def test_extraction_with_mapping_dict(self): self.dist.message_extractors = { @@ -168,13 +175,16 @@ } self.cmd.copyright_holder = 'FooBar, Inc.' self.cmd.msgid_bugs_address = 'bugs.address@email.tld' - self.cmd.output_file = 'project/i18n/messages.pot' + self.cmd.output_file = 'project/i18n/temp.pot' self.cmd.add_comments = 'TRANSLATOR:,TRANSLATORS:' self.cmd.finalize_options() self.cmd.run() - self.assertEqual(open(os.path.join(self.datadir, 'project', 'i18n', - 'messages.pot'), 'r').read(), + + pot_file = os.path.join(self.datadir, 'project', 'i18n', 'temp.pot') + assert os.path.isfile(pot_file) + + self.assertEqual( r"""# Translations template for TestProject. # Copyright (C) %(year)s FooBar, Inc. # This file is distributed under the same license as the TestProject @@ -209,13 +219,98 @@ """ % {'version': VERSION, 'year': time.strftime('%Y'), - 'date': time.strftime('%Y-%m-%d %H:%M%z')}) + 'date': time.strftime('%Y-%m-%d %H:%M%z')}, open(pot_file, 'U').read()) + + +class NewCatalogTestCase(unittest.TestCase): + + def setUp(self): + self.olddir = os.getcwd() + self.datadir = os.path.join(os.path.dirname(__file__), 'data') + os.chdir(self.datadir) + _global_log.threshold = 5 # shut up distutils logging + + self.dist = Distribution(dict( + name='TestProject', + version='0.1', + packages=['project'] + )) + self.cmd = frontend.new_catalog(self.dist) + self.cmd.initialize_options() + + def tearDown(self): + locale_dir = os.path.join(self.datadir, 'project', 'i18n', 'en_US') + if os.path.isdir(locale_dir): + shutil.rmtree(locale_dir) + + os.chdir(self.olddir) + + def test_no_input_file(self): + self.cmd.locale = 'en_US' + self.cmd.output_file = 'dummy' + self.assertRaises(DistutilsOptionError, self.cmd.finalize_options) + + def test_no_locale(self): + self.cmd.input_file = 'dummy' + self.cmd.output_file = 'dummy' + self.assertRaises(DistutilsOptionError, self.cmd.finalize_options) + + def test_init_with_output_dir(self): + self.cmd.input_file = 'project/i18n/messages.pot' + self.cmd.locale = 'en_US' + self.cmd.output_dir = 'project/i18n' + + self.cmd.finalize_options() + self.cmd.run() + + po_file = os.path.join(self.datadir, 'project', 'i18n', 'en_US', + 'LC_MESSAGES', 'messages.po') + 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 , 2007. +# +#, fuzzy +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 \n" +"Language-Team: en_US \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 "bar" +msgstr "" + +#: project/file2.py:9 +msgid "foobar" +msgid_plural "foobars" +msgstr[0] "" +msgstr[1] "" + +""" % {'version': VERSION, + 'date': time.strftime('%Y-%m-%d %H:%M%z')}, + open(po_file, 'U').read()) def suite(): suite = unittest.TestSuite() suite.addTest(doctest.DocTestSuite(frontend)) suite.addTest(unittest.makeSuite(ExtractMessagesTestCase)) + suite.addTest(unittest.makeSuite(NewCatalogTestCase)) return suite if __name__ == '__main__':