diff --git a/vppcfg/vpp/dumper.py b/vppcfg/vpp/dumper.py index 52d5788..2ac82aa 100644 --- a/vppcfg/vpp/dumper.py +++ b/vppcfg/vpp/dumper.py @@ -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): diff --git a/vppcfg/vpp/reconciler.py b/vppcfg/vpp/reconciler.py index 500424f..55b60e7 100644 --- a/vppcfg/vpp/reconciler.py +++ b/vppcfg/vpp/reconciler.py @@ -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}")