summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVille Skyttä <ville.skytta@iki.fi>2016-07-26 23:31:43 +0300
committerVille Skyttä <ville.skytta@iki.fi>2016-07-27 02:23:13 +0300
commit2b37db836f0a7bc253fd2e4a8dd5437aa6dee44e (patch)
tree073e9755ec7dfcbf7abeca3dec16d8135c4d98ac
parent888f65545a2fa0841d7592f44463b09a7a28f6eb (diff)
downloadbabel-2b37db836f0a7bc253fd2e4a8dd5437aa6dee44e.tar.gz
Handle file open/close with "with"
-rw-r--r--babel/core.py5
-rw-r--r--babel/localedata.py5
-rw-r--r--babel/messages/extract.py5
-rw-r--r--babel/messages/frontend.py40
-rwxr-xr-xscripts/download_import_cldr.py6
-rwxr-xr-xscripts/dump_global.py5
-rw-r--r--tests/messages/test_mofile.py5
7 files changed, 16 insertions, 55 deletions
diff --git a/babel/core.py b/babel/core.py
index e6f432c..0b7b4ed 100644
--- a/babel/core.py
+++ b/babel/core.py
@@ -71,11 +71,8 @@ def get_global(key):
filename = os.path.join(dirname, 'global.dat')
if not os.path.isfile(filename):
_raise_no_data_error()
- fileobj = open(filename, 'rb')
- try:
+ with open(filename, 'rb') as fileobj:
_global_data = pickle.load(fileobj)
- finally:
- fileobj.close()
return _global_data.get(key, {})
diff --git a/babel/localedata.py b/babel/localedata.py
index 87981fe..9e272a2 100644
--- a/babel/localedata.py
+++ b/babel/localedata.py
@@ -106,14 +106,11 @@ def load(name, merge_inherited=True):
parent = '_'.join(parts[:-1])
data = load(parent).copy()
filename = os.path.join(_dirname, '%s.dat' % name)
- fileobj = open(filename, 'rb')
- try:
+ with open(filename, 'rb') as fileobj:
if name != 'root' and merge_inherited:
merge(data, pickle.load(fileobj))
else:
data = pickle.load(fileobj)
- finally:
- fileobj.close()
_cache[name] = data
return data
finally:
diff --git a/babel/messages/extract.py b/babel/messages/extract.py
index db17848..1aade43 100644
--- a/babel/messages/extract.py
+++ b/babel/messages/extract.py
@@ -230,12 +230,9 @@ def extract_from_file(method, filename, keywords=DEFAULT_KEYWORDS,
tags to be removed from the collected comments.
:param options: a dictionary of additional options (optional)
"""
- fileobj = open(filename, 'rb')
- try:
+ with open(filename, 'rb') as fileobj:
return list(extract(method, fileobj, keywords, comment_tags, options,
strip_comment_tags))
- finally:
- fileobj.close()
def extract(method, fileobj, keywords=DEFAULT_KEYWORDS, comment_tags=(),
diff --git a/babel/messages/frontend.py b/babel/messages/frontend.py
index a4e9c2e..5a1c741 100644
--- a/babel/messages/frontend.py
+++ b/babel/messages/frontend.py
@@ -213,11 +213,8 @@ class compile_catalog(Command):
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
@@ -244,11 +241,8 @@ class compile_catalog(Command):
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):
@@ -471,11 +465,8 @@ 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))
@@ -591,23 +582,17 @@ class init_catalog(Command):
'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):
@@ -709,22 +694,16 @@ 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,
@@ -734,14 +713,11 @@ class update_catalog(Command):
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
diff --git a/scripts/download_import_cldr.py b/scripts/download_import_cldr.py
index bfcab5f..4cc6a4b 100755
--- a/scripts/download_import_cldr.py
+++ b/scripts/download_import_cldr.py
@@ -1,5 +1,6 @@
#!/usr/bin/env python
+import contextlib
import os
import sys
import shutil
@@ -90,9 +91,8 @@ def main():
shutil.rmtree(common_path)
log('Extracting CLDR to \'%s\'', cldr_path)
- z = zipfile.ZipFile(zip_path)
- z.extractall(cldr_path)
- z.close()
+ with contextlib.closing(zipfile.ZipFile(zip_path)) as z:
+ z.extractall(cldr_path)
subprocess.check_call([
sys.executable,
diff --git a/scripts/dump_global.py b/scripts/dump_global.py
index ad6c15e..2970bc2 100755
--- a/scripts/dump_global.py
+++ b/scripts/dump_global.py
@@ -21,11 +21,8 @@ import babel
dirname = os.path.join(os.path.dirname(babel.__file__))
filename = os.path.join(dirname, 'global.dat')
-fileobj = open(filename, 'rb')
-try:
+with open(filename, 'rb') as fileobj:
data = pickle.load(fileobj)
-finally:
- fileobj.close()
if len(sys.argv) > 1:
pprint(data.get(sys.argv[1]))
diff --git a/tests/messages/test_mofile.py b/tests/messages/test_mofile.py
index d257c5d..5fedc60 100644
--- a/tests/messages/test_mofile.py
+++ b/tests/messages/test_mofile.py
@@ -27,8 +27,7 @@ class ReadMoTestCase(unittest.TestCase):
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)
@@ -36,8 +35,6 @@ class ReadMoTestCase(unittest.TestCase):
self.assertEqual('Stange', catalog['bar'].string)
self.assertEqual(['Fuhstange', 'Fuhstangen'],
catalog['foobar'].string)
- finally:
- mo_file.close()
class WriteMoTestCase(unittest.TestCase):