# Copyright (c) 2022-2025, The Isaac Lab Project Developers.# All rights reserved.## SPDX-License-Identifier: BSD-3-Clause"""Utilities for file I/O with yaml."""importosimportyamlfromisaaclab.utilsimportclass_to_dict
[docs]defload_yaml(filename:str)->dict:"""Loads an input PKL file safely. Args: filename: The path to pickled file. Raises: FileNotFoundError: When the specified file does not exist. Returns: The data read from the input file. """ifnotos.path.exists(filename):raiseFileNotFoundError(f"File not found: {filename}")withopen(filename)asf:data=yaml.full_load(f)returndata
[docs]defdump_yaml(filename:str,data:dict|object,sort_keys:bool=False):"""Saves data into a YAML file safely. Note: The function creates any missing directory along the file's path. Args: filename: The path to save the file at. data: The data to save either a dictionary or class object. sort_keys: Whether to sort the keys in the output file. Defaults to False. """# check endingifnotfilename.endswith("yaml"):filename+=".yaml"# create directoryifnotos.path.exists(os.path.dirname(filename)):os.makedirs(os.path.dirname(filename),exist_ok=True)# convert data into dictionaryifnotisinstance(data,dict):data=class_to_dict(data)# save datawithopen(filename,"w")asf:yaml.dump(data,f,default_flow_style=False,sort_keys=sort_keys)