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)
+6 -3
View File
@@ -11,13 +11,13 @@ import pathlib
import typing
from blender_asset_tracer import blendfile, bpathlib
from . import expanders
from . import expanders, progress
_funcs_for_code = {} # type: typing.Dict[bytes, typing.Callable]
log = logging.getLogger(__name__)
class _BlockIterator:
class BlockIterator:
"""Expand blocks with dependencies from other libraries.
This class exists so that we have some context for the recursive expansion
@@ -31,12 +31,15 @@ class _BlockIterator:
# Queue of blocks to visit
self.to_visit = collections.deque() # type: typing.Deque[blendfile.BlendFileBlock]
self.progress_cb = progress.Callback()
def iter_blocks(self,
bfile: blendfile.BlendFile,
limit_to: typing.Set[blendfile.BlendFileBlock] = set(),
) -> typing.Iterator[blendfile.BlendFileBlock]:
"""Expand blocks with dependencies from other libraries."""
self.progress_cb.trace_blendfile(bfile.filepath)
log.info('inspecting: %s', bfile.filepath)
if limit_to:
self._queue_named_blocks(bfile, limit_to)
@@ -148,5 +151,5 @@ class _BlockIterator:
def iter_blocks(bfile: blendfile.BlendFile) -> typing.Iterator[blendfile.BlendFileBlock]:
"""Generator, yield all blocks in this file + required blocks in libs."""
bi = _BlockIterator()
bi = BlockIterator()
yield from bi.iter_blocks(bfile)
+12
View File
@@ -0,0 +1,12 @@
"""Callback class definition for BAT Tracer progress reporting.
Mostly used to forward events to pack.progress.Callback.
"""
import pathlib
class Callback:
"""BAT Tracer progress reporting."""
def trace_blendfile(self, filename: pathlib.Path) -> None:
"""Called for every blendfile opened when tracing dependencies."""