Commit dd09282a authored by SVN-Git Migration's avatar SVN-Git Migration

Imported Upstream version 3.04

parents
Copyright (c) 2006 Kirill Simonov
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Metadata-Version: 1.0
Name: PyYAML
Version: 3.04
Summary: YAML parser and emitter for Python
Home-page: http://pyyaml.org/wiki/PyYAML
Author: Kirill Simonov
Author-email: xi@resolvent.net
License: MIT
Download-URL: http://pyyaml.org/download/pyyaml/PyYAML-3.04.tar.gz
Description: YAML is a data serialization format designed for human readability and
interaction with scripting languages. PyYAML is a YAML parser and
emitter for Python.
PyYAML features a complete YAML 1.1 parser, Unicode support, pickle
support, capable extension API, and sensible error messages. PyYAML
supports standard YAML tags and provides Python-specific tags that allow
to represent an arbitrary Python object.
PyYAML is applicable for a broad range of tasks from complex
configuration files to object serialization and persistance.
Platform: Any
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Topic :: Text Processing :: Markup
PyYAML 3000 - The next generation YAML parser and emitter for Python.
To install, type 'python setup.py install'.
You may build faster LibYAML based parser and emitter with
'python setup_with_libyaml.py install'.
Then you may use them with the calls:
>>> yaml.load(stream, Loader=yaml.CLoader)
>>> yaml.dump(data, Dumper=yaml.CDumper)
For more information, check the PyYAML homepage:
'http://pyyaml.org/wiki/PyYAML'.
Documentation (rough and incomplete though):
'http://pyyaml.org/wiki/PyYAMLDocumentation'.
Post your questions and opinions to the YAML-Core mailing list:
'http://lists.sourceforge.net/lists/listinfo/yaml-core'.
Submit bug reports and feature requests to the PyYAML bug tracker:
'http://pyyaml.org/newticket?component=pyyaml'.
PyYAML 3000 is written by Kirill Simonov <xi@resolvent.net>. It is released
under the MIT license. See the file LICENSE for more details.
%YAML 1.1
---
ascii:
header: "\e[0;1;30;40m"
footer: "\e[0m"
tokens:
stream-start:
stream-end:
directive: { start: "\e[35m", end: "\e[0;1;30;40m" }
document-start: { start: "\e[35m", end: "\e[0;1;30;40m" }
document-end: { start: "\e[35m", end: "\e[0;1;30;40m" }
block-sequence-start:
block-mapping-start:
block-end:
flow-sequence-start: { start: "\e[33m", end: "\e[0;1;30;40m" }
flow-mapping-start: { start: "\e[33m", end: "\e[0;1;30;40m" }
flow-sequence-end: { start: "\e[33m", end: "\e[0;1;30;40m" }
flow-mapping-end: { start: "\e[33m", end: "\e[0;1;30;40m" }
key: { start: "\e[33m", end: "\e[0;1;30;40m" }
value: { start: "\e[33m", end: "\e[0;1;30;40m" }
block-entry: { start: "\e[33m", end: "\e[0;1;30;40m" }
flow-entry: { start: "\e[33m", end: "\e[0;1;30;40m" }
alias: { start: "\e[32m", end: "\e[0;1;30;40m" }
anchor: { start: "\e[32m", end: "\e[0;1;30;40m" }
tag: { start: "\e[32m", end: "\e[0;1;30;40m" }
scalar: { start: "\e[36m", end: "\e[0;1;30;40m" }
replaces:
- "\r\n": "\n"
- "\r": "\n"
- "\n": "\n"
- "\x85": "\n"
- "\u2028": "\n"
- "\u2029": "\n"
html: &html
tokens:
stream-start:
stream-end:
directive: { start: <code class="directive_token">, end: </code> }
document-start: { start: <code class="document_start_token">, end: </code> }
document-end: { start: <code class="document_end_token">, end: </code> }
block-sequence-start:
block-mapping-start:
block-end:
flow-sequence-start: { start: <code class="delimiter_token">, end: </code> }
flow-mapping-start: { start: <code class="delimiter_token">, end: </code> }
flow-sequence-end: { start: <code class="delimiter_token">, end: </code> }
flow-mapping-end: { start: <code class="delimiter_token">, end: </code> }
key: { start: <code class="delimiter_token">, end: </code> }
value: { start: <code class="delimiter_token">, end: </code> }
block-entry: { start: <code class="delimiter_token">, end: </code> }
flow-entry: { start: <code class="delimiter_token">, end: </code> }
alias: { start: <code class="anchor_token">, end: </code> }
anchor: { start: <code class="anchor_token">, end: </code> }
tag: { start: <code class="tag_token">, end: </code> }
scalar: { start: <code class="scalar_token">, end: </code> }
events:
stream-start: { start: <pre class="yaml_stream"> }
stream-end: { end: </pre> }
document-start: { start: <span class="document"> }
document-end: { end: </span> }
sequence-start: { start: <span class="sequence"> }
sequence-end: { end: </span> }
mapping-start: { start: <span class="mapping"> }
mapping-end: { end: </span> }
scalar: { start: <span class="scalar">, end: </span> }
replaces:
- "\r\n": "\n"
- "\r": "\n"
- "\n": "\n"
- "\x85": "\n"
- "\u2028": "\n"
- "\u2029": "\n"
- "&": "&amp;"
- "<": "&lt;"
- ">": "&gt;"
html-page:
header: |
<html>
<head>
<title>A YAML stream</title>
<style type="text/css">
.document { background: #FFF }
.sequence { background: #EEF }
.mapping { background: #EFE }
.scalar { background: #FEE }
.directive_token { color: #C0C }
.document_start_token { color: #C0C; font-weight: bold }
.document_end_token { color: #C0C; font-weight: bold }
.delimiter_token { color: #600; font-weight: bold }
.anchor_token { color: #090 }
.tag_token { color: #090 }
.scalar_token { color: #000 }
.yaml_stream { color: #999 }
</style>
<body>
footer: |
</body>
</html>
<<: *html
# vim: ft=yaml
#!/usr/bin/python
import yaml, codecs, sys, os.path, optparse
class Style:
def __init__(self, header=None, footer=None,
tokens=None, events=None, replaces=None):
self.header = header
self.footer = footer
self.replaces = replaces
self.substitutions = {}
for domain, Class in [(tokens, 'Token'), (events, 'Event')]:
if not domain:
continue
for key in domain:
name = ''.join([part.capitalize() for part in key.split('-')])
cls = getattr(yaml, '%s%s' % (name, Class))
value = domain[key]
if not value:
continue
start = value.get('start')
end = value.get('end')
if start:
self.substitutions[cls, -1] = start
if end:
self.substitutions[cls, +1] = end
def __setstate__(self, state):
self.__init__(**state)
yaml.add_path_resolver(u'tag:yaml.org,2002:python/object:__main__.Style',
[None], dict)
yaml.add_path_resolver(u'tag:yaml.org,2002:pairs',
[None, u'replaces'], list)
class YAMLHighlight:
def __init__(self, options):
config = yaml.load(file(options.config, 'rb').read())
self.style = config[options.style]
if options.input:
self.input = file(options.input, 'rb')
else:
self.input = sys.stdin
if options.output:
self.output = file(options.output, 'wb')
else:
self.output = sys.stdout
def highlight(self):
input = self.input.read()
if input.startswith(codecs.BOM_UTF16_LE):
input = unicode(input, 'utf-16-le')
elif input.startswith(codecs.BOM_UTF16_BE):
input = unicode(input, 'utf-16-be')
else:
input = unicode(input, 'utf-8')
substitutions = self.style.substitutions
tokens = yaml.scan(input)
events = yaml.parse(input)
markers = []
number = 0
for token in tokens:
number += 1
if token.start_mark.index != token.end_mark.index:
cls = token.__class__
if (cls, -1) in substitutions:
markers.append([token.start_mark.index, +2, number, substitutions[cls, -1]])
if (cls, +1) in substitutions:
markers.append([token.end_mark.index, -2, number, substitutions[cls, +1]])
number = 0
for event in events:
number += 1
cls = event.__class__
if (cls, -1) in substitutions:
markers.append([event.start_mark.index, +1, number, substitutions[cls, -1]])
if (cls, +1) in substitutions:
markers.append([event.end_mark.index, -1, number, substitutions[cls, +1]])
markers.sort()
markers.reverse()
chunks = []
position = len(input)
for index, weight1, weight2, substitution in markers:
if index < position:
chunk = input[index:position]
for substring, replacement in self.style.replaces:
chunk = chunk.replace(substring, replacement)
chunks.append(chunk)
position = index
chunks.append(substitution)
chunks.reverse()
result = u''.join(chunks)
if self.style.header:
self.output.write(self.style.header)
self.output.write(result.encode('utf-8'))
if self.style.footer:
self.output.write(self.style.footer)
if __name__ == '__main__':
parser = optparse.OptionParser()
parser.add_option('-s', '--style', dest='style', default='ascii',
help="specify the highlighting style", metavar='STYLE')
parser.add_option('-c', '--config', dest='config',
default=os.path.join(os.path.dirname(sys.argv[0]), 'yaml_hl.cfg'),
help="set an alternative configuration file", metavar='CONFIG')
parser.add_option('-i', '--input', dest='input', default=None,
help="set the input file (default: stdin)", metavar='FILE')
parser.add_option('-o', '--output', dest='output', default=None,
help="set the output file (default: stdout)", metavar='FILE')
(options, args) = parser.parse_args()
hl = YAMLHighlight(options)
hl.highlight()
#include <yaml.h>
cdef extern from "_yaml.h":
void malloc(int l)
void memcpy(char *d, char *s, int l)
int strlen(char *s)
int PyString_CheckExact(object o)
int PyUnicode_CheckExact(object o)
char *PyString_AS_STRING(object o)
int PyString_GET_SIZE(object o)
object PyString_FromStringAndSize(char *v, int l)
object PyUnicode_DecodeUTF8(char *s, int s, char *e)
object PyUnicode_AsUTF8String(object o)
ctypedef enum:
SIZEOF_VOID_P
ctypedef enum yaml_encoding_t:
YAML_ANY_ENCODING
YAML_UTF8_ENCODING
YAML_UTF16LE_ENCODING
YAML_UTF16BE_ENCODING
ctypedef enum yaml_break_t:
YAML_ANY_BREAK
YAML_CR_BREAK
YAML_LN_BREAK
YAML_CRLN_BREAK
ctypedef enum yaml_error_type_t:
YAML_NO_ERROR
YAML_MEMORY_ERROR
YAML_READER_ERROR
YAML_SCANNER_ERROR
YAML_PARSER_ERROR
YAML_WRITER_ERROR
YAML_EMITTER_ERROR
ctypedef enum yaml_scalar_style_t:
YAML_ANY_SCALAR_STYLE
YAML_PLAIN_SCALAR_STYLE
YAML_SINGLE_QUOTED_SCALAR_STYLE
YAML_DOUBLE_QUOTED_SCALAR_STYLE
YAML_LITERAL_SCALAR_STYLE
YAML_FOLDED_SCALAR_STYLE
ctypedef enum yaml_sequence_style_t:
YAML_ANY_SEQUENCE_STYLE
YAML_BLOCK_SEQUENCE_STYLE
YAML_FLOW_SEQUENCE_STYLE
ctypedef enum yaml_mapping_style_t:
YAML_ANY_MAPPING_STYLE
YAML_BLOCK_MAPPING_STYLE
YAML_FLOW_MAPPING_STYLE
ctypedef enum yaml_token_type_t:
YAML_NO_TOKEN
YAML_STREAM_START_TOKEN
YAML_STREAM_END_TOKEN
YAML_VERSION_DIRECTIVE_TOKEN
YAML_TAG_DIRECTIVE_TOKEN
YAML_DOCUMENT_START_TOKEN
YAML_DOCUMENT_END_TOKEN
YAML_BLOCK_SEQUENCE_START_TOKEN
YAML_BLOCK_MAPPING_START_TOKEN
YAML_BLOCK_END_TOKEN
YAML_FLOW_SEQUENCE_START_TOKEN
YAML_FLOW_SEQUENCE_END_TOKEN
YAML_FLOW_MAPPING_START_TOKEN
YAML_FLOW_MAPPING_END_TOKEN
YAML_BLOCK_ENTRY_TOKEN
YAML_FLOW_ENTRY_TOKEN
YAML_KEY_TOKEN
YAML_VALUE_TOKEN
YAML_ALIAS_TOKEN
YAML_ANCHOR_TOKEN
YAML_TAG_TOKEN
YAML_SCALAR_TOKEN
ctypedef enum yaml_event_type_t:
YAML_NO_EVENT
YAML_STREAM_START_EVENT
YAML_STREAM_END_EVENT
YAML_DOCUMENT_START_EVENT
YAML_DOCUMENT_END_EVENT
YAML_ALIAS_EVENT
YAML_SCALAR_EVENT
YAML_SEQUENCE_START_EVENT
YAML_SEQUENCE_END_EVENT
YAML_MAPPING_START_EVENT
YAML_MAPPING_END_EVENT
ctypedef int yaml_read_handler_t(void *data, char *buffer,
int size, int *size_read) except 0
ctypedef int yaml_write_handler_t(void *data, char *buffer,
int size) except 0
ctypedef struct yaml_mark_t:
int index
int line
int column
ctypedef struct yaml_version_directive_t:
int major
int minor
ctypedef struct yaml_tag_directive_t:
char *handle
char *prefix
ctypedef struct _yaml_token_stream_start_data_t:
yaml_encoding_t encoding
ctypedef struct _yaml_token_alias_data_t:
char *value
ctypedef struct _yaml_token_anchor_data_t:
char *value
ctypedef struct _yaml_token_tag_data_t:
char *handle
char *suffix
ctypedef struct _yaml_token_scalar_data_t:
char *value
int length
yaml_scalar_style_t style
ctypedef struct _yaml_token_version_directive_data_t:
int major
int minor
ctypedef struct _yaml_token_tag_directive_data_t:
char *handle
char *prefix
ctypedef union _yaml_token_data_t:
_yaml_token_stream_start_data_t stream_start
_yaml_token_alias_data_t alias
_yaml_token_anchor_data_t anchor
_yaml_token_tag_data_t tag
_yaml_token_scalar_data_t scalar
_yaml_token_version_directive_data_t version_directive
_yaml_token_tag_directive_data_t tag_directive
ctypedef struct yaml_token_t:
yaml_token_type_t type
_yaml_token_data_t data
yaml_mark_t start_mark
yaml_mark_t end_mark
ctypedef struct _yaml_event_stream_start_data_t:
yaml_encoding_t encoding
ctypedef struct _yaml_event_document_start_data_tag_directives_t:
yaml_tag_directive_t *start
yaml_tag_directive_t *end
ctypedef struct _yaml_event_document_start_data_t:
yaml_version_directive_t *version_directive
_yaml_event_document_start_data_tag_directives_t tag_directives
int implicit
ctypedef struct _yaml_event_document_end_data_t:
int implicit
ctypedef struct _yaml_event_alias_data_t:
char *anchor
ctypedef struct _yaml_event_scalar_data_t:
char *anchor
char *tag
char *value
int length
int plain_implicit
int quoted_implicit
yaml_scalar_style_t style
ctypedef struct _yaml_event_sequence_start_data_t:
char *anchor
char *tag
int implicit
yaml_sequence_style_t style
ctypedef struct _yaml_event_mapping_start_data_t:
char *anchor
char *tag
int implicit
yaml_mapping_style_t style
ctypedef union _yaml_event_data_t:
_yaml_event_stream_start_data_t stream_start
_yaml_event_document_start_data_t document_start
_yaml_event_document_end_data_t document_end
_yaml_event_alias_data_t alias
_yaml_event_scalar_data_t scalar
_yaml_event_sequence_start_data_t sequence_start
_yaml_event_mapping_start_data_t mapping_start
ctypedef struct yaml_event_t:
yaml_event_type_t type
_yaml_event_data_t data
yaml_mark_t start_mark
yaml_mark_t end_mark
ctypedef struct yaml_parser_t:
yaml_error_type_t error
char *problem
int problem_offset
int problem_value
yaml_mark_t problem_mark
char *context
yaml_mark_t context_mark
ctypedef struct yaml_emitter_t:
yaml_error_type_t error
char *problem
char *yaml_get_version_string()
void yaml_get_version(int *major, int *minor, int *patch)
void yaml_token_delete(yaml_token_t *token)
int yaml_stream_start_event_initialize(yaml_event_t *event,
yaml_encoding_t encoding)
int yaml_stream_end_event_initialize(yaml_event_t *event)
int yaml_document_start_event_initialize(yaml_event_t *event,
yaml_version_directive_t *version_directive,
yaml_tag_directive_t *tag_directives_start,
yaml_tag_directive_t *tag_directives_end,
int implicit)
int yaml_document_end_event_initialize(yaml_event_t *event,
int implicit)
int yaml_alias_event_initialize(yaml_event_t *event, char *anchor)
int yaml_scalar_event_initialize(yaml_event_t *event,
char *anchor, char *tag, char *value, int length,
int plain_implicit, int quoted_implicit,
yaml_scalar_style_t style)
int yaml_sequence_start_event_initialize(yaml_event_t *event,
char *anchor, char *tag, int implicit, yaml_sequence_style_t style)
int yaml_sequence_end_event_initialize(yaml_event_t *event)
int yaml_mapping_start_event_initialize(yaml_event_t *event,
char *anchor, char *tag, int implicit, yaml_mapping_style_t style)
int yaml_mapping_end_event_initialize(yaml_event_t *event)
void yaml_event_delete(yaml_event_t *event)
int yaml_parser_initialize(yaml_parser_t *parser)
void yaml_parser_delete(yaml_parser_t *parser)
void yaml_parser_set_input_string(yaml_parser_t *parser,
char *input, int size)
void yaml_parser_set_input(yaml_parser_t *parser,
yaml_read_handler_t *handler, void *data)
void yaml_parser_set_encoding(yaml_parser_t *parser,
yaml_encoding_t encoding)
int yaml_parser_scan(yaml_parser_t *parser, yaml_token_t *token) except *
int yaml_parser_parse(yaml_parser_t *parser, yaml_event_t *event) except *
int yaml_emitter_initialize(yaml_emitter_t *emitter)
void yaml_emitter_delete(yaml_emitter_t *emitter)
void yaml_emitter_set_output_string(yaml_emitter_t *emitter,
char *output, int size, int *size_written)
void yaml_emitter_set_output(yaml_emitter_t *emitter,
yaml_write_handler_t *handler, void *data)
void yaml_emitter_set_encoding(yaml_emitter_t *emitter,
yaml_encoding_t encoding)
void yaml_emitter_set_canonical(yaml_emitter_t *emitter, int canonical)
void yaml_emitter_set_indent(yaml_emitter_t *emitter, int indent)
void yaml_emitter_set_width(yaml_emitter_t *emitter, int width)
void yaml_emitter_set_unicode(yaml_emitter_t *emitter, int unicode)
void yaml_emitter_set_break(yaml_emitter_t *emitter,
yaml_break_t line_break)
int yaml_emitter_emit(yaml_emitter_t *emitter, yaml_event_t *event) except *
int yaml_emitter_flush(yaml_emitter_t *emitter)
This diff is collapsed.
from error import *
from tokens import *
from events import *
from nodes import *
from loader import *
from dumper import *
try:
from cyaml import *
except ImportError:
pass
def scan(stream, Loader=Loader):
"""
Scan a YAML stream and produce scanning tokens.
"""
loader = Loader(stream)
while loader.check_token():
yield loader.get_token()
def parse(stream, Loader=Loader):
"""
Parse a YAML stream and produce parsing events.
"""
loader = Loader(stream)
while loader.check_event():
yield loader.get_event()
def compose(stream, Loader=Loader):
"""
Parse the first YAML document in a stream
and produce the corresponding representation tree.
"""
loader = Loader(stream)
if loader.check_node():
return loader.get_node()
def compose_all(stream, Loader=Loader):
"""
Parse all YAML documents in a stream
and produce corresponsing representation trees.
"""
loader = Loader(stream)
while loader.check_node():
yield loader.get_node()
def load_all(stream, Loader=Loader):
"""
Parse all YAML documents in a stream
and produce corresponding Python objects.
"""
loader = Loader(stream)
while loader.check_data():
yield loader.get_data()
def load(stream, Loader=Loader):
"""
Parse the first YAML document in a stream
and produce the corresponding Python object.
"""
loader = Loader(stream)
if loader.check_data():
return loader.get_data()
def safe_load_all(stream):
"""
Parse all YAML documents in a stream
and produce corresponding Python objects.
Resolve only basic YAML tags.
"""
return load_all(stream, SafeLoader)
def safe_load(stream):
"""
Parse the first YAML document in a stream
and produce the corresponding Python object.
Resolve only basic YAML tags.
"""
return load(stream, SafeLoader)
def emit(events, stream=None, Dumper=Dumper,
canonical=None, indent=None, width=None,
allow_unicode=None, line_break=None):
"""
Emit YAML parsing events into a stream.
If stream is None, return the produced string instead.
"""
getvalue = None
if stream is None:
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
stream = StringIO()
getvalue = stream.getvalue
dumper = Dumper(stream, canonical=canonical, indent=indent, width=width,
allow_unicode=allow_unicode, line_break=line_break)
for event in events:
dumper.emit(event)
if getvalue:
return getvalue()
def serialize_all(nodes, stream=None, Dumper=Dumper,
canonical=None, indent=None, width=None,
allow_unicode=None, line_break=None,
encoding='utf-8', explicit_start=None, explicit_end=None,
version=None, tags=None):
"""
Serialize a sequence of representation trees into a YAML stream.
If stream is None, return the produced string instead.
"""
getvalue = None
if stream is None:
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
stream = StringIO()
getvalue = stream.getvalue
dumper = Dumper(stream, canonical=canonical, indent=indent, width=width,
allow_unicode=allow_unicode, line_break=line_break,
encoding=encoding, version=version, tags=tags,
explicit_start=explicit_start, explicit_end=explicit_end)
dumper.open()
for node in nodes:
dumper.serialize(node)
dumper.close()
if getvalue:
return getvalue()
def serialize(node, stream=None, Dumper=Dumper, **kwds):
"""
Serialize a representation tree into a YAML stream.
If stream is None, return the produced string instead.
"""
return serialize_all([node], stream, Dumper=Dumper, **kwds)
def dump_all(documents, stream=None, Dumper=Dumper,
default_style=None, default_flow_style=None,
canonical=None, indent=None, width=None,
allow_unicode=None, line_break=None,
encoding='utf-8', explicit_start=None, explicit_end=None,
version=None, tags=None):
"""
Serialize a sequence of Python objects into a YAML stream.
If stream is None, return the produced string instead.
"""
getvalue = None
if stream is None:
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
stream = StringIO()
getvalue = stream.getvalue
dumper = Dumper(stream, default_style=default_style,
default_flow_style=default_flow_style,
canonical=canonical, indent=indent, width=width,
allow_unicode=allow_unicode, line_break=line_break,
encoding=encoding, version=version, tags=tags,
explicit_start=explicit_start, explicit_end=explicit_end)
dumper.open()
for data in documents:
dumper.represent(data)
dumper.close()
if getvalue:
return getvalue()
def dump(data, stream=None, Dumper=Dumper, **kwds):
"""
Serialize a Python object into a YAML stream.
If stream is None, return the produced string instead.
"""