From 9f030febbfc528a6c14b43fed42b1e43155ce9d2 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sat, 16 Nov 2013 21:42:17 +0000 Subject: [PATCH] Support YAML as an output format. Signed-off-by: brian m. carlson --- lib/newfol/filemanip.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/newfol/filemanip.py b/lib/newfol/filemanip.py index bb8a4e5..37e29bf 100644 --- a/lib/newfol/filemanip.py +++ b/lib/newfol/filemanip.py @@ -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