Created
April 17, 2024 13:48
-
-
Save matheus-rossi/eca7d49bb767215dacbbd6e6f6569ff6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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