XML parser: perform atomic save by moving a temporary file after the serialization
This commit is contained in:
parent
0e76a6ed2a
commit
92895a7b1d
1 changed files with 13 additions and 2 deletions
|
@ -222,10 +222,21 @@ class ModuleState:
|
||||||
"XML node: %s with %s", self.name, attrs)
|
"XML node: %s with %s", self.name, attrs)
|
||||||
|
|
||||||
def save(self, filename):
|
def save(self, filename):
|
||||||
"""Save the current node as root node in a XML file"""
|
"""Save the current node as root node in a XML file
|
||||||
with open(filename, "w") as f:
|
|
||||||
|
Argument:
|
||||||
|
filename -- location of the file to create/erase
|
||||||
|
"""
|
||||||
|
|
||||||
|
import tempfile
|
||||||
|
_, tmpath = tempfile.mkstemp()
|
||||||
|
with open(tmpath, "w") as f:
|
||||||
import xml.sax.saxutils
|
import xml.sax.saxutils
|
||||||
gen = xml.sax.saxutils.XMLGenerator(f, "utf-8")
|
gen = xml.sax.saxutils.XMLGenerator(f, "utf-8")
|
||||||
gen.startDocument()
|
gen.startDocument()
|
||||||
self.save_node(gen)
|
self.save_node(gen)
|
||||||
gen.endDocument()
|
gen.endDocument()
|
||||||
|
|
||||||
|
# Atomic save
|
||||||
|
import shutil
|
||||||
|
shutil.move(tmpath, filename)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue