Made yaml template loader more generic
This commit is contained in:
25
tools/render
25
tools/render
@@ -34,7 +34,7 @@ TEMPLATES = Environment(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def node_encoder(node: dict):
|
def render_templates(node: dict):
|
||||||
class Inner(json.JSONEncoder):
|
class Inner(json.JSONEncoder):
|
||||||
def default(self, o):
|
def default(self, o):
|
||||||
if isinstance(o, Template):
|
if isinstance(o, Template):
|
||||||
@@ -70,20 +70,23 @@ def schematic_constructor(loader: yaml.SafeLoader, node: yaml.nodes.ScalarNode):
|
|||||||
raise yaml.MarkedYAMLError("Failed to load schematic", node.start_mark)
|
raise yaml.MarkedYAMLError("Failed to load schematic", node.start_mark)
|
||||||
|
|
||||||
|
|
||||||
def patch_constructor(loader: yaml.SafeLoader, node: yaml.nodes.ScalarNode):
|
def template_constructor(environment: Environment):
|
||||||
patch_name = loader.construct_scalar(node)
|
def inner(loader: yaml.SafeLoader, node: yaml.nodes.ScalarNode):
|
||||||
try:
|
patch_name = loader.construct_scalar(node)
|
||||||
template = PATCHES.get_template(f"{patch_name}.yaml")
|
try:
|
||||||
return template
|
template = environment.get_template(f"{patch_name}.yaml")
|
||||||
except Exception:
|
return template
|
||||||
raise yaml.MarkedYAMLError("Failed to load patch", node.start_mark)
|
except Exception:
|
||||||
|
raise yaml.MarkedYAMLError("Failed to load patch", node.start_mark)
|
||||||
|
|
||||||
|
return inner
|
||||||
|
|
||||||
|
|
||||||
def get_loader():
|
def get_loader():
|
||||||
"""Add special constructors to yaml loader"""
|
"""Add special constructors to yaml loader"""
|
||||||
loader = yaml.SafeLoader
|
loader = yaml.SafeLoader
|
||||||
loader.add_constructor("!schematic", schematic_constructor)
|
loader.add_constructor("!schematic", schematic_constructor)
|
||||||
loader.add_constructor("!patch", patch_constructor)
|
loader.add_constructor("!patch", template_constructor(PATCHES))
|
||||||
|
|
||||||
return loader
|
return loader
|
||||||
|
|
||||||
@@ -126,7 +129,9 @@ def main():
|
|||||||
|
|
||||||
# Quick and dirty way to resolve all the templates using a custom encoder
|
# Quick and dirty way to resolve all the templates using a custom encoder
|
||||||
nodes = list(
|
nodes = list(
|
||||||
map(lambda node: json.loads(json.dumps(node, cls=node_encoder(node))), nodes)
|
map(
|
||||||
|
lambda node: json.loads(json.dumps(node, cls=render_templates(node))), nodes
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
with open(ROOT.joinpath("config.yaml")) as fyaml:
|
with open(ROOT.joinpath("config.yaml")) as fyaml:
|
||||||
|
|||||||
Reference in New Issue
Block a user