Ejemplo n.º 1
0
class FloatNode(float, Node):
    pass


class ListNode(list, Node):
    pass


class DictNode(OrderedDict, Node):
    pass


from yaml.representer import Representer
from yaml.constructor import Constructor
yaml_representer = Representer()
yaml_constructor = Constructor()


def yaml_to_node(yaml_obj):
    if isinstance(yaml_obj, yaml.MappingNode):
        v = [(yaml_to_node(k), yaml_to_node(v)) for k, v in yaml_obj.value]
        node = DictNode(v)
    elif isinstance(yaml_obj, yaml.SequenceNode):
        v = [yaml_to_node(v) for v in yaml_obj.value]
        node = ListNode(v)
    elif isinstance(yaml_obj, yaml.ScalarNode):
        # 'tag:yaml.org,2002:bool'
        type = yaml_obj.tag.split(':')[2]
        type = type[0].upper() + type[1:] + 'Node'
        v = yaml_obj.value