for idx, (locale, po_file) in enumerate(po_files):
mo_file = mo_files[idx]
- infile = open(po_file, 'rb')
- try:
+ with open(po_file, 'rb') as infile:
catalog = read_po(infile, locale)
- finally:
- infile.close()
if self.statistics:
translated = 0
self.log.info('compiling catalog %s to %s', po_file, mo_file)
- outfile = open(mo_file, 'wb')
- try:
+ with open(mo_file, 'wb') as outfile:
write_mo(outfile, catalog, use_fuzzy=self.use_fuzzy)
- finally:
- outfile.close()
class extract_messages(Command):
mappings = []
if self.mapping_file:
- fileobj = open(self.mapping_file, 'U')
- try:
+ with open(self.mapping_file, 'U') as fileobj:
method_map, options_map = parse_mapping(fileobj)
- finally:
- fileobj.close()
for path in self.input_paths:
mappings.append((path, method_map, options_map))
'creating catalog %s based on %s', self.output_file, self.input_file
)
- infile = open(self.input_file, 'rb')
- try:
+ with open(self.input_file, 'rb') as infile:
# Although reading from the catalog template, read_po must be fed
# the locale in order to correctly calculate plurals
catalog = read_po(infile, locale=self.locale)
- finally:
- infile.close()
catalog.locale = self._locale
catalog.revision_date = datetime.now(LOCALTZ)
catalog.fuzzy = False
- outfile = open(self.output_file, 'wb')
- try:
+ with open(self.output_file, 'wb') as outfile:
write_po(outfile, catalog, width=self.width)
- finally:
- outfile.close()
class update_catalog(Command):
if not domain:
domain = os.path.splitext(os.path.basename(self.input_file))[0]
- infile = open(self.input_file, 'rb')
- try:
+ with open(self.input_file, 'rb') as infile:
template = read_po(infile)
- finally:
- infile.close()
if not po_files:
raise DistutilsOptionError('no message catalogs found')
for locale, filename in po_files:
self.log.info('updating catalog %s based on %s', filename, self.input_file)
- infile = open(filename, 'rb')
- try:
+ with open(filename, 'rb') as infile:
catalog = read_po(infile, locale=locale, domain=domain)
- finally:
- infile.close()
catalog.update(
template, self.no_fuzzy_matching,
tmpname = os.path.join(os.path.dirname(filename),
tempfile.gettempprefix() +
os.path.basename(filename))
- tmpfile = open(tmpname, 'wb')
try:
- try:
+ with open(tmpname, 'wb') as tmpfile:
write_po(tmpfile, catalog,
ignore_obsolete=self.ignore_obsolete,
include_previous=self.previous, width=self.width)
- finally:
- tmpfile.close()
except:
os.remove(tmpname)
raise
def test_basics(self):
mo_path = os.path.join(self.datadir, 'project', 'i18n', 'de',
'LC_MESSAGES', 'messages.mo')
- mo_file = open(mo_path, 'rb')
- try:
+ with open(mo_path, 'rb') as mo_file:
catalog = mofile.read_mo(mo_file)
self.assertEqual(2, len(catalog))
self.assertEqual('TestProject', catalog.project)
self.assertEqual('Stange', catalog['bar'].string)
self.assertEqual(['Fuhstange', 'Fuhstangen'],
catalog['foobar'].string)
- finally:
- mo_file.close()
class WriteMoTestCase(unittest.TestCase):