Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions src/allotropy/allotrope/path_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,13 @@ def get_schema_path_from_manifest(manifest: str) -> Path:
if not match:
msg = f"No matching schema in repo for manifest: {manifest}"
raise ValueError(msg)
return Path(f"adm/{match.groups()[0]}.schema.json")
base = match.groups()[0]
path = Path(f"adm/{base}.schema.json")
if not get_full_schema_path(path).exists():
tabular = Path(f"adm/{base}.tabular.schema.json")
if get_full_schema_path(tabular).exists():
return tabular
return path


def get_schema_path_from_asm(asm_dict: Mapping[str, Any]) -> Path:
Expand All @@ -82,7 +88,8 @@ def get_schema_path_from_reference(reference: str) -> Path:
def get_model_path_from_schema_path(schema_path: Path) -> Path:
rel_schema_path = PureWindowsPath(get_rel_schema_path(schema_path))
schema_file = rel_schema_path.name
model_file = schema_file.replace(".schema.json", ".py").replace("-", "_")
stem = schema_file.replace(".schema.json", "").replace("-", "_").replace(".", "_")
model_file = stem + ".py"
model_path = Path(
*[
re.sub("^([0-9]+)$", r"_\1", part.lower().replace("-", "_"))
Expand All @@ -95,14 +102,22 @@ def get_model_path_from_schema_path(schema_path: Path) -> Path:
def get_schema_path_from_model_path(model_path: Path) -> Path:
rel_model_path = PureWindowsPath(get_rel_model_path(model_path))
model_file = rel_model_path.name
model_file = model_file.replace(".py", ".schema.json").replace("_", "-")
schema_file = model_file.replace(".py", ".schema.json").replace("_", "-")
model_path_parts = [
re.sub("^_([0-9]+)$", r"\1", part).replace("_", "-")
for part in rel_model_path.parent.parts
]
model_path_parts[2] = model_path_parts[2].upper()
model_path = Path(*model_path_parts)
return Path(model_path, model_file)
schema_dir = Path(*model_path_parts)
path = Path(schema_dir, schema_file)
if not get_full_schema_path(path).exists():
tabular_file = schema_file.replace(
"-tabular.schema.json", ".tabular.schema.json"
)
tabular = Path(schema_dir, tabular_file)
if get_full_schema_path(tabular).exists():
return tabular
return path


def get_import_path_from_path(model_path: Path) -> str:
Expand Down
4 changes: 2 additions & 2 deletions src/allotropy/schema_gen/naming.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,10 @@ def _path_component_to_python(component: str) -> str:
"""Convert a path component to a valid Python identifier.

- Lowercase
- Replace hyphens with underscores
- Replace hyphens and dots with underscores
- Prefix digits with underscore
"""
result = component.lower().replace("-", "_")
result = component.lower().replace("-", "_").replace(".", "_")
if result and result[0].isdigit():
result = "_" + result
return result
Expand Down
25 changes: 25 additions & 0 deletions tests/allotrope/path_util_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ def test_get_schema_path_from_manifest() -> None:
assert get_schema_path_from_manifest(MANIFEST) == REL_SCHEMA_PATH


def test_get_schema_path_from_manifest_tabular_fallback() -> None:
manifest = "http://purl.allotrope.org/manifests/liquid-chromatography/REC/2023/03/liquid-chromatography.manifest"
assert get_schema_path_from_manifest(manifest) == Path(
"adm/liquid-chromatography/REC/2023/03/liquid-chromatography.tabular.schema.json"
)


def test_get_schema_path_from_asm() -> None:
assert get_schema_path_from_asm({"$asm.manifest": MANIFEST}) == REL_SCHEMA_PATH

Expand All @@ -74,12 +81,30 @@ def test_get_model_path_from_schema_path() -> None:
)


def test_get_model_path_from_schema_path_tabular() -> None:
tabular_schema = Path(
"adm/liquid-chromatography/REC/2023/03/liquid-chromatography.tabular.schema.json"
)
assert get_model_path_from_schema_path(tabular_schema) == Path(
"adm/liquid_chromatography/rec/_2023/_03/liquid_chromatography_tabular.py"
)


def test_get_schema_path_from_model_path() -> None:
assert get_schema_path_from_model_path(REL_MODEL_PATH) == Path(
"adm/plate-reader/BENCHLING/2023/09/plate-reader.schema.json"
)


def test_get_schema_path_from_model_path_tabular() -> None:
tabular_model = Path(
"adm/liquid_chromatography/rec/_2023/_03/liquid_chromatography_tabular.py"
)
assert get_schema_path_from_model_path(tabular_model) == Path(
"adm/liquid-chromatography/REC/2023/03/liquid-chromatography.tabular.schema.json"
)


def test_get_model_path_from_schema_path_windows_path() -> None:
# Replace linux / sep with \\ and test.
rel_windows_path = Path(str(REL_SCHEMA_PATH).replace("/", "\\"))
Expand Down
Loading