fix: dict list

This commit is contained in:
Yann Ahlgrim
2026-06-19 10:47:28 +02:00
parent d93bb9321b
commit dcbb3609c6
+50 -39
View File
@@ -1,7 +1,6 @@
import argparse import argparse
import json import json
import os import os
import re
def _find_metric(metrics, key): def _find_metric(metrics, key):
@@ -22,12 +21,17 @@ def _find_metric(metrics, key):
def _parse_yaml_value(value): def _parse_yaml_value(value):
value = value.strip() value = value.strip()
if value == "null" or value == "~": if not value or value in ("null", "~"):
return None return None
if value == "true": if value == "true":
return True return True
if value == "false": if value == "false":
return False return False
if value.startswith("[") and value.endswith("]"):
try:
return json.loads(value)
except (json.JSONDecodeError, ValueError):
pass
try: try:
return int(value) return int(value)
except ValueError: except ValueError:
@@ -57,51 +61,58 @@ def _load_params_simple(dirpath):
def _parse_simple_yaml(text): def _parse_simple_yaml(text):
stack = [{}] lines = text.split("\n")
indent_stack = [-1]
for raw_line in text.split("\n"): def _parse_block(start, indent):
line = raw_line.rstrip() result = {}
if not line.strip() or line.strip().startswith("#"): list_items = []
is_list = False
i = start
while i < len(lines):
raw = lines[i].rstrip()
if not raw.strip() or raw.strip().startswith("#"):
i += 1
continue continue
stripped = line.lstrip(" ") stripped = raw.lstrip(" ")
indent = len(line) - len(stripped) cur_indent = len(raw) - len(stripped)
while indent <= indent_stack[-1]: if cur_indent < indent:
stack.pop()
indent_stack.pop()
if stripped.endswith(":") and not stripped.startswith("- "):
key = stripped[:-1].strip()
new_dict = {}
stack[-1][key] = new_dict
stack.append(new_dict)
indent_stack.append(indent)
elif ": " in stripped:
key, _, value = stripped.partition(": ")
key = key.strip()
value = _parse_yaml_value(value)
stack[-1][key] = value
elif stripped.startswith("- "):
item = _parse_yaml_value(stripped[2:])
if not isinstance(stack[-1], list):
parent = stack[-2] if len(stack) >= 2 else stack[-1]
for k, v in list(parent.items()):
if v is stack[-1]:
parent[k] = []
stack[-1] = parent[k]
break break
stack[-1].append(item) if cur_indent > indent:
else: i += 1
continue continue
if stripped.endswith(":") and not stripped.startswith("- "): if " #" in stripped:
pass effective = stripped[: stripped.index(" #")].rstrip()
elif ":" not in stripped or indent_stack[-1] != indent: else:
pass effective = stripped
return stack[0] if not effective:
i += 1
continue
if effective.startswith("- "):
is_list = True
list_items.append(_parse_yaml_value(effective[2:]))
i += 1
elif effective.endswith(":"):
key = effective[:-1].strip()
sub_val, i = _parse_block(i + 1, indent + 2)
result[key] = sub_val
elif ": " in effective:
key, _, val_str = effective.partition(": ")
result[key.strip()] = _parse_yaml_value(val_str)
i += 1
else:
i += 1
if is_list:
return list_items, i
return result, i
return _parse_block(0, 0)[0]
def _get_in_params(params, path): def _get_in_params(params, path):