From 9e0c6cf52478e8a4df1f02cdb277d218bf182a2d Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Thu, 16 Jul 2026 14:29:11 +0500 Subject: [PATCH] util.schema: dedupe inline schema resolution logic --- src/ifcopenshell-python/ifcopenshell/__init__.py | 7 ++++--- src/ifcopenshell-python/ifcopenshell/file.py | 7 ++++--- .../ifcopenshell/util/schema.py | 15 +++++++++++++++ 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/ifcopenshell-python/ifcopenshell/__init__.py b/src/ifcopenshell-python/ifcopenshell/__init__.py index 12c6cd6b84..0a309695df 100644 --- a/src/ifcopenshell-python/ifcopenshell/__init__.py +++ b/src/ifcopenshell-python/ifcopenshell/__init__.py @@ -305,12 +305,13 @@ def schema_by_name( you are testing non-ISO IFC releases. :return: Schema definition object. """ + import ifcopenshell.util.schema + assert schema_version or schema, "Either schema or schema_version must be specified." if schema_version: - prefixes = ("IFC", "X", "_ADD", "_TC") - schema = "".join("".join(map(str, t)) if t[1] else "" for t in zip(prefixes, schema_version)) + schema = ifcopenshell.util.schema.get_schema_name_from_version(schema_version) else: - schema = {"IFC4X3": "IFC4X3_ADD2"}.get(schema, schema) + schema = ifcopenshell.util.schema.get_schema_identifier(schema) return ifcopenshell_wrapper.schema_by_name(schema) diff --git a/src/ifcopenshell-python/ifcopenshell/file.py b/src/ifcopenshell-python/ifcopenshell/file.py index 4b6503aabf..6d327df4af 100644 --- a/src/ifcopenshell-python/ifcopenshell/file.py +++ b/src/ifcopenshell-python/ifcopenshell/file.py @@ -594,11 +594,12 @@ class file: # A poweruser testing out a particular version of IFC4X3 model = ifcopenshell.file(schema_version=(4, 3, 0, 1)) """ + import ifcopenshell.util.schema + if schema_version: - prefixes = ("IFC", "X", "_ADD", "_TC") - schema = "".join("".join(map(str, t)) if t[1] else "" for t in zip(prefixes, schema_version)) + schema = ifcopenshell.util.schema.get_schema_name_from_version(schema_version) else: - schema = {"IFC4X3": "IFC4X3_ADD2"}.get(schema, schema) + schema = ifcopenshell.util.schema.get_schema_identifier(schema) if f is not None: self.wrapped_data = f if not f.good(): diff --git a/src/ifcopenshell-python/ifcopenshell/util/schema.py b/src/ifcopenshell-python/ifcopenshell/util/schema.py index aec74ac7cc..5aa9b26fab 100644 --- a/src/ifcopenshell-python/ifcopenshell/util/schema.py +++ b/src/ifcopenshell-python/ifcopenshell/util/schema.py @@ -50,6 +50,21 @@ def get_fallback_schema(version: str) -> IFC_SCHEMA: return version +def get_schema_identifier(schema: IFC_SCHEMA) -> str: + """Resolve a general schema name to the specific identifier used internally + (as in ``file.schema_identifier``). + + E.g. ``IFC4X3`` -> ``IFC4X3_ADD2``. + """ + return {"IFC4X3": "IFC4X3_ADD2"}.get(schema, schema) + + +def get_schema_name_from_version(schema_version: tuple[int, ...]) -> str: + """Build a schema name from a version tuple, e.g. (4, 3, 0, 1) -> "IFC4X3_TC1".""" + prefixes = ("IFC", "X", "_ADD", "_TC") + return "".join("".join(map(str, t)) if t[1] else "" for t in zip(prefixes, schema_version)) + + def get_declaration(element: ifcopenshell.entity_instance): """Get the schema declaration of an actively used entity instance