Skip to content

Commit a826da4

Browse files
committed
try to preserve the right comments
1 parent ef60033 commit a826da4

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

bin/upgrade.py

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,26 @@
55
import shutil
66
from typing import Any
77

8+
if has_ryaml:
9+
# TODO #102 The conditional import in util.py isn't picked up properly
10+
from ruamel.yaml.comments import CommentedMap, CommentedSeq
11+
12+
13+
# this is slow but tries to preserve the correct comments
14+
def _filter(data: CommentedMap, remove: str) -> None:
15+
prev = None
16+
for key in data:
17+
if key == remove:
18+
break
19+
while prev and isinstance(prev, (list, dict)):
20+
if isinstance(prev, list):
21+
prev = prev[-1]
22+
else:
23+
prev = prev[prev.keys()[-1]]
24+
if prev is not None:
25+
data.ca.items[prev] = data.ca.items.pop(key)
26+
data.pop(key)
27+
828

929
def upgrade_data(problem_path: Path, bar: ProgressBar) -> None:
1030
rename = [
@@ -143,9 +163,6 @@ def upgrade_statement(problem_path: Path, bar: ProgressBar) -> None:
143163

144164

145165
def upgrade_problem_yaml(problem_path: Path, bar: ProgressBar) -> None:
146-
# TODO #102 The conditional import in util.py isn't picked up properly
147-
from ruamel.yaml.comments import CommentedMap, CommentedSeq
148-
149166
assert (problem_path / "problem.yaml").exists()
150167
data = cast(CommentedMap, read_yaml(problem_path / "problem.yaml"))
151168
assert data is not None
@@ -173,7 +190,7 @@ def upgrade_problem_yaml(problem_path: Path, bar: ProgressBar) -> None:
173190
if not type:
174191
type.append("pass-fail")
175192
data["type"] = type if len(type) > 1 else type[0]
176-
data.pop("validation")
193+
_filter(data, "validation")
177194

178195
if "author" in data:
179196
if "credits" in data:
@@ -187,7 +204,7 @@ def upgrade_problem_yaml(problem_path: Path, bar: ProgressBar) -> None:
187204
)
188205
data["credits"] = CommentedMap()
189206
data["credits"]["authors"] = authors if len(authors) > 1 else authors[0]
190-
data.pop("author")
207+
_filter(data, "author")
191208

192209
if "source_url" in data:
193210
if "source" not in data:
@@ -200,8 +217,8 @@ def upgrade_problem_yaml(problem_path: Path, bar: ProgressBar) -> None:
200217
data["source"] = source
201218
else:
202219
bar.log("remove empty 'source(_url)' in problem.yaml")
203-
data.pop("source")
204-
data.pop("source_url")
220+
_filter(data, "source")
221+
_filter(data, "source_url")
205222

206223
if "limits" in data:
207224
limits = data["limits"]
@@ -220,21 +237,19 @@ def upgrade_problem_yaml(problem_path: Path, bar: ProgressBar) -> None:
220237
if "time_multiplier" in limits:
221238
if limits["time_multiplier"] != 2: # Skip if it's equal to the new default
222239
time_multipliers["ac_to_time_limit"] = limits["time_multiplier"]
223-
limits.pop("time_multiplier")
240+
_filter(limits, "time_multiplier")
224241

225242
if "time_safety_margin" in limits:
226243
if limits["time_safety_margin"] != 1.5: # Skip if it's equal to the new default
227244
time_multipliers["time_limit_to_tle"] = limits["time_safety_margin"]
228-
limits.pop("time_safety_margin")
245+
_filter(limits, "time_safety_margin")
229246

230247
if time_multipliers:
231248
limits["time_multipliers"] = time_multipliers
232249
# If both time multipliers are default, remove the comments (this only works if
233250
# there are no other limits configured, but that's the most common case anyway)
234251
if not limits:
235-
if "limits" in data.ca.items:
236-
data.ca.items.pop("limits")
237-
data.pop("limits")
252+
_filter(data, "limits")
238253

239254
def add_args(new_data: dict[str, Any]) -> bool:
240255
if "output_validator_args" in new_data:
@@ -245,7 +260,7 @@ def add_args(new_data: dict[str, Any]) -> bool:
245260
return False
246261
bar.log("change 'validator_flags' to 'output_validator_args' in testdata.yaml")
247262
new_data["output_validator_args"] = data["validator_flags"]
248-
data.pop("validator_flags")
263+
_filter(data, "validator_flags")
249264
return True
250265

251266
if "validator_flags" in data:

0 commit comments

Comments
 (0)