Skip to content

Commit b8c5b32

Browse files
refactor: Add get_schema_from_asm (#1037)
1 parent 6d9ded3 commit b8c5b32

3 files changed

Lines changed: 24 additions & 1 deletion

File tree

src/allotropy/allotrope/schema_parser/path_util.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import re
55
from typing import Any
66

7+
from allotropy.exceptions import AllotropeValidationError
8+
79
ALLOTROPE_DIR: Path = Path(__file__).parent.parent
810
ALLOTROPY_DIR: Path = ALLOTROPE_DIR.parent
911
ROOT_DIR: Path = ALLOTROPE_DIR.parent.parent.parent
@@ -69,6 +71,16 @@ def get_schema_path_from_manifest(manifest: str) -> Path:
6971
return Path(f"adm/{match.groups()[0]}.schema.json")
7072

7173

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+
7284
def get_schema_path_from_reference(reference: str) -> Path:
7385
ref_match = re.match(r"http://purl.allotrope.org/json-schemas/(.*)", reference)
7486
if not ref_match:
@@ -111,7 +123,7 @@ def get_import_path_from_path(model_path: Path) -> str:
111123

112124

113125
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)
115127
model_path = get_model_path_from_schema_path(Path(schema_path))
116128
import_path = get_import_path_from_path(model_path)
117129
# NOTE: it is safe to assume that every schema module has Model, as we generate this code.

src/allotropy/allotrope/schemas.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
from collections.abc import Mapping
12
import json
23
from pathlib import Path
34
from typing import Any
45

56
from allotropy.allotrope.schema_parser.path_util import (
67
get_full_schema_path,
8+
get_schema_path_from_asm,
79
get_schema_path_from_manifest,
810
SHARED_SCHEMAS_DEFINITIONS_PATH,
911
)
@@ -51,6 +53,10 @@ def get_schema_from_manifest(manifest: str) -> dict[str, Any]:
5153
return get_schema(get_schema_path_from_manifest(manifest))
5254

5355

56+
def get_schema_from_asm(asm_dict: Mapping[str, Any]) -> dict[str, Any]:
57+
return get_schema(get_schema_path_from_asm(asm_dict))
58+
59+
5460
def get_schema_from_model(model: Any) -> dict[str, Any]:
5561
manifest = getattr(model, "manifest", getattr(model, "field_asm_manifest", None))
5662
if not manifest:

tests/allotrope/schema_parser/path_util_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
get_model_path_from_schema_path,
1010
get_rel_model_path,
1111
get_rel_schema_path,
12+
get_schema_path_from_asm,
1213
get_schema_path_from_manifest,
1314
get_schema_path_from_model_path,
1415
get_schema_path_from_reference,
@@ -54,6 +55,10 @@ def test_get_schema_path_from_manifest() -> None:
5455
assert get_schema_path_from_manifest(MANIFEST) == REL_SCHEMA_PATH
5556

5657

58+
def test_get_schema_path_from_asm() -> None:
59+
assert get_schema_path_from_asm({"$asm.manifest": MANIFEST}) == REL_SCHEMA_PATH
60+
61+
5762
def test_get_schema_path_from_reference() -> None:
5863
assert (
5964
get_schema_path_from_reference(

0 commit comments

Comments
 (0)