pylint fix: Use 'with' on file handle

This commit is contained in:
Pim van Pelt
2025-11-10 00:36:27 +01:00
parent ffa0a77f5e
commit 63585671dc
2 changed files with 9 additions and 13 deletions

View File

@@ -41,18 +41,14 @@ class Dumper(VPPApi):
def write(self, outfile):
"""Emit the configuration to either stdout (outfile=='-') or a filename"""
config = self.cache_to_config()
if outfile and outfile == "-":
file = sys.stdout
outfile = "(stdout)"
print(yaml.dump(config), file=file)
else:
file = open(outfile, "w", encoding="utf-8")
config = self.cache_to_config()
print(yaml.dump(config), file=file)
if file is not sys.stdout:
file.close()
with open(outfile, "w", encoding="utf-8") as file:
print(yaml.dump(config), file=file)
self.logger.info(f"Wrote YAML config to {outfile}")
def cache_to_config(self):

View File

@@ -1530,11 +1530,11 @@ class Reconciler:
if outfile and outfile == "-":
file = sys.stdout
outfile = "(stdout)"
if len(output) > 0:
print("\n".join(output), file=file)
else:
file = open(outfile, "w", encoding="utf-8")
if len(output) > 0:
print("\n".join(output), file=file)
if file is not sys.stdout:
file.close()
with open(outfile, "w", encoding="utf-8") as file:
if len(output) > 0:
print("\n".join(output), file=file)
self.logger.info(f"Wrote {len(output)} lines to {outfile}")