Skip to content

Commit 4e3f42b

Browse files
committed
fix yaml handling
1 parent 8efc27e commit 4e3f42b

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

bin/upgrade.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ def upgrade_generators_yaml(problem_path: Path, bar: ProgressBar) -> None:
5555
generators_yaml = problem_path / "generators" / "generators.yaml"
5656
if not generators_yaml.is_file():
5757
return
58-
data = read_yaml(generators_yaml)
59-
if data is None or not isinstance(data, dict):
58+
yaml_data = read_yaml(generators_yaml)
59+
if yaml_data is None or not isinstance(yaml_data, dict):
6060
return
6161

6262
changed = False
@@ -67,17 +67,19 @@ def upgrade_generators_yaml(problem_path: Path, bar: ProgressBar) -> None:
6767
("invalid_outputs", "invalid_output"),
6868
("valid_outputs", "valid_output"),
6969
]
70-
for old_name, new_name in rename:
71-
if old_name in data:
72-
if new_name in data:
73-
bar.error(
74-
f"can't rename 'data.{old_name}', 'data.{new_name}' already exists in generators.yaml",
75-
resume=True,
76-
)
77-
continue
78-
bar.log(f"renaming 'data.{old_name}' to 'data.{new_name}' in generators.yaml")
79-
ryaml_replace(data, old_name, new_name)
80-
changed = True
70+
if "data" in yaml_data and isinstance(yaml_data["data"], dict):
71+
data = yaml_data["data"]
72+
for old_name, new_name in rename:
73+
if old_name in data:
74+
if new_name in data:
75+
bar.error(
76+
f"can't rename 'data.{old_name}', 'data.{new_name}' already exists in generators.yaml",
77+
resume=True,
78+
)
79+
continue
80+
bar.log(f"renaming 'data.{old_name}' to 'data.{new_name}' in generators.yaml")
81+
ryaml_replace(data, old_name, new_name)
82+
changed = True
8183

8284
def upgrade_generated_testdata_yaml(data: dict[str, Any], path: str) -> bool:
8385
changed = False
@@ -111,10 +113,10 @@ def upgrade_generated_testdata_yaml(data: dict[str, Any], path: str) -> bool:
111113
)
112114
return changed
113115

114-
changed |= upgrade_generated_testdata_yaml(data, "")
116+
changed |= upgrade_generated_testdata_yaml(yaml_data, "")
115117

116118
if changed:
117-
write_yaml(data, generators_yaml)
119+
write_yaml(yaml_data, generators_yaml)
118120

119121

120122
def upgrade_statement(problem_path: Path, bar: ProgressBar) -> None:

bin/util.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ def ryaml_filter(data: Any, remove: str) -> Any:
729729

730730
curr = data
731731
prev_key = list(data.keys())[remove_index - 1]
732-
while isinstance(curr[prev_key], list | dict):
732+
while isinstance(curr[prev_key], list | dict) and len(curr[prev_key]):
733733
# Try to remove the comment from the last element in the preceding list/dict
734734
curr = curr[prev_key]
735735
if isinstance(curr, list):
@@ -741,7 +741,7 @@ def ryaml_filter(data: Any, remove: str) -> Any:
741741
# Move the comment that belongs to the removed key (which comes _after_ the removed key)
742742
# to the preceding key
743743
curr.ca.items[prev_key] = data.ca.items.pop(remove)
744-
elif prev_key in data.ca.items:
744+
elif prev_key in curr.ca.items:
745745
# If the removed key does not have a comment,
746746
# the comment after the previous key should be removed
747747
curr.ca.items.pop(prev_key)
@@ -755,7 +755,9 @@ def ryaml_replace(data: Any, old_key: str, new_key: str, new_value: Any = None)
755755
if new_value is None:
756756
new_value = data[old_key]
757757
data.insert(list(data.keys()).index(old_key), new_key, new_value)
758-
ryaml_filter(data, old_key)
758+
data.pop(old_key)
759+
if old_key in data.ca.items:
760+
data.ca.items[new_key] = data.ca.items.pop(old_key)
759761

760762

761763
# Only allow one thread to write at the same time. Else, e.g., generating test cases in parallel goes wrong.

0 commit comments

Comments
 (0)