Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add yaml include support #3133

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
19 changes: 17 additions & 2 deletions nvflare/lighter/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,26 @@ def sign_all(content_folder, signing_pri_key):
return signatures


class YamlLoader(yaml.SafeLoader):

def __init__(self, stream):

self._root = os.path.split(stream.name)[0]
super(YamlLoader, self).__init__(stream)

def include(self, node):

filename = os.path.join(self._root, self.construct_scalar(node))
with open(filename, "r") as f:
return yaml.load(f, YamlLoader)


def load_yaml(file):
YamlLoader.add_constructor("!include", YamlLoader.include)
yhwen marked this conversation as resolved.
Show resolved Hide resolved
if isinstance(file, str):
return yaml.safe_load(open(file, "r"))
return yaml.load(open(file, "r"), YamlLoader)
elif isinstance(file, bytes):
return yaml.safe_load(file)
return yaml.load(file, YamlLoader)
else:
return None

Expand Down
Loading