def load(yaml_string, schema=None, label=u"<unicode string>"):
"""
Parse the first YAML document in a string
and produce corresponding YAML object.
"""
if str(type(yaml_string)) not in ("<type 'unicode'>", "<type 'str'>", "<class 'str'>"):
raise TypeError("StrictYAML can only read a string of valid YAML.")
# We manufacture a class that has the label we want
DynamicStrictYAMLLoader = type('DynamicStrictYAMLLoader', (StrictYAMLLoader,), {"label": label})
try:
document = ruamelyaml.load(yaml_string, Loader=DynamicStrictYAMLLoader)
except ruamelyaml.YAMLError as parse_error:
if parse_error.context_mark is not None:
parse_error.context_mark.name = label
if parse_error.problem_mark is not None:
parse_error.problem_mark.name = label
raise parse_error
# Document is just a (string, int, etc.)
if type(document) not in (CommentedMap, CommentedSeq):
document = yaml_string
if schema is None:
schema = Any()
return schema(YAMLChunk(document, label=label))
评论列表
文章目录