pylint fix: Use 'with' on file handle
This commit is contained in:
@@ -41,18 +41,14 @@ class Dumper(VPPApi):
|
|||||||
|
|
||||||
def write(self, outfile):
|
def write(self, outfile):
|
||||||
"""Emit the configuration to either stdout (outfile=='-') or a filename"""
|
"""Emit the configuration to either stdout (outfile=='-') or a filename"""
|
||||||
|
config = self.cache_to_config()
|
||||||
if outfile and outfile == "-":
|
if outfile and outfile == "-":
|
||||||
file = sys.stdout
|
file = sys.stdout
|
||||||
outfile = "(stdout)"
|
outfile = "(stdout)"
|
||||||
|
print(yaml.dump(config), file=file)
|
||||||
else:
|
else:
|
||||||
file = open(outfile, "w", encoding="utf-8")
|
with open(outfile, "w", encoding="utf-8") as file:
|
||||||
|
print(yaml.dump(config), file=file)
|
||||||
config = self.cache_to_config()
|
|
||||||
|
|
||||||
print(yaml.dump(config), file=file)
|
|
||||||
|
|
||||||
if file is not sys.stdout:
|
|
||||||
file.close()
|
|
||||||
self.logger.info(f"Wrote YAML config to {outfile}")
|
self.logger.info(f"Wrote YAML config to {outfile}")
|
||||||
|
|
||||||
def cache_to_config(self):
|
def cache_to_config(self):
|
||||||
|
|||||||
@@ -1530,11 +1530,11 @@ class Reconciler:
|
|||||||
if outfile and outfile == "-":
|
if outfile and outfile == "-":
|
||||||
file = sys.stdout
|
file = sys.stdout
|
||||||
outfile = "(stdout)"
|
outfile = "(stdout)"
|
||||||
|
if len(output) > 0:
|
||||||
|
print("\n".join(output), file=file)
|
||||||
else:
|
else:
|
||||||
file = open(outfile, "w", encoding="utf-8")
|
with open(outfile, "w", encoding="utf-8") as file:
|
||||||
if len(output) > 0:
|
if len(output) > 0:
|
||||||
print("\n".join(output), file=file)
|
print("\n".join(output), file=file)
|
||||||
if file is not sys.stdout:
|
|
||||||
file.close()
|
|
||||||
|
|
||||||
self.logger.info(f"Wrote {len(output)} lines to {outfile}")
|
self.logger.info(f"Wrote {len(output)} lines to {outfile}")
|
||||||
|
|||||||
Reference in New Issue
Block a user