|
| 1 | +from ..structures.core import StructureFamily |
| 2 | +from .container import Container |
| 3 | + |
| 4 | + |
| 5 | +def _walk_readables(node, nodes=None): |
| 6 | + "Walk nodes in depth first order, yielding readable nodes." |
| 7 | + if nodes is None: |
| 8 | + for child in node: |
| 9 | + yield from _walk_readables(node, [child]) |
| 10 | + else: |
| 11 | + value = node[nodes[-1]] |
| 12 | + if value.structure_family == StructureFamily.container: |
| 13 | + value = value.base |
| 14 | + yield nodes |
| 15 | + for k, v in value.items(): |
| 16 | + yield from _walk_readables(value, nodes + [k]) |
| 17 | + else: |
| 18 | + yield nodes |
| 19 | + |
| 20 | + |
| 21 | +def deepcopy(src, dst): |
| 22 | + "Copy metadata and underlying data from src to dst." |
| 23 | + src = src.new_variation(structure_clients="dask") |
| 24 | + for path in _walk_readables(src): |
| 25 | + # Use Container.__getitem__ explicitly in two places here to |
| 26 | + # work around an oddity (bug?) in bluesky-tiled-plugins. |
| 27 | + s = Container.__getitem__(src, tuple(path)) |
| 28 | + copy_func = _registry[s.structure_family] |
| 29 | + copy_func(s, Container.__getitem__(dst, tuple(path[:-1])), path[-1]) |
| 30 | + |
| 31 | + |
| 32 | +_registry = {} |
| 33 | + |
| 34 | + |
| 35 | +def _register(structure_family: StructureFamily): |
| 36 | + def f(func): |
| 37 | + _registry[structure_family] = func |
| 38 | + return func |
| 39 | + |
| 40 | + return f |
| 41 | + |
| 42 | + |
| 43 | +@_register(StructureFamily.array) |
| 44 | +def copy_array(src, dst, key): |
| 45 | + dst.write_array( |
| 46 | + src.read(), |
| 47 | + key=key, |
| 48 | + metadata=dict(src.metadata), |
| 49 | + specs=src.specs, |
| 50 | + ) |
| 51 | + |
| 52 | + |
| 53 | +@_register(StructureFamily.container) |
| 54 | +def copy_container(src, dst, key): |
| 55 | + dst.create_container( |
| 56 | + key=key, |
| 57 | + metadata=dict(src.metadata), |
| 58 | + specs=src.specs, |
| 59 | + ) |
| 60 | + |
| 61 | + |
| 62 | +@_register(StructureFamily.table) |
| 63 | +def copy_table(src, dst, key): |
| 64 | + dst.write_table( |
| 65 | + src.read(), |
| 66 | + key=key, |
| 67 | + metadata=dict(src.metadata), |
| 68 | + specs=src.specs, |
| 69 | + ) |
0 commit comments