|
4 | 4 | import re |
5 | 5 | from typing import Any |
6 | 6 |
|
| 7 | +from allotropy.exceptions import AllotropeValidationError |
| 8 | + |
7 | 9 | ALLOTROPE_DIR: Path = Path(__file__).parent.parent |
8 | 10 | ALLOTROPY_DIR: Path = ALLOTROPE_DIR.parent |
9 | 11 | ROOT_DIR: Path = ALLOTROPE_DIR.parent.parent.parent |
@@ -69,6 +71,16 @@ def get_schema_path_from_manifest(manifest: str) -> Path: |
69 | 71 | return Path(f"adm/{match.groups()[0]}.schema.json") |
70 | 72 |
|
71 | 73 |
|
| 74 | +def get_schema_path_from_asm(asm_dict: Mapping[str, Any]) -> Path: |
| 75 | + if "$asm.manifest" not in asm_dict: |
| 76 | + msg = "File is not valid ASM - missing $asm.manifest field" |
| 77 | + raise AllotropeValidationError(msg) |
| 78 | + if not isinstance(asm_dict["$asm.manifest"], str): |
| 79 | + msg = f"File is not valid ASM - $asm.manifest is not a string: {asm_dict['$asm.manifest']}" |
| 80 | + raise AllotropeValidationError(msg) |
| 81 | + return get_schema_path_from_manifest(asm_dict["$asm.manifest"]) |
| 82 | + |
| 83 | + |
72 | 84 | def get_schema_path_from_reference(reference: str) -> Path: |
73 | 85 | ref_match = re.match(r"http://purl.allotrope.org/json-schemas/(.*)", reference) |
74 | 86 | if not ref_match: |
@@ -111,7 +123,7 @@ def get_import_path_from_path(model_path: Path) -> str: |
111 | 123 |
|
112 | 124 |
|
113 | 125 | def get_model_class_from_schema(asm: Mapping[str, Any]) -> Any: |
114 | | - schema_path = get_schema_path_from_manifest(asm["$asm.manifest"]) |
| 126 | + schema_path = get_schema_path_from_asm(asm) |
115 | 127 | model_path = get_model_path_from_schema_path(Path(schema_path)) |
116 | 128 | import_path = get_import_path_from_path(model_path) |
117 | 129 | # NOTE: it is safe to assume that every schema module has Model, as we generate this code. |
|
0 commit comments