Skip to content

Instantly share code, notes, and snippets.

@nskeip
Created February 17, 2012 08:35
Show Gist options
  • Save nskeip/1851845 to your computer and use it in GitHub Desktop.
Save nskeip/1851845 to your computer and use it in GitHub Desktop.
Atomic wrapper for ElementFlow
# If you create xml via ElementFlow and make some bad things (and exceptions raise)
# you will get invalid xml (only a criple part of the xml you wish to have).
#
# This is a wrapper for ElementFlow for 'atomic' xml: it either created or not.
import shutil
import tempfile
class ElementFlowAtomicOutputWrapper(object):
def __init__(self, out_file_path):
self.out_file_path = out_file_path
def __enter__(self):
self.temp_file = tempfile.NamedTemporaryFile()
return self.temp_file
def __exit__(self, type, value, traceback):
self.temp_file.flush()
shutil.copyfile(self.temp_file.name, self.out_file_path)
## Can be used like this:
# with AtomicOutputWrapper('/path/to/file') as f:
# with elementflow.xml(f, u'some_tree') as xml:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment