Add a RawFile object.

This provides support for storing raw data under the transactions
infrastructure.

Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
This commit is contained in:
brian m. carlson 2014-01-10 00:24:00 +00:00
parent b3b76c7a57
commit e514e62747
No known key found for this signature in database
GPG key ID: BF535D811F52F68B

View file

@ -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