Exemplo n.º 1
0
def test_emitter_styles(data_filename, canonical_filename, verbose=False):
    for filename in [data_filename, canonical_filename]:
        events = list(yaml.parse(open(filename, 'rb')))
        for flow_style in [False, True]:
            for style in ['|', '>', '"', '\'', '']:
                styled_events = []
                for event in events:
                    if isinstance(event, yaml.ScalarEvent):
                        event = yaml.ScalarEvent(event.anchor,
                                                 event.tag,
                                                 event.implicit,
                                                 event.value,
                                                 style=style)
                    elif isinstance(event, yaml.SequenceStartEvent):
                        event = yaml.SequenceStartEvent(event.anchor,
                                                        event.tag,
                                                        event.implicit,
                                                        flow_style=flow_style)
                    elif isinstance(event, yaml.MappingStartEvent):
                        event = yaml.MappingStartEvent(event.anchor,
                                                       event.tag,
                                                       event.implicit,
                                                       flow_style=flow_style)
                    styled_events.append(event)
                output = yaml.emit(styled_events)
                if verbose:
                    print("OUTPUT (filename=%r, flow_style=%r, style=%r)" %
                          (filename, flow_style, style))
                    print(output)
                new_events = list(yaml.parse(output))
                _compare_events(events, new_events)
Exemplo n.º 2
0
    def _GetYAMLEvents(self):
        events = []
        events.append(yaml.MappingStartEvent(anchor = None, tag = None, implicit = True, flow_style = False))

        # Serialize all properties to yaml pairs
        sorted_keys = sorted(self._ALL_PROPERTIES.keys())
        for k in sorted_keys:
            if k == 'children' and self.HasChildren():
                events.append(yaml.ScalarEvent(anchor = None, tag = None, implicit = (True, True), value = 'children'))
                events.append(yaml.SequenceStartEvent(anchor = None, tag = None, implicit = (True, True)))
                for child in self.GetChildren():
                    events.extend(child._GetYAMLEvents())

                events.append(yaml.SequenceEndEvent())
            else:
                val = str(self._properties[k])
                # Escape input. This must be symmetric with __init__ when loading initial properties
                val = val.replace('"', '\\"').replace('\n', '\\n').replace('\r', '\\r')
                # val = val.replace("'","\\'")

                events.extend([yaml.ScalarEvent(anchor = None, tag = None, implicit = (True, True), value = str(k)),
                               yaml.ScalarEvent(anchor = None, tag = None, implicit = (True, True), value = val),
                               ])

        events.append(yaml.MappingEndEvent())
        return events
Exemplo n.º 3
0
 def parse_node(self):
     if self.check_token(yaml.AliasToken):
         self.events.append(
             yaml.AliasEvent(self.get_token_value(), None, None))
     else:
         anchor = None
         if self.check_token(yaml.AnchorToken):
             anchor = self.get_token_value()
         tag = None
         if self.check_token(yaml.TagToken):
             tag = self.get_token_value()
         if self.check_token(yaml.ScalarToken):
             self.events.append(
                 yaml.ScalarEvent(anchor, tag, (False, False),
                                  self.get_token_value(), None, None))
         elif self.check_token(yaml.FlowSequenceStartToken):
             self.events.append(
                 yaml.SequenceStartEvent(anchor, tag, None, None))
             self.parse_sequence()
         elif self.check_token(yaml.FlowMappingStartToken):
             self.events.append(
                 yaml.MappingStartEvent(anchor, tag, None, None))
             self.parse_mapping()
         else:
             raise CanonicalError("SCALAR, '[', or '{' is expected, got " +
                                  repr(self.tokens[0]))
Exemplo n.º 4
0
    def _case_yaml_events(self, content_events):
        yield yaml.StreamStartEvent()
        yield yaml.DocumentStartEvent(explicit=True)
        yield yaml.SequenceStartEvent(None,
                                      None,
                                      implicit=True,
                                      flow_style=False)

        yield from content_events

        yield yaml.SequenceEndEvent()
        yield yaml.DocumentEndEvent()
        yield yaml.StreamEndEvent()
Exemplo n.º 5
0
    def _StoreYAMLToStream(self, stream):
        events = [
            yaml.StreamStartEvent(encoding='ascii'),
            yaml.DocumentStartEvent(explicit=True),
            yaml.SequenceStartEvent(anchor=None,
                                    tag=None,
                                    implicit=True,
                                    flow_style=False),
        ]

        # Serialize all elements in order
        for e in self.__elements:
            events.extend(e._GetYAMLEvents())

        events.extend([
            yaml.SequenceEndEvent(),
            yaml.DocumentEndEvent(explicit=True),
            yaml.StreamEndEvent(),
        ])

        yaml.emit(events, stream)