XML parser: perform atomic save by moving a temporary file after the serialization

This commit is contained in:
nemunaire 2015-06-14 19:34:04 +02:00
parent 0e76a6ed2a
commit 92895a7b1d

View file

@ -222,10 +222,21 @@ class ModuleState:
"XML node: %s with %s", self.name, attrs)
def save(self, filename):
"""Save the current node as root node in a XML file"""
with open(filename, "w") as f:
"""Save the current node as root node in a XML file
Argument:
filename -- location of the file to create/erase
"""
import tempfile
_, tmpath = tempfile.mkstemp()
with open(tmpath, "w") as f:
import xml.sax.saxutils
gen = xml.sax.saxutils.XMLGenerator(f, "utf-8")
gen.startDocument()
self.save_node(gen)
gen.endDocument()
# Atomic save
import shutil
shutil.move(tmpath, filename)