309
310
311
312
@@ -3290,20 +3332,39 @@
315
316
317
-318 | @staticmethod
-def parse(filename: str | Path) -> AntaCatalog:
+318
+319
+320
+321
+322
+323
+324
+325
+326
+327
+328
+329
+330
+331
+332
| @staticmethod
+def parse(filename: str | Path, file_format: Literal["yaml", "json"] = "yaml") -> AntaCatalog:
"""Create an AntaCatalog instance from a test catalog file.
Parameters
----------
- filename: Path to test catalog YAML file
+ filename: Path to test catalog YAML or JSON fil
+ file_format: Format of the file, either 'yaml' or 'json'
"""
+ if file_format not in ["yaml", "json"]:
+ message = f"'{file_format}' is not a valid format for an AntaCatalog file. Only 'yaml' and 'json' are supported."
+ raise ValueError(message)
+
try:
file: Path = filename if isinstance(filename, Path) else Path(filename)
with file.open(encoding="UTF-8") as f:
- data = safe_load(f)
- except (TypeError, YAMLError, OSError) as e:
+ data = safe_load(f) if file_format == "yaml" else json_load(f)
+ except (TypeError, YAMLError, OSError, ValueError) as e:
message = f"Unable to parse ANTA Test Catalog file '{filename}'"
anta_log_exception(e, message, logger)
raise
@@ -3904,6 +3965,67 @@
+
+ to_json
+
+
+
+
+
+
+
+ Return a JSON representation string of this model.
+
+
+ Returns:
+
+
+
+ Type |
+ Description |
+
+
+
+
+
+ The JSON representation string of this model.
+ |
+
+
+
+
+ |
+
+
+
+
+
+ Source code in anta/catalog.py
+ 243
+244
+245
+246
+247
+248
+249
+250 | def to_json(self) -> str:
+ """Return a JSON representation string of this model.
+
+ Returns
+ -------
+ The JSON representation string of this model.
+ """
+ return self.model_dump_json(serialize_as_any=True, exclude_unset=True, indent=2)
+
|
+
+
+
+
+
+
+
+
yaml
@@ -3963,7 +4085,7 @@
# This could be improved.
# https://github.com/pydantic/pydantic/issues/1043
# Explore if this worth using this: https://github.com/NowanIlfideme/pydantic-yaml
- return yaml.safe_dump(yaml.safe_load(self.model_dump_json(serialize_as_any=True, exclude_unset=True)), indent=2, width=math.inf)
+ return safe_dump(safe_load(self.model_dump_json(serialize_as_any=True, exclude_unset=True)), indent=2, width=math.inf)
|