| 1 | # -*- coding: utf-8 -*- |
|---|
| 2 | |
|---|
| 3 | import yaml |
|---|
| 4 | from yaml import events |
|---|
| 5 | |
|---|
| 6 | binarydata = [chr(i) for i in xrange(256) ] |
|---|
| 7 | binarydata = "".join(binarydata) |
|---|
| 8 | print "binarydata: %r" % (binarydata, ) |
|---|
| 9 | |
|---|
| 10 | def dump_dict(dumper, d): |
|---|
| 11 | dumper.emit( events.MappingStartEvent(anchor=None, tag=None, implicit=True) ) |
|---|
| 12 | |
|---|
| 13 | for k, v in d.items(): |
|---|
| 14 | dumper.emit( events.ScalarEvent(anchor=None, tag=None, implicit=(True, False), value=k) ) |
|---|
| 15 | dumper.emit( events.ScalarEvent(anchor=None, tag=None, implicit=(True, False), value=v) ) |
|---|
| 16 | |
|---|
| 17 | dumper.emit( events.MappingEndEvent() ) |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | dump_fn = "test.yaml" |
|---|
| 21 | print ("Saving to file: %r " % (dump_fn, )) |
|---|
| 22 | dumpf = open(dump_fn, "wb") |
|---|
| 23 | # http://pyyaml.org/wiki/PyYAMLDocumentation |
|---|
| 24 | dumper = yaml.CSafeDumper(dumpf) |
|---|
| 25 | |
|---|
| 26 | # dumper.emit( yaml.StreamStartEvent(encoding='utf-8') ) |
|---|
| 27 | # dumper.emit( yaml.DocumentStartEvent() ) |
|---|
| 28 | dumper.open() |
|---|
| 29 | |
|---|
| 30 | # dumper.emit( yaml.events.StreamStartEvent() ) |
|---|
| 31 | dumper.emit( events.DocumentStartEvent() ) |
|---|
| 32 | |
|---|
| 33 | # MappingStartEvent(anchor=None, tag=None, implicit=True) |
|---|
| 34 | dumper.emit( events.MappingStartEvent(anchor=None, tag=None, implicit=True) ) |
|---|
| 35 | |
|---|
| 36 | dumper.emit( events.ScalarEvent(anchor=None, tag=None, implicit=(True, False), value='1') ) |
|---|
| 37 | dumper.emit( events.ScalarEvent(anchor=None, tag=None, implicit=(True, False), value='one') ) |
|---|
| 38 | |
|---|
| 39 | dumper.emit( events.ScalarEvent(anchor=None, tag=None, implicit=(True, False), value='2') ) |
|---|
| 40 | dump_dict(dumper, {"intro":"intro_value", "desc": "long description", "multiline": "line 1\nline2\nline3\n", "binary": binarydata }) |
|---|
| 41 | |
|---|
| 42 | dumper.emit( events.MappingEndEvent() ) |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | dumper.emit( events.DocumentEndEvent(explicit=True) ) |
|---|
| 46 | # dumper.emit( yaml.StreamEndEvent() ) |
|---|
| 47 | dumper.close() |
|---|