From e514e62747243a78dff650542204e45bcae188dc Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Fri, 10 Jan 2014 00:24:00 +0000 Subject: [PATCH] Add a RawFile object. This provides support for storing raw data under the transactions infrastructure. Signed-off-by: brian m. carlson --- lib/newfol/filemanip.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lib/newfol/filemanip.py b/lib/newfol/filemanip.py index 1fd7da8..03fa06a 100644 --- a/lib/newfol/filemanip.py +++ b/lib/newfol/filemanip.py @@ -424,6 +424,24 @@ class YAMLFile(JSONFile): def load(self): raise NotImplementedError("Loading arbitrary YAML is not supported.") +class RawFile(FileFormat): + """A raw data file. + + This file type stores raw data, not records. It is provided for wrapping + normal file IO in the transaction code. + """ + def store(self, records): + """Store the data to this file.""" + fp = self._open("w") + fp.write(records) + self._close(fp) + def load(self): + """Read the contents of this file.""" + fp = self._open("r") + recs = fp.read() + self._close(fp) + return recs + class FileStorage: """A file (or file-like object) in a certain format.""" def __init__(self, fmt, filename, txnformat=None, options=None): @@ -438,6 +456,8 @@ class FileStorage: self._backend = JSONFile(filename, self._txn, options) elif fmt == "yaml": self._backend = YAMLFile(filename, self._txn, options) + elif fmt == "raw": + self._backend = RawFile(filename, self._txn, options) else: raise ValueError("{0}: not a supported backend".format(fmt)) @staticmethod