Add hooke.util.yaml.data_constructor to rebuild hooke.curve.Data.info.
authorW. Trevor King <wking@drexel.edu>
Sun, 22 Aug 2010 13:34:37 +0000 (09:34 -0400)
committerW. Trevor King <wking@drexel.edu>
Sun, 22 Aug 2010 13:34:37 +0000 (09:34 -0400)
hooke/util/yaml.py

index e7f99806a49c6382716f49764d8ac72e3d1092f1..46d6e2a73963c7dccf3eaea22a3aadcc916556c0 100644 (file)
@@ -54,12 +54,16 @@ import types
 import numpy
 import yaml
 import yaml.constructor
+from yaml.constructor import ConstructorError
 import yaml.representer
 
 from ..curve import Data, Curve
 from ..playlist import FilePlaylist
 
 
+DATA_INFO_TAG = u'!hooke.curve.DataInfo'
+
+
 if False: # YAML dump debugging code
     """To help isolate data types etc. that give YAML problems.
 
@@ -120,9 +124,14 @@ def data_representer(dumper, data):
     for key in info.keys():
         if key.startswith('raw '):
             del(info[key])
-    return dumper.represent_mapping(u'!hooke.curve.DataInfo', info)
+    return dumper.represent_mapping(DATA_INFO_TAG, info)
 yaml.add_representer(Data, data_representer)
 
+def data_constructor(loader, node):
+    info = loader.construct_mapping(node)
+    return Data(shape=(0,0), dtype=numpy.float32, info=info)
+yaml.add_constructor(DATA_INFO_TAG, data_constructor)
+
 def object_representer(dumper, data):
     cls = type(data)
     if cls in copy_reg.dispatch_table: