Implemented reporting callbacks.

Both the dependency Tracer class and the Packer class now support a
callback object, where the latter is a subclass of the former.

For file transfers running in a separate thread, there is a thread-safe
wrapper for progress callbacks. This wrapper can be called from any thread,
and calls the wrapped callback object from the main thread. This way the
callback implementation itself doesn't have to worry about threading
issues.
This commit is contained in:
Sybren A. Stüvel
2018-03-15 17:59:55 +01:00
parent d2e13750e9
commit 889f3abd25
10 changed files with 421 additions and 68 deletions
+9 -3
View File
@@ -3,7 +3,7 @@ import pathlib
import typing
from blender_asset_tracer import blendfile
from . import result, blocks2assets, file2blocks
from . import result, blocks2assets, file2blocks, progress
log = logging.getLogger(__name__)
@@ -17,18 +17,24 @@ codes_to_skip = {
}
def deps(bfilepath: pathlib.Path) -> typing.Iterator[result.BlockUsage]:
def deps(bfilepath: pathlib.Path, progress_cb: typing.Optional[progress.Callback] = None) \
-> typing.Iterator[result.BlockUsage]:
"""Open the blend file and report its dependencies.
:param bfilepath: File to open.
:param progress_cb: Progress callback object.
"""
log.info('opening: %s', bfilepath)
bfile = blendfile.open_cached(bfilepath)
bi = file2blocks.BlockIterator()
if progress_cb:
bi.progress_cb = progress_cb
ahb = asset_holding_blocks(bi.iter_blocks(bfile))
# Sort the asset-holding blocks so that we can iterate over them
# in disk order, which is slightly faster than random order.
ahb = asset_holding_blocks(file2blocks.iter_blocks(bfile))
for block in sorted(ahb):
yield from blocks2assets.iter_assets(block)