-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_lld.py
More file actions
60 lines (49 loc) · 1.67 KB
/
generate_lld.py
File metadata and controls
60 lines (49 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#! /bin/env -S uv run
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "pyyaml",
# ]
# ///
import yaml
import json
import sys
def flatten_yaml(data, parent_key='', sep='_'):
"""
Recursively flattens a nested dictionayr, prefixing keys with a parent key and convetring them to uppercase
Args :
data (dict ) : The dictionary to flatten
parent_key (str) : The base key to prefix
sep (str) : The separator between keys
Returns:
dict : A flattened dictionary with prefixe keys
"""
items = []
for key, value in data.items():
new_key = f"{parent_key}{sep}{key}".upper() if parent_key else key.upper()
if isinstance(value, dict):
items.extend(flatten_yaml(value, new_key, sep=sep).items())
else:
items.append((new_key, value))
return dict(items)
def generate_lld(yaml_file):
"""
Generates a LLD JSON output from a YAML file struture and print in the console
Args:
yaml_file (str): The path to the input YAML fille
"""
# Load the YAML file
with open(yaml_file, 'r') as yf:
data = yaml.safe_load(yf)
# Flatten the YAML data
flattened_data = flatten_yaml(data)
# Create the LLD data with the desired format
lld_data = {f"{{#{key}}}": f"{value}" for key, value in flattened_data.items()}
# Print the LLD data as JSON to the console
print(json.dumps([lld_data], indent=2))
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python generate_lld.py <input_yaml_file>")
sys.exit(1)
input_yaml_file = sys.argv[1]
generate_lld(input_yaml_file)