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.

close()[source]

Remove all produced temporary files.

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:
Returns:

An opened file

Return type:

io.IOBase

write()[source]

Finalize writing all open files by moving the created temporary files to their final destinations.

Existing file destinations will be backed up according to the Gromacs scheme.

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:
Returns:

An opened file

Return type:

io.IOBase