Skip to content

yaml_fp

parse_yaml(yaml_str, error_if_empty=False)

Parse a string as yaml.

Parameters:

Name Type Description Default
yaml_str str

The string to parse.

required
error_if_empty bool

The parser may throw an error if True.

False

Returns:

Type Description
OneOf[Issue, Any]

A Good containing the parsed contents of the yaml string.

Source code in m/core/yaml_fp.py
def parse_yaml(
    yaml_str: str,
    error_if_empty: bool = False,
) -> OneOf[Issue, Any]:
    """Parse a string as yaml.

    Args:
        yaml_str: The string to parse.
        error_if_empty: The parser may throw an error if `True`.

    Returns:
        A `Good` containing the parsed contents of the yaml string.
    """
    empty = '' if error_if_empty else 'null'
    try:
        return Good(yaml.safe_load(yaml_str or empty))
    except Exception as ex:
        return issue('failed to parse the yaml data', cause=ex)

read_yson(filename, error_if_empty=False)

Read a json object from a json or yaml file.

It will choose the parser based on the filename extension.

Parameters:

Name Type Description Default
filename str

The filename to read from, if None it reads from stdin.

required
error_if_empty bool

The json parser may throw an error if True.

False

Returns:

Type Description
OneOf[Issue, Any]

A Good containing the parsed contents of the json file.

Source code in m/core/yaml_fp.py
def read_yson(
    filename: str,
    error_if_empty: bool = False,
) -> OneOf[Issue, Any]:
    """Read a json object from a json or yaml file.

    It will choose the parser based on the filename extension.

    Args:
        filename: The filename to read from, if `None` it reads from stdin.
        error_if_empty: The json parser may throw an error if `True`.

    Returns:
        A `Good` containing the parsed contents of the json file.
    """
    empty: str = '' if error_if_empty else 'null'
    file_map: dict[str, ParserFunction] = {
        '.yaml': parse_yaml,
        '.yml': parse_yaml,
        '.json': parse_json,
    }
    ext = Path(filename).suffix
    parser = file_map.get(ext, parse_json)
    context = {'filename': filename or 'SYS.STDIN'}
    return one_of(lambda: [
        json_data
        for json_str in rw.read_file(filename)
        for json_data in parser(json_str or empty, error_if_empty)
    ]).flat_map_bad(hone(f'failed to read "{ext}" file', context=context))