diff --git a/src/allotropy/allotrope/models/adm/liquid_chromatography/rec/_2023/_03/liquid_chromatography.tabular.py b/src/allotropy/allotrope/models/adm/liquid_chromatography/rec/_2023/_03/liquid_chromatography_tabular.py similarity index 100% rename from src/allotropy/allotrope/models/adm/liquid_chromatography/rec/_2023/_03/liquid_chromatography.tabular.py rename to src/allotropy/allotrope/models/adm/liquid_chromatography/rec/_2023/_03/liquid_chromatography_tabular.py diff --git a/src/allotropy/allotrope/models/adm/liquid_chromatography/rec/_2024/_03/liquid_chromatography.tabular.py b/src/allotropy/allotrope/models/adm/liquid_chromatography/rec/_2024/_03/liquid_chromatography_tabular.py similarity index 100% rename from src/allotropy/allotrope/models/adm/liquid_chromatography/rec/_2024/_03/liquid_chromatography.tabular.py rename to src/allotropy/allotrope/models/adm/liquid_chromatography/rec/_2024/_03/liquid_chromatography_tabular.py diff --git a/src/allotropy/allotrope/path_util.py b/src/allotropy/allotrope/path_util.py index c6a44e9ec..e4790654b 100644 --- a/src/allotropy/allotrope/path_util.py +++ b/src/allotropy/allotrope/path_util.py @@ -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: @@ -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("-", "_")) @@ -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: diff --git a/src/allotropy/schema_gen/naming.py b/src/allotropy/schema_gen/naming.py index 12d85e966..a1a597dc8 100644 --- a/src/allotropy/schema_gen/naming.py +++ b/src/allotropy/schema_gen/naming.py @@ -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 diff --git a/tests/allotrope/path_util_test.py b/tests/allotrope/path_util_test.py index e0ed86fcc..d6981ca85 100644 --- a/tests/allotrope/path_util_test.py +++ b/tests/allotrope/path_util_test.py @@ -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 @@ -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("/", "\\"))