source: pyyaml/trunk/lib/yaml/yaml_object.py @ 133

Revision 133, 1.0 KB checked in by xi, 9 years ago (diff)

Implement yaml.dump().

Line 
1
2__all__ = ['YAMLObject', 'YAMLObjectMetaclass']
3
4from constructor import *
5from representer import *
6
7class YAMLObjectMetaclass(type):
8
9    def __init__(cls, name, bases, kwds):
10        super(YAMLObjectMetaclass, cls).__init__(name, bases, kwds)
11        if 'yaml_tag' in kwds and kwds['yaml_tag'] is not None:
12            cls.yaml_constructor.add_constructor(cls.yaml_tag, cls.from_yaml)
13            cls.yaml_representer.add_representer(cls, cls.to_yaml)
14
15class YAMLObject(object):
16
17    __metaclass__ = YAMLObjectMetaclass
18
19    yaml_constructor = Constructor
20    yaml_representer = Representer
21
22    yaml_tag = None
23
24    def from_yaml(cls, constructor, node):
25        raise ConstructorError(None, None,
26                "found undefined constructor for the tag %r"
27                % node.tag.encode('utf-8'), node.start_mark)
28    from_yaml = classmethod(from_yaml)
29
30    def to_yaml(cls, representer, native):
31        raise RepresenterError(
32                "found undefined representer for the object: %s" % native)
33    to_yaml = classmethod(to_yaml)
34
Note: See TracBrowser for help on using the repository browser.