def _fresh_content_events(self, content_iterable): # Header events yield yaml.StreamStartEvent() yield yaml.DocumentStartEvent() yield yaml.MappingStartEvent(None, None, True, flow_style=False) # Content events for key, value in content_iterable: yield yaml.ScalarEvent(None, None, (True, False), key) if isinstance(value, dict): yield from _yaml_content_events( dict((k, v) for k, v in value.items() if k not in self.CASE_PRIMARY_KEYS)) elif callable(getattr(value, 'case_data_events')): yield yaml.MappingStartEvent(None, None, True, flow_style=False) yield from value.case_data_events() yield yaml.MappingEndEvent() else: yield yaml.MappingStartEvent(None, None, True, flow_style=False) yield from value yield yaml.MappingEndEvent() # Tail events yield yaml.MappingEndEvent() yield yaml.DocumentEndEvent() yield yaml.StreamEndEvent()
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)
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_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]))
def augmented_test_case_events(self, case_key, case_id_events): """Generate YAML events for a test case :param str case_key: The case key for augmentation :param case_id_events: An iterable of YAML events representing the key/value pairs of the test case identity This is used internally when extending an updates file with the existing data from a case, given the ID of the case as YAML. """ case_augmenter = self._case_augmenters.get(case_key) yield yaml.MappingStartEvent(None, None, True, flow_style=False) yield from case_id_events if case_augmenter is not None: yield from case_augmenter.case_data_events() yield yaml.MappingEndEvent()
def _full_yaml_mapping_events_from_update_augmentation(cls, augmenter): yield yaml.MappingStartEvent(None, None, True, flow_style=False) yield from augmenter.case_data_events() yield yaml.MappingEndEvent()