| 1 | |
|---|
| 2 | __all__ = ['YAMLObject', 'YAMLObjectMetaclass'] |
|---|
| 3 | |
|---|
| 4 | from constructor import * |
|---|
| 5 | from representer import * |
|---|
| 6 | |
|---|
| 7 | class 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 | |
|---|
| 15 | class 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 | |
|---|