source: branches/pyyaml3000/lib/yaml/resolver.py @ 53

Revision 53, 2.1 KB checked in by xi, 8 years ago (diff)

Implement Composer and BaseResolver?.

RevLine 
[53]1
2from nodes import *
3
4class BaseResolver:
5
6    DEFAULT_SCALAR_TAG = u'tag:yaml.org,2002:str'
7    DEFAULT_SEQUENCE_TAG = u'tag:yaml.org,2002:seq'
8    DEFAULT_MAPPING_TAG = u'tag:yaml.org,2002:map'
9
10    def __init__(self, composer):
11        self.composer = composer
12        self.resolved_nodes = {}
13
14    def check(self):
15        # If there are more documents available?
16        return self.composer.check()
17
18    def get(self):
19        # Resolve and return the root node of the next document.
20        if self.composer.check():
21            return self.resolve_document(self.composer.get())
22
23    def __iter__(self):
24        # Iterator protocol.
25        while self.composer.check():
26            yield self.resolve_document(self.composer.get())
27
28    def resolve_document(self, node):
29        self.resolve_node([], node)
30        return node
31        self.resolved_nodes = {}
32
33    def resolve_node(self, path, node):
34        if node in self.resolved_nodes:
35            return
36        self.resolved_nodes[node] = None
37        if isinstance(node, ScalarNode):
38            self.resolve_scalar(path, node)
39        elif isinstance(node, SequenceNode):
40            self.resolve_sequence(path, node)
41            for index in len(node.value):
42                self.resolve_node(path+[node, index], node.value[index])
43        elif isinstance(node, MappingNode):
44            self.resolve_mapping(path, node)
45            for key, value in node.value:
46                self.resolve_node(path+[node, None], key)
47                self.resolve_node(path+[node, key], value)
48
49    def resolve_scalar(self, node):
50        if node.tag is None:
51            node.tag = self.detect_scalar(node.value)
52        if node.tag is None or node.tag == u'!':
53            node.tag = self.DEFAULT_SCALAR_TAG
54
55    def resolve_sequence(self, node):
56        if node.tag is None or node.tag == u'!':
57            node.tag = self.DEFAULT_SEQUENCE_TAG
58
59    def resolve_mapping(self, node):
60        if node.tag is None or node.tag == u'!':
61            node.tag = self.DEFAULT_MAPPING_TAG
62
63    def detect_scalar(self, value):
64        return None
65
66class Resolver(BaseResolver):
67    pass
68
Note: See TracBrowser for help on using the repository browser.