Commit c2f4cced authored by Stefan Behnel's avatar Stefan Behnel

Minor code modernisations in Tempita module to use the with-statement.

parent f1eaa9c1
......@@ -146,9 +146,8 @@ class Template(object):
def from_filename(cls, filename, namespace=None, encoding=None,
default_inherit=None, get_template=get_file_template):
f = open(filename, 'rb')
c = f.read()
f.close()
with open(filename, 'rb') as f:
c = f.read()
if encoding:
c = c.decode(encoding)
return cls(content=c, name=filename, namespace=namespace,
......@@ -1164,9 +1163,8 @@ def fill_command(args=None):
template_content = sys.stdin.read()
template_name = '<stdin>'
else:
f = open(template_name, 'rb')
template_content = f.read()
f.close()
with open(template_name, 'rb') as f:
template_content = f.read()
if options.use_html:
TemplateClass = HTMLTemplate
else:
......@@ -1174,9 +1172,8 @@ def fill_command(args=None):
template = TemplateClass(template_content, name=template_name)
result = template.substitute(vars)
if options.output:
f = open(options.output, 'wb')
f.write(result)
f.close()
with open(options.output, 'wb') as f:
f.write(result)
else:
sys.stdout.write(result)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment