diff options
author | Ville Skyttä <ville.skytta@iki.fi> | 2016-07-26 23:31:43 +0300 |
---|---|---|
committer | Ville Skyttä <ville.skytta@iki.fi> | 2016-07-27 02:23:13 +0300 |
commit | 2b37db836f0a7bc253fd2e4a8dd5437aa6dee44e (patch) | |
tree | 073e9755ec7dfcbf7abeca3dec16d8135c4d98ac /babel | |
parent | 888f65545a2fa0841d7592f44463b09a7a28f6eb (diff) | |
download | babel-2b37db836f0a7bc253fd2e4a8dd5437aa6dee44e.tar.gz |
Handle file open/close with "with"
Diffstat (limited to 'babel')
-rw-r--r-- | babel/core.py | 5 | ||||
-rw-r--r-- | babel/localedata.py | 5 | ||||
-rw-r--r-- | babel/messages/extract.py | 5 | ||||
-rw-r--r-- | babel/messages/frontend.py | 40 |
4 files changed, 11 insertions, 44 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 |