More changes to pass around logger to parse-related calls

This commit is contained in:
Thomas Krijnen
2026-06-14 14:49:14 +02:00
parent 682bd0a4f7
commit ca99ef3af7
10 changed files with 203 additions and 105 deletions
@@ -132,10 +132,20 @@ class SchemaError(Error):
@overload
def open(
path: Union[os.PathLike, str], format: SupportedFormat = None, *, should_stream: Literal[False] = False
path: Union[os.PathLike, str],
format: SupportedFormat = None,
*,
should_stream: Literal[False] = False,
logger: Optional[logger] = None,
) -> Union[_file, sqlite]: ...
@overload
def open(path: Union[os.PathLike, str], format: SupportedFormat = None, *, should_stream: Literal[True]) -> _stream: ...
def open(
path: Union[os.PathLike, str],
format: SupportedFormat = None,
*,
should_stream: Literal[True],
logger: Optional[logger] = None,
) -> _stream: ...
@overload
def open(
path: Union[os.PathLike, str],
@@ -143,6 +153,7 @@ def open(
*,
should_stream: bool = False,
readonly: bool = False,
logger: Optional[logger] = None,
) -> Union[_file, sqlite, _stream]: ...
def open(
path: Union[os.PathLike, str],
@@ -151,11 +162,13 @@ def open(
readonly: bool = False,
mmap: bool = False,
bypass_types: Optional[Sequence[str]] = None,
logger: Optional[logger] = None,
) -> Union[_file, sqlite, _stream]:
"""Loads an IFC dataset from a filepath
:param should_stream: Whether to open the file in streaming mode. Could be useful
for reading large files.
:param logger: Logger that receives native parser messages.
You can specify a file format. If no format is given, it is guessed from
its extension.
@@ -179,8 +192,10 @@ def open(
raise FileNotFoundError(f"Path does not exist: '{path}'.")
if format is None:
format = guess_format(path)
if logger is None:
logger = ifcopenshell_wrapper.logger.Root()
if format == ".ifcXML":
f = ifcopenshell_wrapper.parse_ifcxml(str(path.absolute()))
f = ifcopenshell_wrapper.parse_ifcxml(str(path.absolute()), logger)
if f:
return file(f)
raise OSError(f"Failed to parse .ifcXML file from {path}")
@@ -189,7 +204,7 @@ def open(
with zipfile.ZipFile(path) as zf:
for name in zf.namelist():
if Path(name).suffix.lower() in (".ifc", ".ifcxml"):
return open(zf.extract(name, unzipped_path))
return open(zf.extract(name, unzipped_path), logger=logger)
else:
raise LookupError(f"No .ifc or .ifcXML file found in {path}")
if format == ".ifcSQLite":
@@ -197,9 +212,9 @@ def open(
if should_stream:
return stream(path)
if readonly: # Temporary conditional see #7131. Remove once newer builds don't segfault on Linux.
f = ifcopenshell_wrapper.open(str(path.absolute()), readonly=readonly)
f = ifcopenshell_wrapper.open(str(path.absolute()), readonly, logger)
elif bypass_types:
f = ifcopenshell_wrapper.file(ifcopenshell_wrapper.uninitialized_tag())
f = ifcopenshell_wrapper.file(ifcopenshell_wrapper.uninitialized_tag(), logger)
for ty in bypass_types:
f.bypass_type(ty)
if mmap:
@@ -209,9 +224,9 @@ def open(
f.initialize(str(path.absolute()))
elif mmap:
# mmap parameter is only available for builds with USE_MMAP, not used in our main builds
f = ifcopenshell_wrapper.open(str(path.absolute()), mmap=mmap) # ty: ignore[unknown-argument]
f = ifcopenshell_wrapper.open(str(path.absolute()), mmap=mmap, logger=logger) # ty: ignore[unknown-argument]
else:
f = ifcopenshell_wrapper.open(str(path.absolute()))
f = ifcopenshell_wrapper.open(str(path.absolute()), False, logger)
return file(f)
+5 -2
View File
@@ -105,7 +105,10 @@ def main(
iterators: Sequence[ifcopenshell.geom.iterator] = (),
merge_projection: bool = True,
progress_function: Callable = DO_NOTHING,
logger=None,
):
if logger is None:
logger = ifcopenshell.logger.Root()
def by_guid(g):
for f in files:
@@ -147,7 +150,7 @@ def main(
iterator_kwargs["include"] = list(
filter(has_selected_parent, sum((f.by_type(x) for x in iterator_kwargs["include"]), []))
)
return ifcopenshell.geom.iterator(geom_settings, f, **iterator_kwargs)
return ifcopenshell.geom.iterator(geom_settings, f, logger=logger, **iterator_kwargs)
# We have to keep the iterator in memory because otherwise
# the styles are cleared up.
@@ -458,7 +461,6 @@ def main(
g1.appendChild(g2)
if settings.arrange_spaces or settings.arrange_zones:
if settings.storey_filter:
# delete storey groups not selected by filter
# sometimes happens in case of elements protruding multiple stories
@@ -541,6 +543,7 @@ def main(
arranged = W.arrange_polygons(
*filter(None, (ARRANGE_POLYGON_SETTINGS,)),
polies, # ty: ignore[too-many-positional-arguments]
logger,
)
svg_data_3 = W.polygons_to_svg(arranged, False)
dom3 = parseString(svg_data_3)
@@ -299,13 +299,16 @@ class iterator(ifcopenshell_wrapper.Iterator):
include: Optional[Union[list[entity_instance], list[str]]] = None,
exclude: Optional[Union[list[entity_instance], list[str]]] = None,
geometry_library: GEOMETRY_LIBRARY = "opencascade",
logger=None,
):
self.settings = settings
if logger is None:
logger = ifcopenshell_wrapper.logger.Root()
if isinstance(file_or_filename, file):
self.file = file
file_or_filename = file_or_filename.wrapped_data
else:
file_or_filename = self.file = open(file_or_filename)
file_or_filename = self.file = open(file_or_filename, logger=logger)
if include is not None and exclude is not None:
raise ValueError("include and exclude cannot be specified simultaneously")
@@ -334,11 +337,17 @@ class iterator(ifcopenshell_wrapper.Iterator):
initializer = ifcopenshell_wrapper.construct_iterator_with_include_exclude
self.this = initializer(
geometry_library, self.settings, file_or_filename, include_or_exclude, include is not None, num_threads
geometry_library,
self.settings,
file_or_filename,
include_or_exclude,
include is not None,
num_threads,
logger,
)
else:
self.this = ifcopenshell_wrapper.construct_iterator(
geometry_library, self.settings, file_or_filename, num_threads
geometry_library, self.settings, file_or_filename, num_threads, logger
)
if has_occ:
@@ -564,6 +573,7 @@ def iterate(
cache: Optional[str] = None,
serializer_settings: Optional[serializer_settings] = None,
geometry_library: GEOMETRY_LIBRARY = "opencascade",
logger=None,
) -> Generator[IteratorOutput, None, None]: ...
@overload
def iterate(
@@ -577,6 +587,7 @@ def iterate(
cache: Optional[str] = None,
serializer_settings: Optional[serializer_settings] = None,
geometry_library: GEOMETRY_LIBRARY = "opencascade",
logger=None,
) -> Generator[tuple[int, IteratorOutput], None, None]: ...
@overload
def iterate(
@@ -590,6 +601,7 @@ def iterate(
cache: Optional[str] = None,
serializer_settings: Optional[serializer_settings] = None,
geometry_library: GEOMETRY_LIBRARY = "opencascade",
logger=None,
) -> Generator[Union[IteratorOutput, tuple[int, IteratorOutput]], None, None]: ...
def iterate(
settings: settings,
@@ -602,13 +614,14 @@ def iterate(
cache: Optional[str] = None,
serializer_settings: Optional[serializer_settings] = None,
geometry_library: GEOMETRY_LIBRARY = "opencascade",
logger=None,
) -> Generator[Union[IteratorOutput, tuple[int, IteratorOutput]], None, None]:
"""Get a geometry iterator for the provided file.
:param cache: .h5 cache filepath (might not exist, will be created).
:param serializer_settings: Settings for cache serializer. Required if `cache` is provided.
"""
it = iterator(settings, file_or_filename, num_threads, include, exclude, geometry_library)
it = iterator(settings, file_or_filename, num_threads, include, exclude, geometry_library, logger)
if cache:
assert serializer_settings, "`serializer_settings` argument is not optional if `cache` is provided."
hdf5_cache = serializers.hdf5(cache, settings, serializer_settings)