Convert from "print ..." to "print(...)"
[hooke.git] / hooke / util / yaml.py
index e7f99806a49c6382716f49764d8ac72e3d1092f1..956db4153ffd5c5f0ca36afad8f3e3eba52b8386 100644 (file)
@@ -1,4 +1,19 @@
-# Copyright
+# Copyright (C) 2010-2012 W. Trevor King <wking@tremily.us>
+#
+# This file is part of Hooke.
+#
+# Hooke is free software: you can redistribute it and/or modify it under the
+# terms of the GNU Lesser General Public License as published by the Free
+# Software Foundation, either version 3 of the License, or (at your option) any
+# later version.
+#
+# Hooke is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+# A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
+# details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with Hooke.  If not, see <http://www.gnu.org/licenses/>.
 
 """Add representers to YAML to support Hooke.
 
@@ -9,7 +24,7 @@ stored somewhere else or not important.
 
 >>> import yaml
 >>> a = numpy.array([1,2,3])
->>> print yaml.dump(a)
+>>> print(yaml.dump(a))
 null
 ...
 <BLANKLINE>
@@ -18,7 +33,7 @@ The default behavior is to crash.
 
 >>> yaml.Dumper.yaml_representers.pop(numpy.ndarray)  # doctest: +ELLIPSIS
 <function none_representer at 0x...>
->>> print yaml.dump(a)
+>>> print(yaml.dump(a))  # doctest: +REPORT_UDIFF
 !!python/object/apply:numpy.core.multiarray._reconstruct
 args:
 - !!python/name:numpy.ndarray ''
@@ -27,7 +42,9 @@ args:
 state: !!python/tuple
 - 1
 - !!python/tuple [3]
-- null
+- !!python/object/apply:numpy.dtype
+  args: [i4, 0, 1]
+  state: !!python/tuple [3, <, null, null, null, -1, -1, 0]
 - false
 - "\\x01\\0\\0\\0\\x02\\0\\0\\0\\x03\\0\\0\\0"
 <BLANKLINE>
@@ -44,6 +61,19 @@ Must be because of the other representers I've loaded since.
 Restore the representer for future tests.
 
 >>> yaml.add_representer(numpy.ndarray, none_representer)
+
+We also avoid !!python/unicode tags by sacrificing the string/unicode
+distinction.
+
+>>> yaml.dump('ascii', allow_unicode=True)
+'ascii\\n...\\n'
+>>> yaml.dump(u'ascii', allow_unicode=True)
+'ascii\\n...\\n'
+>>> a = yaml.dump(u'Fran\\xe7ois', allow_unicode=True)
+>>> a
+'Fran\\xc3\\xa7ois\\n...\\n'
+>>> unicode(a, 'utf-8')
+u'Fran\\xe7ois\\n...\\n'
 """
 
 from __future__ import absolute_import
@@ -54,12 +84,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.
 
@@ -67,7 +101,8 @@ if False: # YAML dump debugging code
     define new types (e.g. numpy.ndarray) which YAML cannot inspect.
     """
     def ignore_aliases(data):
-        print data, repr(data), type(data), repr(type(data))
+        print(' '.join(str(x) for x in [
+                    data, repr(data), type(data), repr(type(data))]))
         sys.stdout.flush()
         if data in [None, ()]:
             return True
@@ -91,11 +126,13 @@ else:
     yaml.representer.SafeRepresenter.ignore_aliases = staticmethod(
         ignore_aliases)
 
+def unicode_representer(dumper, data):
+    return dumper.represent_scalar(u'tag:yaml.org,2002:str', data)
+yaml.add_representer(unicode, unicode_representer)
 
 def none_representer(dumper, data):
     return dumper.represent_none(None)
 yaml.add_representer(numpy.ndarray, none_representer)
-yaml.add_representer(numpy.dtype, none_representer)
 
 def bool_representer(dumper, data):
     return dumper.represent_bool(data)
@@ -120,9 +157,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: