Input yaml file
kind: Testing
metadata:
name: test-file
annotations:
purpose: To test the deployed code
spec:
containers:
- name: client
image: {{.registry}}/xyz:{{.client}}
env:
{{ if ne .proxy "" }}
- name: http_proxy
value: "{{.proxy}}"
{{ end -}}
I want to load all yaml content except the placeholder content in a dictionary. How can I achieve it? Can I use any regular expression to filter placeholders.
I tried using the following code, it works for the yaml which does not have placeholder values but gives parsing error with above yaml.
def __load_yaml(filename):
with open(filename, 'r') as stream:
try:
return yaml.load(stream, Loader=yaml.FullLoader)
except yaml.YAMLError as exception:
raise exception
def main():
data = {}
data.update(__load_yaml(file))
print(data)
if __name__ == '__main__':
main()
I also tried this, it is loading yaml to dictionary, but also giving FileNotFoundError. Is there any way to read list as stream? or any suggestion how can I achieve it?:
def __load_yaml(filename):
with open(filename, 'r') as stream:
try:
data = []
for text in stream:
match = re.search(r'\{\{.*?\}\}', text)
if not match and text != None:
data.append(text)
with open(str(data), 'r') as stream:
return yaml.load(stream, Loader=yaml.FullLoader)
except yaml.YAMLError as exception:
raise exception
def __load_yaml(filename):
with open(filename, "r") as stream:
string = stream.read()
# Find placeholders
reg_one = re.findall(r':\{\{.*?\}\}', string)
reg_two = re.findall(r'\{\{.*?\}\}', string)
placeholders = reg_one + reg_two
# Replace placeholders
for placeholder in placeholders:
string = string.replace(placeholder, "")
try:
return yaml.load(string, Loader=yaml.FullLoader)
except yaml.YAMLError as exception:
raise exception