diff --git a/src/ifcopenshell-python/ifcopenshell/__init__.py b/src/ifcopenshell-python/ifcopenshell/__init__.py index aa81dd762b..6dee2a5711 100644 --- a/src/ifcopenshell-python/ifcopenshell/__init__.py +++ b/src/ifcopenshell-python/ifcopenshell/__init__.py @@ -103,10 +103,6 @@ try: except: pass -READ_ERROR = ifcopenshell_wrapper.file_open_status.READ_ERROR -NO_HEADER = ifcopenshell_wrapper.file_open_status.NO_HEADER -UNSUPPORTED_SCHEMA = ifcopenshell_wrapper.file_open_status.UNSUPPORTED_SCHEMA - class Error(Exception): """Error used when a generic problem occurs""" @@ -162,18 +158,7 @@ def open(path: Union[os.PathLike, str], format: Optional[str] = None, should_str if should_stream: return stream(path) f = ifcopenshell_wrapper.open(str(path.absolute())) - if f.good(): - return file(f) - else: - exc, msg = { - READ_ERROR: (IOError, "Unable to open file for reading"), - NO_HEADER: (Error, "Unable to parse IFC SPF header"), - UNSUPPORTED_SCHEMA: ( - SchemaError, - "Unsupported schema: %s" % ",".join(f.header.file_schema.schema_identifiers), - ), - }[f.good().value()] - raise exc(msg) + return file(f) def create_entity(type, schema="IFC4", *args, **kwargs): diff --git a/src/ifcopenshell-python/ifcopenshell/file.py b/src/ifcopenshell-python/ifcopenshell/file.py index 000b249c5d..fd08c36b77 100644 --- a/src/ifcopenshell-python/ifcopenshell/file.py +++ b/src/ifcopenshell-python/ifcopenshell/file.py @@ -175,6 +175,10 @@ class Transaction: file_dict = {} +READ_ERROR = ifcopenshell_wrapper.file_open_status.READ_ERROR +NO_HEADER = ifcopenshell_wrapper.file_open_status.NO_HEADER +UNSUPPORTED_SCHEMA = ifcopenshell_wrapper.file_open_status.UNSUPPORTED_SCHEMA + class file: """Base class for containing IFC files. @@ -246,6 +250,18 @@ class file: else: schema = {"IFC4X3": "IFC4X3_ADD2"}.get(schema, schema) if f is not None: + if not f.good(): + from . import Error, SchemaError + + exc, msg = { + READ_ERROR: (IOError, "Unable to open file for reading"), + NO_HEADER: (Error, "Unable to parse IFC SPF header"), + UNSUPPORTED_SCHEMA: ( + SchemaError, + "Unsupported schema: %s" % ",".join(f.header.file_schema.schema_identifiers), + ), + }[f.good().value()] + raise exc(msg) self.wrapped_data = f else: args = filter(None, [schema])