5
5
import shutil
6
6
from typing import Any
7
7
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
+
8
28
9
29
def upgrade_data (problem_path : Path , bar : ProgressBar ) -> None :
10
30
rename = [
@@ -143,9 +163,6 @@ def upgrade_statement(problem_path: Path, bar: ProgressBar) -> None:
143
163
144
164
145
165
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
-
149
166
assert (problem_path / "problem.yaml" ).exists ()
150
167
data = cast (CommentedMap , read_yaml (problem_path / "problem.yaml" ))
151
168
assert data is not None
@@ -173,7 +190,7 @@ def upgrade_problem_yaml(problem_path: Path, bar: ProgressBar) -> None:
173
190
if not type :
174
191
type .append ("pass-fail" )
175
192
data ["type" ] = type if len (type ) > 1 else type [0 ]
176
- data . pop ( "validation" )
193
+ _filter ( data , "validation" )
177
194
178
195
if "author" in data :
179
196
if "credits" in data :
@@ -187,7 +204,7 @@ def upgrade_problem_yaml(problem_path: Path, bar: ProgressBar) -> None:
187
204
)
188
205
data ["credits" ] = CommentedMap ()
189
206
data ["credits" ]["authors" ] = authors if len (authors ) > 1 else authors [0 ]
190
- data . pop ( "author" )
207
+ _filter ( data , "author" )
191
208
192
209
if "source_url" in data :
193
210
if "source" not in data :
@@ -200,8 +217,8 @@ def upgrade_problem_yaml(problem_path: Path, bar: ProgressBar) -> None:
200
217
data ["source" ] = source
201
218
else :
202
219
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" )
205
222
206
223
if "limits" in data :
207
224
limits = data ["limits" ]
@@ -220,21 +237,19 @@ def upgrade_problem_yaml(problem_path: Path, bar: ProgressBar) -> None:
220
237
if "time_multiplier" in limits :
221
238
if limits ["time_multiplier" ] != 2 : # Skip if it's equal to the new default
222
239
time_multipliers ["ac_to_time_limit" ] = limits ["time_multiplier" ]
223
- limits . pop ( "time_multiplier" )
240
+ _filter ( limits , "time_multiplier" )
224
241
225
242
if "time_safety_margin" in limits :
226
243
if limits ["time_safety_margin" ] != 1.5 : # Skip if it's equal to the new default
227
244
time_multipliers ["time_limit_to_tle" ] = limits ["time_safety_margin" ]
228
- limits . pop ( "time_safety_margin" )
245
+ _filter ( limits , "time_safety_margin" )
229
246
230
247
if time_multipliers :
231
248
limits ["time_multipliers" ] = time_multipliers
232
249
# If both time multipliers are default, remove the comments (this only works if
233
250
# there are no other limits configured, but that's the most common case anyway)
234
251
if not limits :
235
- if "limits" in data .ca .items :
236
- data .ca .items .pop ("limits" )
237
- data .pop ("limits" )
252
+ _filter (data , "limits" )
238
253
239
254
def add_args (new_data : dict [str , Any ]) -> bool :
240
255
if "output_validator_args" in new_data :
@@ -245,7 +260,7 @@ def add_args(new_data: dict[str, Any]) -> bool:
245
260
return False
246
261
bar .log ("change 'validator_flags' to 'output_validator_args' in testdata.yaml" )
247
262
new_data ["output_validator_args" ] = data ["validator_flags" ]
248
- data . pop ( "validator_flags" )
263
+ _filter ( data , "validator_flags" )
249
264
return True
250
265
251
266
if "validator_flags" in data :
0 commit comments