| [53] | 1 | |
|---|
| [137] | 2 | __all__ = ['Composer', 'ComposerError'] |
|---|
| [57] | 3 | |
|---|
| [53] | 4 | from error import MarkedYAMLError |
|---|
| 5 | from events import * |
|---|
| 6 | from nodes import * |
|---|
| 7 | |
|---|
| 8 | class ComposerError(MarkedYAMLError): |
|---|
| 9 | pass |
|---|
| 10 | |
|---|
| [222] | 11 | class Composer(object): |
|---|
| [53] | 12 | |
|---|
| [136] | 13 | def __init__(self): |
|---|
| [142] | 14 | self.anchors = {} |
|---|
| [53] | 15 | |
|---|
| [136] | 16 | def check_node(self): |
|---|
| [53] | 17 | # If there are more documents available? |
|---|
| [136] | 18 | return not self.check_event(StreamEndEvent) |
|---|
| [53] | 19 | |
|---|
| [136] | 20 | def get_node(self): |
|---|
| [53] | 21 | # Get the root node of the next document. |
|---|
| [136] | 22 | if not self.check_event(StreamEndEvent): |
|---|
| [53] | 23 | return self.compose_document() |
|---|
| 24 | |
|---|
| 25 | def compose_document(self): |
|---|
| [118] | 26 | |
|---|
| [136] | 27 | # Drop the STREAM-START event. |
|---|
| 28 | if self.check_event(StreamStartEvent): |
|---|
| 29 | self.get_event() |
|---|
| 30 | |
|---|
| [118] | 31 | # Drop the DOCUMENT-START event. |
|---|
| [136] | 32 | self.get_event() |
|---|
| [118] | 33 | |
|---|
| 34 | # Compose the root node. |
|---|
| [137] | 35 | node = self.compose_node(None, None) |
|---|
| [118] | 36 | |
|---|
| 37 | # Drop the DOCUMENT-END event. |
|---|
| [136] | 38 | self.get_event() |
|---|
| [118] | 39 | |
|---|
| [223] | 40 | self.anchors = {} |
|---|
| [53] | 41 | return node |
|---|
| 42 | |
|---|
| [137] | 43 | def compose_node(self, parent, index): |
|---|
| [136] | 44 | if self.check_event(AliasEvent): |
|---|
| 45 | event = self.get_event() |
|---|
| [53] | 46 | anchor = event.anchor |
|---|
| [142] | 47 | if anchor not in self.anchors: |
|---|
| [53] | 48 | raise ComposerError(None, None, "found undefined alias %r" |
|---|
| [116] | 49 | % anchor.encode('utf-8'), event.start_mark) |
|---|
| [142] | 50 | return self.anchors[anchor] |
|---|
| [136] | 51 | event = self.peek_event() |
|---|
| [53] | 52 | anchor = event.anchor |
|---|
| 53 | if anchor is not None: |
|---|
| [142] | 54 | if anchor in self.anchors: |
|---|
| [53] | 55 | raise ComposerError("found duplicate anchor %r; first occurence" |
|---|
| [142] | 56 | % anchor.encode('utf-8'), self.anchors[anchor].start_mark, |
|---|
| [116] | 57 | "second occurence", event.start_mark) |
|---|
| [137] | 58 | self.descend_resolver(parent, index) |
|---|
| [136] | 59 | if self.check_event(ScalarEvent): |
|---|
| [142] | 60 | node = self.compose_scalar_node(anchor) |
|---|
| [136] | 61 | elif self.check_event(SequenceStartEvent): |
|---|
| [142] | 62 | node = self.compose_sequence_node(anchor) |
|---|
| [136] | 63 | elif self.check_event(MappingStartEvent): |
|---|
| [142] | 64 | node = self.compose_mapping_node(anchor) |
|---|
| [137] | 65 | self.ascend_resolver() |
|---|
| [53] | 66 | return node |
|---|
| 67 | |
|---|
| [142] | 68 | def compose_scalar_node(self, anchor): |
|---|
| [136] | 69 | event = self.get_event() |
|---|
| [137] | 70 | tag = event.tag |
|---|
| 71 | if tag is None or tag == u'!': |
|---|
| 72 | tag = self.resolve(ScalarNode, event.value, event.implicit) |
|---|
| [142] | 73 | node = ScalarNode(tag, event.value, |
|---|
| [133] | 74 | event.start_mark, event.end_mark, style=event.style) |
|---|
| [142] | 75 | if anchor is not None: |
|---|
| 76 | self.anchors[anchor] = node |
|---|
| 77 | return node |
|---|
| [53] | 78 | |
|---|
| [142] | 79 | def compose_sequence_node(self, anchor): |
|---|
| [136] | 80 | start_event = self.get_event() |
|---|
| [137] | 81 | tag = start_event.tag |
|---|
| 82 | if tag is None or tag == u'!': |
|---|
| 83 | tag = self.resolve(SequenceNode, None, start_event.implicit) |
|---|
| [136] | 84 | node = SequenceNode(tag, [], |
|---|
| 85 | start_event.start_mark, None, |
|---|
| [133] | 86 | flow_style=start_event.flow_style) |
|---|
| [142] | 87 | if anchor is not None: |
|---|
| 88 | self.anchors[anchor] = node |
|---|
| [136] | 89 | index = 0 |
|---|
| 90 | while not self.check_event(SequenceEndEvent): |
|---|
| [137] | 91 | node.value.append(self.compose_node(node, index)) |
|---|
| [136] | 92 | index += 1 |
|---|
| 93 | end_event = self.get_event() |
|---|
| 94 | node.end_mark = end_event.end_mark |
|---|
| 95 | return node |
|---|
| [53] | 96 | |
|---|
| [142] | 97 | def compose_mapping_node(self, anchor): |
|---|
| [136] | 98 | start_event = self.get_event() |
|---|
| [137] | 99 | tag = start_event.tag |
|---|
| 100 | if tag is None or tag == u'!': |
|---|
| 101 | tag = self.resolve(MappingNode, None, start_event.implicit) |
|---|
| [222] | 102 | node = MappingNode(tag, [], |
|---|
| [136] | 103 | start_event.start_mark, None, |
|---|
| 104 | flow_style=start_event.flow_style) |
|---|
| [142] | 105 | if anchor is not None: |
|---|
| 106 | self.anchors[anchor] = node |
|---|
| [136] | 107 | while not self.check_event(MappingEndEvent): |
|---|
| [222] | 108 | #key_event = self.peek_event() |
|---|
| [137] | 109 | item_key = self.compose_node(node, None) |
|---|
| [222] | 110 | #if item_key in node.value: |
|---|
| 111 | # raise ComposerError("while composing a mapping", start_event.start_mark, |
|---|
| 112 | # "found duplicate key", key_event.start_mark) |
|---|
| [137] | 113 | item_value = self.compose_node(node, item_key) |
|---|
| [222] | 114 | #node.value[item_key] = item_value |
|---|
| 115 | node.value.append((item_key, item_value)) |
|---|
| [136] | 116 | end_event = self.get_event() |
|---|
| 117 | node.end_mark = end_event.end_mark |
|---|
| 118 | return node |
|---|
| [53] | 119 | |
|---|