Support YAML as an output format.

Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
This commit is contained in:
brian m. carlson 2013-11-16 21:42:17 +00:00
parent d21b309ca8
commit 9f030febbf
No known key found for this signature in database
GPG key ID: BF535D811F52F68B

View file

@ -421,6 +421,17 @@ class JSONFile(FileFormat):
"(unknown dict)")
return o
class YAMLFile(JSONFile):
"""A serializer for YAML.
Since by default the JSON output by JSONEncoder is valid YAML, this
serializer works by outputting JSON. However, loading YAML is not
supported, since that would require a full YAML parser (and has security
implications).
"""
def load(self):
raise NotImplementedError("Loading arbitrary YAML is not supported.")
class FileStorage:
"""A file (or file-like object) in a certain format."""
def __init__(self, fmt, filename, txnformat=None, options=None):
@ -433,6 +444,8 @@ class FileStorage:
self._backend = PickleFile(filename, self._txn, options)
elif fmt == "json":
self._backend = JSONFile(filename, self._txn, options)
elif fmt == "yaml":
self._backend = YAMLFile(filename, self._txn, options)
else:
raise ValueError("{0}: not a supported backend".format(fmt))
@staticmethod