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): ...@@ -146,9 +146,8 @@ class Template(object):
def from_filename(cls, filename, namespace=None, encoding=None, def from_filename(cls, filename, namespace=None, encoding=None,
default_inherit=None, get_template=get_file_template): default_inherit=None, get_template=get_file_template):
f = open(filename, 'rb') with open(filename, 'rb') as f:
c = f.read() c = f.read()
f.close()
if encoding: if encoding:
c = c.decode(encoding) c = c.decode(encoding)
return cls(content=c, name=filename, namespace=namespace, return cls(content=c, name=filename, namespace=namespace,
...@@ -1164,9 +1163,8 @@ def fill_command(args=None): ...@@ -1164,9 +1163,8 @@ def fill_command(args=None):
template_content = sys.stdin.read() template_content = sys.stdin.read()
template_name = '<stdin>' template_name = '<stdin>'
else: else:
f = open(template_name, 'rb') with open(template_name, 'rb') as f:
template_content = f.read() template_content = f.read()
f.close()
if options.use_html: if options.use_html:
TemplateClass = HTMLTemplate TemplateClass = HTMLTemplate
else: else:
...@@ -1174,9 +1172,8 @@ def fill_command(args=None): ...@@ -1174,9 +1172,8 @@ def fill_command(args=None):
template = TemplateClass(template_content, name=template_name) template = TemplateClass(template_content, name=template_name)
result = template.substitute(vars) result = template.substitute(vars)
if options.output: if options.output:
f = open(options.output, 'wb') with open(options.output, 'wb') as f:
f.write(result) f.write(result)
f.close()
else: else:
sys.stdout.write(result) 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