Skip to content

Instantly share code, notes, and snippets.

@matheus-rossi
Created April 17, 2024 13:48
Show Gist options
  • Save matheus-rossi/eca7d49bb767215dacbbd6e6f6569ff6 to your computer and use it in GitHub Desktop.
Save matheus-rossi/eca7d49bb767215dacbbd6e6f6569ff6 to your computer and use it in GitHub Desktop.
import string, yaml
def load_yaml(file_path: str, context: dict = None):
def string_constructor(loader, node):
t = string.Template(node.value)
value = t.substitute(context)
return value
l = yaml.SafeLoader
l.add_constructor('tag:yaml.org,2002:str', string_constructor)
token_re = string.Template.pattern
l.add_implicit_resolver('tag:yaml.org,2002:str', token_re, None)
with open(file_path, 'r') as f:
x = yaml.safe_load(f)
return x
context = {'ENV': 'dev', 'SLA_HRS': 24}
yaml_with_context = load_yaml('./props.yml', context)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment