-
-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathmetdata.py
More file actions
456 lines (351 loc) · 12.8 KB
/
metdata.py
File metadata and controls
456 lines (351 loc) · 12.8 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
from dataclasses import dataclass, field
from pathlib import Path
from typing import List, Tuple, Optional, Dict
import glob
import yaml
import itertools
from paths import PRECICE_TESTS_DIR, PRECICE_TUTORIAL_DIR
# Import TutorialSource from systemtests.sources (used for external tutorial sources).
from systemtests.sources import TutorialSource
@dataclass
class BuildArgument:
"""Represents a BuildArgument needed to run the docker container"""
description: str
"""The description of the parameter."""
key: str
"""The name of the parameter."""
value_options: Optional[list] = None
"""The optinal list of value options for the parameter. If none is suplied all values are accepted"""
default: Optional[str] = None
"""The default value for the parameter."""
@property
def required(self) -> bool:
"""
Check if the BuildArgument need to be supplied via CommandLineArgs
Returns:
bool: True if the parameter is required, False otherwise.
"""
return False if self.default else True
def __eq__(self, other) -> bool:
if isinstance(other, BuildArgument):
return self.key == other.key
return False
def __hash__(self) -> int:
return hash(self.key)
def __repr__(self) -> str:
return f"{self.key}"
class BuildArguments:
"""Represents a collection of build_arguments used to built the docker images."""
def __init__(self, arguments: List[BuildArgument]):
self.arguments = arguments
@classmethod
def from_components_yaml(cls, data):
"""
Create a list of Paramters from the components YAML data.
Args:
data: The components YAML data.
"""
arguments = []
for argument_name, argument_dict in data['build_arguments'].items():
# TODO maybe **params
description = argument_dict.get(
'description', f"No description provided for {argument_name}")
key = argument_name
default = argument_dict.get('default', None)
value_options = argument_dict.get('value_options', None)
arguments.append(BuildArgument(
description, key, value_options, default))
return cls(arguments)
def __iter__(self):
return iter(self.arguments)
def __getitem__(self, index):
return self.arguments[index]
def __setitem__(self, index, value):
self.arguments[index] = value
def __len__(self):
return len(self.arguments)
def __repr__(self) -> str:
return f"{self.arguments}"
@dataclass
class Component:
"""
Represents a component like e.g the openfoam-adapter
"""
name: str
template: str
repository: str
parameters: BuildArguments
def __eq__(self, other):
if isinstance(other, Component):
return self.name == other.name
return False
def __repr__(self) -> str:
return f"{self.name}"
class Components(list):
"""
Represents the collection of components read in from the components.yaml
"""
def __init__(self, components: List[Component]):
self.components = components
@classmethod
def from_yaml(cls, path):
"""
Creates a Components instance from a YAML file.
Args:
path: The path to the YAML file.
Returns:
An instance of Components.
"""
components = []
with open(path, 'r') as f:
data = yaml.safe_load(f)
for component_name in data:
parameters = BuildArguments.from_components_yaml(
data[component_name])
repository = data[component_name]["repository"]
template = data[component_name]["template"]
components.append(
Component(component_name, template, repository, parameters))
return cls(components)
def __iter__(self):
return iter(self.components)
def __getitem__(self, index):
return self.components[index]
def __setitem__(self, index, value):
self.components[index] = value
def __len__(self):
return len(self.components)
def get_by_name(self, name_to_search):
"""
Retrieves a component by its name.
Args:
name_to_search: The name of the component to search for.
Returns:
The component with the specified name, or None if not found.
"""
for component in self.components:
if component.name == name_to_search:
return component
return None
@dataclass
class Participant:
"""Represents a participant in a coupled simulation"""
name: str
"""The name of the participant."""
def __eq__(self, other) -> bool:
if isinstance(other, Participant):
return self.name == other.name
return False
def __repr__(self) -> str:
return f"{self.name}"
# Forward declaration of tutorial
class Tutorial:
pass
@dataclass
class Case:
"""
Represents a case inside of a tutorial.
"""
name: str
participant: str
path: Path
run_cmd: str
tutorial: Tutorial = field(init=False)
component: Component
def __post_init__(self):
"""
Performs sanity checks after initializing the Case instance.
"""
if not self.component:
raise Exception(
f'Tried to instantiate the case {self.name} but failed. Reason: Could not find the component it uses in the components.yaml file.')
@classmethod
def from_dict(cls, name, dict, available_components):
"""
Creates a Case instance from a the tutorial yaml dict.
Args:
name: The name of the case.
dict: The dictionary containing the case data.
available_components: Components read from the components.yaml file
Returns:
An instance of the Case but without the tutorial set, this needs to be done later
"""
participant = dict["participant"]
path = Path(dict["directory"])
run_cmd = dict["run"]
component = available_components.get_by_name(dict["component"])
return cls(name, participant, path, run_cmd, component)
def __repr__(self) -> str:
return f"{self.name}"
def __hash__(self) -> int:
return hash(f"{self.name, self.participant, self.component, self.tutorial}")
def __eq__(self, other) -> bool:
if isinstance(other, Case):
return (
self.name == other.name) and (
self.participant == other.participant) and (
self.component == other.component) and (
self.tutorial == other.tutorial)
return False
@dataclass
class CaseCombination:
"""Represents a case combination able to run the tutorial"""
cases: Tuple[Case]
tutorial: Tutorial
def __eq__(self, other) -> bool:
if isinstance(other, CaseCombination):
return set(self.cases) == set(other.cases)
return False
def __repr__(self) -> str:
return f"{self.cases}"
@classmethod
def from_string_list(cls, case_names: List[str], tutorial: Tutorial):
cases = []
for case_name in case_names:
cases.append(tutorial.get_case_by_string(case_name))
return cls(tuple(cases), tutorial)
@classmethod
def from_cases_tuple(cls, cases: Tuple[Case], tutorial: Tutorial):
return cls(cases, tutorial)
@dataclass
class ReferenceResult:
path: Path
case_combination: CaseCombination
base_dir: Optional[Path] = None
def __repr__(self) -> str:
return f"{self.path.as_posix()}"
def __post_init__(self):
# built full path
base = self.base_dir if self.base_dir is not None else PRECICE_TUTORIAL_DIR
self.path = Path(base) / self.path
@dataclass
class Tutorial:
"""
Represents a tutorial with various attributes and methods.
"""
name: str
path: Path
url: str
participants: List[str]
cases: List[Case]
source: "TutorialSource" = field(default_factory=TutorialSource.local)
case_combinations: List[CaseCombination] = field(init=False)
def __post_init__(self):
for case in self.cases:
case.tutorial = self
# get all case combinations
def get_all_possible_case_combinations(tutorial: Tutorial):
case_combinations = []
cases_dict = {}
for participant in tutorial.participants:
cases_dict[participant] = []
for case in tutorial.cases:
cases_dict[case.participant].append(case)
for combination in itertools.product(*[cases_dict[participant] for participant in tutorial.participants]):
case_combinations.append(CaseCombination.from_cases_tuple(combination, self))
return case_combinations
self.case_combinations = get_all_possible_case_combinations(self)
def __eq__(self, other) -> bool:
if isinstance(other, Tutorial):
return (self.name == other.name) and (self.path == other.path)
return False
def __hash__(self) -> int:
return hash(self.path)
def __repr__(self) -> str:
"""
Returns a string representation of the Tutorial.
"""
return f"""\n{self.name}:
Path: {self.path}
URL: {self.url}
Participants: {self.participants}
Cases: {self.cases}
"""
def get_case_by_string(self, case_name: str) -> Optional[Case]:
"""
Retrieves Optional case based on the case_name
Args:
case_name: the name of the case in search
Returns:
Either None or a Case mathing the casename
"""
for case in self.cases:
if case.name == case_name:
return case
return None
@classmethod
def from_yaml(cls, path, available_components, base_dir=None, source=None):
"""
Creates a Tutorial instance from a YAML file.
Args:
path: The path to the metadata.yaml file.
available_components: The Components instance containing available components.
base_dir: Optional base directory for resolving tutorial path (for external sources).
Defaults to PRECICE_TUTORIAL_DIR.
source: Optional TutorialSource (for external tutorials).
Returns:
An instance of Tutorial.
"""
with open(path, 'r') as f:
data = yaml.safe_load(f)
name = data['name']
base = base_dir if base_dir is not None else PRECICE_TUTORIAL_DIR
tutorial_path = Path(base) / data['path']
url = data['url']
participants = data.get('participants', [])
cases_raw = data.get('cases', {})
cases = []
for case_name in cases_raw.keys():
cases.append(Case.from_dict(
case_name, cases_raw[case_name], available_components))
tut = cls(name, tutorial_path, url, participants, cases)
if source is not None:
tut.source = source
return tut
class Tutorials(list):
"""
Represents a collection of tutorials.
"""
def __iter__(self):
return iter(self.tutorials)
def __getitem__(self, index):
return self.tutorials[index]
def __setitem__(self, index, value):
self.tutorials[index] = value
def __len__(self):
return len(self.tutorials)
def __init__(self, tutorials: List[Tutorial]):
"""
Initializes the Tutorials instance with a base path and a list of tutorials.
Args:
path: The path to the folder containing the tutorial folders.
tutorials: The list of tutorials.
"""
self.tutorials = tutorials
def get_by_path(self, relative_path: str) -> Optional[Tutorial]:
"""
Retrieves a Tutorial by its relative path.
Args:
path_to_search: The path of the Tutorial to search for.
Returns:
The Tutorial with the specified path, or None if not found.
"""
for tutorial in self.tutorials:
if tutorial.path.name == relative_path:
return tutorial
return None
@classmethod
def from_path(cls, path):
"""
Read ins all the metadata.yaml files available in path/*/metadata.yaml
Args:
path: The path containing the tutorial folders
"""
yaml_files = glob.glob(f'{path}/*/metadata.yaml')
tutorials = []
available_components = Components.from_yaml(
PRECICE_TESTS_DIR / "components.yaml")
for yaml_path in yaml_files:
tut = Tutorial.from_yaml(yaml_path, available_components)
tutorials.append(tut)
return cls(tutorials)