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_emitter_events(events_filename, verbose=False): events = list(yaml.load(open(events_filename, 'rb'), Loader=EventsLoader)) output = yaml.emit(events) if verbose: print "OUTPUT:" 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 = (u'\ufeff'+input).encode(encoding) output1 = yaml.emit(yaml.parse(input), allow_unicode=True) stream = StringIO.StringIO() yaml.emit(yaml.parse(input), _unicode_open(stream, 'utf-8'), allow_unicode=True) output2 = stream.getvalue() if encoding is None: assert isinstance(output1, unicode), (type(output1), encoding) else: assert isinstance(output1, str), (type(output1), encoding) output1.decode(encoding) assert isinstance(output2, str), (type(output2), encoding) output2.decode('utf-8')
def test_parser_on_canonical(canonical_filename, verbose=False): events1 = None events2 = None try: events1 = list(yaml.parse(open(canonical_filename, 'rb'))) events2 = list(yaml.canonical_parse(open(canonical_filename, 'rb'))) _compare_events(events1, events2, full=True) finally: if verbose: print "EVENTS1:" pprint.pprint(events1) print "EVENTS2:" pprint.pprint(events2)
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 canonical_parse(stream): return yaml.parse(stream, Loader=CanonicalLoader)