Skip to content

yaml

Wrapper for YamlDotNet.

Attributes

Functions

load(yaml_file)

Load Yaml file into YamlDotNet object.

Parameters:

Name Type Description Default
yaml_file str

file path to yaml file

required

Returns:

Type Description
YamlMappingNode

yaml node

Source code in pyrevitlib/pyrevit/coreutils/yaml.py
def load(yaml_file):
    """Load Yaml file into YamlDotNet object.

    Args:
        yaml_file (str): file path to yaml file

    Returns:
        (YamlDotNet.RepresentationModel.YamlMappingNode): yaml node
    """
    if PY3:
        with open(yaml_file, 'r', encoding="utf8") as yamlfile:
            yamlstr = libyaml.RepresentationModel.YamlStream()
            yamldata = yamlfile.read()
            yamlstr.Load(StringReader(yamldata))
            if yamlstr.Documents.Count >= 1:
                return yamlstr.Documents[0].RootNode
    else:
        with open(yaml_file, 'r') as yamlfile:
            yamlstr = libyaml.RepresentationModel.YamlStream()
            yamldata = yamlfile.read().decode('utf-8')
            yamlstr.Load(StringReader(yamldata))
            if yamlstr.Documents.Count >= 1:
                return yamlstr.Documents[0].RootNode

load_as_dict(yaml_file)

Load Yaml file into python dict object.

Parameters:

Name Type Description Default
yaml_file str

file path to yaml file

required

Returns:

Type Description
dict[str, Any]

dictionary representing yaml data

Source code in pyrevitlib/pyrevit/coreutils/yaml.py
def load_as_dict(yaml_file):
    """Load Yaml file into python dict object.

    Args:
        yaml_file (str): file path to yaml file

    Returns:
        (dict[str, Any]): dictionary representing yaml data
    """
    return _convert_yamldotnet_to_dict(load(yaml_file))

dump_dict(dict_object, yaml_file)

Save YamlDotNet object to Yaml file.

Parameters:

Name Type Description Default
dict_object dict

dict object to be serialized into yaml

required
yaml_file str

file path to yaml file

required
Source code in pyrevitlib/pyrevit/coreutils/yaml.py
def dump_dict(dict_object, yaml_file):
    """Save YamlDotNet object to Yaml file.

    Args:
        dict_object (dict): dict object to be serialized into yaml
        yaml_file (str): file path to yaml file
    """
    ybuilder = libyaml.Serialization.SerializerBuilder().Build()
    serialized_yaml = ybuilder.Serialize(dict_object)
    with codecs.open(yaml_file, 'w', 'utf-8') as yamlfile:
        yamlfile.write(serialized_yaml.replace('\r\n', '\n'))