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
def parse_sequence(self): self.get_token(yaml.FlowSequenceStartToken) if not self.check_token(yaml.FlowSequenceEndToken): self.parse_node() while not self.check_token(yaml.FlowSequenceEndToken): self.get_token(yaml.FlowEntryToken) if not self.check_token(yaml.FlowSequenceEndToken): self.parse_node() self.get_token(yaml.FlowSequenceEndToken) self.events.append(yaml.SequenceEndEvent(None, None))
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()
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)