def dump(self, outfile, values=None, show_comments=True, show_choices=True): """Write the configuration to the given output stream. :param stream outfile: A writable stream object :param {} values: Write the configuration file with the given values inserted. Values should be a dictionary as produced by YamlConfigLoader.load(). :param bool show_comments: When dumping the config file, include help_text and general element information as comments. Default True. :param bool show_choices: When creating comments, include the choices available for each item. Default True. """ # We're recursively generating a list of pyYaml events, which will # then be emitted to create the yaml file. Each element knows how to # represent itself and how to include any child elements. events = list() events.extend([yaml.StreamStartEvent(), yaml.DocumentStartEvent()]) events.extend(self.yaml_events(values, show_comments, show_choices)) events.extend([yaml.DocumentEndEvent(), yaml.StreamEndEvent()]) yaml.emit(events, outfile)
def test_emitter_error(error_filename, verbose=False): events = list( yaml.load(open(error_filename, 'rb'), Loader=test_emitter.EventsLoader)) try: yaml.emit(events) except yaml.YAMLError as exc: if verbose: print("%s:" % exc.__class__.__name__, exc) else: raise AssertionError("expected an exception")
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 test_emitter_on_data(data_filename, canonical_filename, verbose=False): events = list(yaml.parse(open(data_filename, 'rb'))) output = yaml.emit(events) if verbose: print("OUTPUT:") print(output) new_events = list(yaml.parse(output)) _compare_events(events, new_events)
def test_emitter_on_canonical(canonical_filename, verbose=False): events = list(yaml.parse(open(canonical_filename, 'rb'))) for canonical in [False, True]: output = yaml.emit(events, canonical=canonical) if verbose: print("OUTPUT (canonical=%s):" % canonical) print(output) new_events = list(yaml.parse(output)) _compare_events(events, new_events)
def test_unicode_transfer(unicode_filename, verbose=False): data = open(unicode_filename, 'rb').read().decode('utf-8') for encoding in [None, 'utf-8', 'utf-16-be', 'utf-16-le']: input = data if encoding is not None: input = ('\ufeff' + input).encode(encoding) output1 = yaml.emit(yaml.parse(input), allow_unicode=True) if encoding is None: stream = io.StringIO() else: stream = io.BytesIO() yaml.emit(yaml.parse(input), stream, allow_unicode=True) output2 = stream.getvalue() assert isinstance(output1, str), (type(output1), encoding) if encoding is None: assert isinstance(output2, str), (type(output1), encoding) else: assert isinstance(output2, bytes), (type(output1), encoding) output2.decode(encoding)
def test_emitter_events(events_filename, verbose=False): events = tuple(yaml.load(open(events_filename, 'rb'), Loader=EventsLoader)) output = yaml.emit(events) if verbose: print("OUTPUT:", events_filename) print(output) new_events = list(yaml.parse(output)) no_comments = filter(lambda e: not isinstance(e, yaml.CommentEvent), events) _compare_events(list(no_comments), new_events)