utils: Handle exceptions in context management for YAMLFile

When an exception is raised within the context, the YAML file should not
written.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa authored and James Valleroy committed 2019-02-18 21:51:20 -05:00
1 parent df76e6afa4
commit d3bdaf0729
2 files changed
+14 -3

No files matched your search

+4 -3
View File
@@ -108,9 +108,10 @@ class YAMLFile(object):
return self.conf
def __exit__(self, typ, value, traceback):
with open(self.yaml_file, 'w') as intro_conf:
ruamel.yaml.round_trip_dump(self.conf, intro_conf)
def __exit__(self, type_, value, traceback):
if not traceback:
with open(self.yaml_file, 'w') as intro_conf:
ruamel.yaml.round_trip_dump(self.conf, intro_conf)
def is_file_empty(self):
return os.stat(self.yaml_file).st_size == 0