OpenQuizz
Une application de gestion des contenus pédagogiques
|
Public Member Functions | |
def | __init__ (self, filepath) |
def | __del__ (self) |
def | open (self, write=False, stream=False) |
def | commit (self) |
def | rollback (self) |
This class facilitates a safe read and write operation to a file on disk. If we write to 'file', we obtain a lock file at 'file.lock' and write to that instead. If we succeed, the lock file will be renamed to overwrite the original file. When reading, we obtain a lock file, but to prevent other writers from succeeding while we are reading the file. This type handles error correctly in that it will assure a consistent state on destruction. **note** with this setup, parallel reading is not possible
def __init__ | ( | self, | |
filepath | |||
) |
Initialize an instance with the givne filepath
def __del__ | ( | self | ) |
def commit | ( | self | ) |
When done writing, call this function to commit your changes into the actual file. The file descriptor will be closed, and the lockfile handled. **Note** can be called multiple times
def open | ( | self, | |
write = False , |
|||
stream = False |
|||
) |
Open the file descriptor for reading or writing, both in binary mode. :param write: if True, the file descriptor will be opened for writing. Other wise it will be opened read-only. :param stream: if True, the file descriptor will be wrapped into a simple stream object which supports only reading or writing :return: fd to read from or write to. It is still maintained by this instance and must not be closed directly :raise IOError: if the lock could not be retrieved :raise OSError: If the actual file could not be opened for reading **note** must only be called once
def rollback | ( | self | ) |
Abort your operation without any changes. The file descriptor will be closed, and the lock released. **Note** can be called multiple times