diff options
-rwxr-xr-x | babel/messages/frontend.py | 77 | ||||
-rw-r--r-- | babel/messages/tests/frontend.py | 140 | ||||
-rw-r--r-- | babel/support.py | 3 | ||||
-rw-r--r-- | babel/tests/support.py | 3 |
4 files changed, 123 insertions, 100 deletions
diff --git a/babel/messages/frontend.py b/babel/messages/frontend.py index 90d01a4..753151e 100755 --- a/babel/messages/frontend.py +++ b/babel/messages/frontend.py @@ -881,11 +881,6 @@ class CommandLineInterface(object): if not args: parser.error('incorrect number of arguments') - if options.output not in (None, '-'): - outfile = open(options.output, 'w') - else: - outfile = sys.stdout - keywords = DEFAULT_KEYWORDS.copy() if options.no_default_keywords: if not options.keywords: @@ -914,47 +909,53 @@ class CommandLineInterface(object): parser.error("'--sort-output' and '--sort-by-file' are mutually " "exclusive") - try: - catalog = Catalog(project=options.project, - version=options.version, - msgid_bugs_address=options.msgid_bugs_address, - copyright_holder=options.copyright_holder, - charset=options.charset) - - for dirname in args: - if not os.path.isdir(dirname): - parser.error('%r is not a directory' % dirname) - - def callback(filename, method, options): - if method == 'ignore': - return - filepath = os.path.normpath(os.path.join(dirname, filename)) - optstr = '' - if options: - optstr = ' (%s)' % ', '.join(['%s="%s"' % (k, v) for - k, v in options.items()]) - self.log.info('extracting messages from %s%s', filepath, - optstr) + catalog = Catalog(project=options.project, + version=options.version, + msgid_bugs_address=options.msgid_bugs_address, + copyright_holder=options.copyright_holder, + charset=options.charset) + + for dirname in args: + if not os.path.isdir(dirname): + parser.error('%r is not a directory' % dirname) + + def callback(filename, method, options): + if method == 'ignore': + return + filepath = os.path.normpath(os.path.join(dirname, filename)) + optstr = '' + if options: + optstr = ' (%s)' % ', '.join(['%s="%s"' % (k, v) for + k, v in options.items()]) + self.log.info('extracting messages from %s%s', filepath, + optstr) + + extracted = extract_from_dir(dirname, method_map, options_map, + keywords, options.comment_tags, + callback=callback, + strip_comment_tags= + options.strip_comment_tags) + for filename, lineno, message, comments, context in extracted: + filepath = os.path.normpath(os.path.join(dirname, filename)) + catalog.add(message, None, [(filepath, lineno)], + auto_comments=comments, context=context) - extracted = extract_from_dir(dirname, method_map, options_map, - keywords, options.comment_tags, - callback=callback, - strip_comment_tags= - options.strip_comment_tags) - for filename, lineno, message, comments, context in extracted: - filepath = os.path.normpath(os.path.join(dirname, filename)) - catalog.add(message, None, [(filepath, lineno)], - auto_comments=comments, context=context) + if options.output not in (None, '-'): + self.log.info('writing PO template file to %s' % options.output) + outfile = open(options.output, 'w') + close_output = True + else: + outfile = sys.stdout + close_output = False - if options.output not in (None, '-'): - self.log.info('writing PO template file to %s' % options.output) + try: write_po(outfile, catalog, width=options.width, no_location=options.no_location, omit_header=options.omit_header, sort_output=options.sort_output, sort_by_file=options.sort_by_file) finally: - if options.output: + if close_output: outfile.close() def init(self, argv): diff --git a/babel/messages/tests/frontend.py b/babel/messages/tests/frontend.py index 2d934e7..0d76b66 100644 --- a/babel/messages/tests/frontend.py +++ b/babel/messages/tests/frontend.py @@ -115,7 +115,8 @@ class ExtractMessagesTestCase(unittest.TestCase): self.cmd.finalize_options() self.cmd.run() - catalog = read_po(open(self._pot_file(), 'U')) + with open(self._pot_file(), 'U') as f: + catalog = read_po(f) msg = catalog.get('bar') self.assertEqual(1, len(msg.locations)) self.assertTrue('file1.py' in msg.locations[0][0]) @@ -138,8 +139,7 @@ class ExtractMessagesTestCase(unittest.TestCase): self.assert_pot_file_exists() - self.assertEqual( -r"""# Translations template for TestProject. + expected_content = r"""# Translations template for TestProject. # Copyright (C) %(year)s FooBar, Inc. # This file is distributed under the same license as the TestProject # project. @@ -180,8 +180,10 @@ msgstr[1] "" """ % {'version': VERSION, 'year': time.strftime('%Y'), 'date': format_datetime(datetime.now(LOCALTZ), 'yyyy-MM-dd HH:mmZ', - tzinfo=LOCALTZ, locale='en')}, - open(self._pot_file(), 'U').read()) + tzinfo=LOCALTZ, locale='en')} + with open(self._pot_file(), 'U') as f: + actual_content = f.read() + self.assertEqual(expected_content, actual_content) def test_extraction_with_mapping_file(self): self.cmd.copyright_holder = 'FooBar, Inc.' @@ -195,8 +197,7 @@ msgstr[1] "" self.assert_pot_file_exists() - self.assertEqual( -r"""# Translations template for TestProject. + expected_content = r"""# Translations template for TestProject. # Copyright (C) %(year)s FooBar, Inc. # This file is distributed under the same license as the TestProject # project. @@ -231,8 +232,10 @@ msgstr[1] "" """ % {'version': VERSION, 'year': time.strftime('%Y'), 'date': format_datetime(datetime.now(LOCALTZ), 'yyyy-MM-dd HH:mmZ', - tzinfo=LOCALTZ, locale='en')}, - open(self._pot_file(), 'U').read()) + tzinfo=LOCALTZ, locale='en')} + with open(self._pot_file(), 'U') as f: + actual_content = f.read() + self.assertEqual(expected_content, actual_content) def test_extraction_with_mapping_dict(self): self.dist.message_extractors = { @@ -251,8 +254,7 @@ msgstr[1] "" self.assert_pot_file_exists() - self.assertEqual( -r"""# Translations template for TestProject. + expected_content = r"""# Translations template for TestProject. # Copyright (C) %(year)s FooBar, Inc. # This file is distributed under the same license as the TestProject # project. @@ -287,8 +289,10 @@ msgstr[1] "" """ % {'version': VERSION, 'year': time.strftime('%Y'), 'date': format_datetime(datetime.now(LOCALTZ), 'yyyy-MM-dd HH:mmZ', - tzinfo=LOCALTZ, locale='en')}, - open(self._pot_file(), 'U').read()) + tzinfo=LOCALTZ, locale='en')} + with open(self._pot_file(), 'U') as f: + actual_content = f.read() + self.assertEqual(expected_content, actual_content) class InitCatalogTestCase(unittest.TestCase): @@ -343,8 +347,7 @@ class InitCatalogTestCase(unittest.TestCase): po_file = self._po_file('en_US') assert os.path.isfile(po_file) - self.assertEqual( -r"""# English (United States) translations for TestProject. + expected_content = r"""# English (United States) translations for TestProject. # Copyright (C) 2007 FooBar, Inc. # This file is distributed under the same license as the TestProject # project. @@ -378,8 +381,10 @@ msgstr[1] "" """ % {'version': VERSION, 'date': format_datetime(datetime.now(LOCALTZ), 'yyyy-MM-dd HH:mmZ', - tzinfo=LOCALTZ, locale='en')}, - open(po_file, 'U').read()) + tzinfo=LOCALTZ, locale='en')} + with open(po_file, 'U') as f: + actual_content = f.read() + self.assertEqual(expected_content, actual_content) def test_keeps_catalog_non_fuzzy(self): self.cmd.input_file = 'project/i18n/messages_non_fuzzy.pot' @@ -392,8 +397,7 @@ msgstr[1] "" po_file = self._po_file('en_US') assert os.path.isfile(po_file) - self.assertEqual( -r"""# English (United States) translations for TestProject. + expected_content = r"""# English (United States) translations for TestProject. # Copyright (C) 2007 FooBar, Inc. # This file is distributed under the same license as the TestProject # project. @@ -427,8 +431,10 @@ msgstr[1] "" """ % {'version': VERSION, 'date': format_datetime(datetime.now(LOCALTZ), 'yyyy-MM-dd HH:mmZ', - tzinfo=LOCALTZ, locale='en')}, - open(po_file, 'U').read()) + tzinfo=LOCALTZ, locale='en')} + with open(po_file, 'U') as f: + actual_content = f.read() + self.assertEqual(expected_content, actual_content) def test_correct_init_more_than_2_plurals(self): self.cmd.input_file = 'project/i18n/messages.pot' @@ -441,8 +447,7 @@ msgstr[1] "" po_file = self._po_file('lv_LV') assert os.path.isfile(po_file) - self.assertEqual( -r"""# Latvian (Latvia) translations for TestProject. + expected_content = r"""# Latvian (Latvia) translations for TestProject. # Copyright (C) 2007 FooBar, Inc. # This file is distributed under the same license as the TestProject # project. @@ -478,8 +483,10 @@ msgstr[2] "" """ % {'version': VERSION, 'date': format_datetime(datetime.now(LOCALTZ), 'yyyy-MM-dd HH:mmZ', - tzinfo=LOCALTZ, locale='en')}, - open(po_file, 'U').read()) + tzinfo=LOCALTZ, locale='en')} + with open(po_file, 'U') as f: + actual_content = f.read() + self.assertEqual(expected_content, actual_content) def test_correct_init_singular_plural_forms(self): self.cmd.input_file = 'project/i18n/messages.pot' @@ -492,8 +499,7 @@ msgstr[2] "" po_file = self._po_file('ja_JP') assert os.path.isfile(po_file) - self.assertEqual( -r"""# Japanese (Japan) translations for TestProject. + expected_content = r"""# Japanese (Japan) translations for TestProject. # Copyright (C) 2007 FooBar, Inc. # This file is distributed under the same license as the TestProject # project. @@ -526,8 +532,10 @@ msgstr[0] "" """ % {'version': VERSION, 'date': format_datetime(datetime.now(LOCALTZ), 'yyyy-MM-dd HH:mmZ', - tzinfo=LOCALTZ, locale='ja_JP')}, - open(po_file, 'U').read()) + tzinfo=LOCALTZ, locale='ja_JP')} + with open(po_file, 'U') as f: + actual_content = f.read() + self.assertEqual(expected_content, actual_content) def test_supports_no_wrap(self): self.cmd.input_file = 'project/i18n/long_messages.pot' @@ -536,9 +544,11 @@ msgstr[0] "" long_message = '"'+ 'xxxxx '*15 + '"' - pot_contents = open('project/i18n/messages.pot', 'U').read() + with open('project/i18n/messages.pot', 'U') as f: + pot_contents = f.read() pot_with_very_long_line = pot_contents.replace('"bar"', long_message) - open(self.cmd.input_file, 'wb').write(pot_with_very_long_line) + with open(self.cmd.input_file, 'wb') as f: + f.write(pot_with_very_long_line) self.cmd.no_wrap = True self.cmd.finalize_options() @@ -546,7 +556,7 @@ msgstr[0] "" po_file = self._po_file('en_US') assert os.path.isfile(po_file) - self.assertEqual(r"""# English (United States) translations for TestProject. + expected_content = r"""# English (United States) translations for TestProject. # Copyright (C) 2007 FooBar, Inc. # This file is distributed under the same license as the TestProject # project. @@ -581,8 +591,10 @@ 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()) + 'long_message': long_message} + with open(po_file, 'U') as f: + actual_content = f.read() + self.assertEqual(expected_content, actual_content) def test_supports_width(self): self.cmd.input_file = 'project/i18n/long_messages.pot' @@ -591,17 +603,18 @@ msgstr[1] "" long_message = '"'+ 'xxxxx '*15 + '"' - pot_contents = open('project/i18n/messages.pot', 'U').read() + with open('project/i18n/messages.pot', 'U') as f: + pot_contents = f.read() pot_with_very_long_line = pot_contents.replace('"bar"', long_message) - open(self.cmd.input_file, 'wb').write(pot_with_very_long_line) + with open(self.cmd.input_file, 'wb') as f: + f.write(pot_with_very_long_line) self.cmd.width = 120 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. + expected_content = r"""# English (United States) translations for TestProject. # Copyright (C) 2007 FooBar, Inc. # This file is distributed under the same license as the TestProject # project. @@ -636,8 +649,10 @@ 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()) + 'long_message': long_message} + with open(po_file, 'U') as f: + actual_content = f.read() + self.assertEqual(expected_content, actual_content) class CommandLineInterfaceTestCase(unittest.TestCase): @@ -753,8 +768,7 @@ commands: '-c', 'TRANSLATOR', '-c', 'TRANSLATORS:', '-o', pot_file, 'project']) self.assert_pot_file_exists() - self.assertEqual( -r"""# Translations template for TestProject. + expected_content = r"""# Translations template for TestProject. # Copyright (C) %(year)s FooBar, Inc. # This file is distributed under the same license as the TestProject # project. @@ -795,8 +809,10 @@ msgstr[1] "" """ % {'version': VERSION, 'year': time.strftime('%Y'), 'date': format_datetime(datetime.now(LOCALTZ), 'yyyy-MM-dd HH:mmZ', - tzinfo=LOCALTZ, locale='en')}, - open(pot_file, 'U').read()) + tzinfo=LOCALTZ, locale='en')} + with open(pot_file, 'U') as f: + actual_content = f.read() + self.assertEqual(expected_content, actual_content) def test_extract_with_mapping_file(self): pot_file = self._pot_file() @@ -808,8 +824,7 @@ msgstr[1] "" '-c', 'TRANSLATOR', '-c', 'TRANSLATORS:', '-o', pot_file, 'project']) self.assert_pot_file_exists() - self.assertEqual( -r"""# Translations template for TestProject. + expected_content = r"""# Translations template for TestProject. # Copyright (C) %(year)s FooBar, Inc. # This file is distributed under the same license as the TestProject # project. @@ -844,8 +859,10 @@ msgstr[1] "" """ % {'version': VERSION, 'year': time.strftime('%Y'), 'date': format_datetime(datetime.now(LOCALTZ), 'yyyy-MM-dd HH:mmZ', - tzinfo=LOCALTZ, locale='en')}, - open(pot_file, 'U').read()) + tzinfo=LOCALTZ, locale='en')} + with open(pot_file, 'U') as f: + actual_content = f.read() + self.assertEqual(expected_content, actual_content) def test_init_with_output_dir(self): po_file = self._po_file('en_US') @@ -854,8 +871,7 @@ msgstr[1] "" '-d', os.path.join(self._i18n_dir()), '-i', os.path.join(self._i18n_dir(), 'messages.pot')]) assert os.path.isfile(po_file) - self.assertEqual( -r"""# English (United States) translations for TestProject. + expected_content = r"""# English (United States) translations for TestProject. # Copyright (C) 2007 FooBar, Inc. # This file is distributed under the same license as the TestProject # project. @@ -890,8 +906,10 @@ msgstr[1] "" """ % {'version': VERSION, 'date': format_datetime(datetime.now(LOCALTZ), 'yyyy-MM-dd HH:mmZ', - tzinfo=LOCALTZ, locale='en')}, - open(po_file, 'U').read()) + tzinfo=LOCALTZ, locale='en')} + with open(po_file, 'U') as f: + actual_content = f.read() + self.assertEqual(expected_content, actual_content) def _i18n_dir(self): return os.path.join(self.datadir, 'project', 'i18n') @@ -903,8 +921,7 @@ msgstr[1] "" '-d', os.path.join(self._i18n_dir()), '-i', os.path.join(self._i18n_dir(), 'messages.pot')]) assert os.path.isfile(po_file) - self.assertEqual( -r"""# Japanese (Japan) translations for TestProject. + expected_content = r"""# Japanese (Japan) translations for TestProject. # Copyright (C) 2007 FooBar, Inc. # This file is distributed under the same license as the TestProject # project. @@ -938,8 +955,10 @@ msgstr[0] "" """ % {'version': VERSION, 'date': format_datetime(datetime.now(LOCALTZ), 'yyyy-MM-dd HH:mmZ', - tzinfo=LOCALTZ, locale='en')}, - open(po_file, 'U').read()) + tzinfo=LOCALTZ, locale='en')} + with open(po_file, 'U') as f: + actual_content = f.read() + self.assertEqual(expected_content, actual_content) def test_init_more_than_2_plural_forms(self): po_file = self._po_file('lv_LV') @@ -948,8 +967,7 @@ msgstr[0] "" '-d', self._i18n_dir(), '-i', os.path.join(self._i18n_dir(), 'messages.pot')]) assert os.path.isfile(po_file) - self.assertEqual( -r"""# Latvian (Latvia) translations for TestProject. + expected_content = r"""# Latvian (Latvia) translations for TestProject. # Copyright (C) 2007 FooBar, Inc. # This file is distributed under the same license as the TestProject # project. @@ -986,8 +1004,10 @@ msgstr[2] "" """ % {'version': VERSION, 'date': format_datetime(datetime.now(LOCALTZ), 'yyyy-MM-dd HH:mmZ', - tzinfo=LOCALTZ, locale='en')}, - open(po_file, 'U').read()) + tzinfo=LOCALTZ, locale='en')} + with open(po_file, 'U') as f: + actual_content = f.read() + self.assertEqual(expected_content, actual_content) def test_compile_catalog(self): po_file = self._po_file('de_DE') diff --git a/babel/support.py b/babel/support.py index ba45ad9..fb6d3ad 100644 --- a/babel/support.py +++ b/babel/support.py @@ -558,7 +558,8 @@ class Translations(NullTranslations, gettext.GNUTranslations): filename = gettext.find(domain, dirname, locales) if not filename: return NullTranslations() - return cls(fp=open(filename, 'rb'), domain=domain) + with open(filename, 'rb') as fp: + return cls(fp=fp, domain=domain) def __repr__(self): return '<%s: "%s">' % (type(self).__name__, diff --git a/babel/tests/support.py b/babel/tests/support.py index 00b8f18..f2e4796 100644 --- a/babel/tests/support.py +++ b/babel/tests/support.py @@ -173,7 +173,8 @@ class TranslationsTestCase(unittest.TestCase): os.makedirs(messages_dir) catalog = Catalog(locale='fr', domain='messages') catalog.add('foo', 'bar') - write_mo(file(os.path.join(messages_dir, 'messages.mo'), 'wb'), catalog) + with open(os.path.join(messages_dir, 'messages.mo'), 'wb') as f: + write_mo(f, catalog) translations = support.Translations.load(tempdir, locales=('fr',), domain='messages') self.assertEqual('bar', translations.gettext('foo')) |