vermouth.file_writer module¶
Provides the DeferredFileWriter, which allow writing of files without affecting existing files, until it is clear the written changes are correct.
- class vermouth.file_writer.DeferredFileWriter(*args, **kwargs)[source]¶
Bases:
object
A singleton class/object that is intended to prevent writing output to files that is invalid, due to e.g. warnings further down the pipeline.
If this class is used to open a file for writing, a temporary file is created and returned instead. Once it’s clear the output produced is valid the
write()
method can be used to finalize the written changes by moving them to their intended destination. If a file with that name already exists it is backed up according to the Gromacs scheme.- open(filename, mode='r', *args, **kwargs)[source]¶
If mode is either ‘w’ or ‘a’, opens and returns a handle to a temporary file. If mode is ‘r’ opens and returns a handle to the file specified.
Once
write()
is called the changes written to all files opened this way are propagated to their final destination.- Parameters:
filename (os.PathLike) – The final name of the file to be opened.
mode (str) – The mode in which the file is to be opened.
*args (collections.abc.Iterable) – Passed to
os.fdopen()
.**kwargs (dict) – Passed to
os.fdopen()
.
- Returns:
An opened file
- Return type:
- class vermouth.file_writer.Singleton[source]¶
Bases:
type
Metaclass for creating singleton objects. Taken from [1].
- vermouth.file_writer.deferred_open(filename, mode='r', *args, **kwargs)¶
If mode is either ‘w’ or ‘a’, opens and returns a handle to a temporary file. If mode is ‘r’ opens and returns a handle to the file specified.
Once
write()
is called the changes written to all files opened this way are propagated to their final destination.- Parameters:
filename (os.PathLike) – The final name of the file to be opened.
mode (str) – The mode in which the file is to be opened.
*args (collections.abc.Iterable) – Passed to
os.fdopen()
.**kwargs (dict) – Passed to
os.fdopen()
.
- Returns:
An opened file
- Return type: