Created
February 17, 2012 08:35
-
-
Save nskeip/1851845 to your computer and use it in GitHub Desktop.
Atomic wrapper for ElementFlow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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