misc: Add dump()

Add misc.dump(), which takes an iterable, assumes all elements have a
dump() method, and calls it on all of them.

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2020-04-23 10:34:28 +02:00
commit c63d1a729e

View file

@ -4,7 +4,7 @@ import tempfile
import filecmp
import inspect
import importlib
from typing import Set
from typing import Set, Iterable
from jwutils import log
_tmpfiles: Set[str] = set()
@ -108,4 +108,16 @@ def commit_tmpfile(tmp: str, path: str) -> None: # export
log.slog(log.NOTICE, "saving {}".format(path), caller=caller)
os.rename(path + '.tmp', path)
def dump(prio: int, objects: Iterable, *args, **kwargs) -> None: # export
caller = log.get_caller_pos(kwargs=kwargs)
log.slog(prio, ",---------- {}".format(' '.join(args)), caller=caller)
prefix = " | "
log.append_to_prefix(prefix)
i = 1
for o in objects:
o.dump(prio, "{} ({})".format(i, o.__class__.__name__), caller=caller, **kwargs)
i += 1
log.remove_from_prefix(prefix)
log.slog(prio, "`---------- {}".format(' '.join(args)), caller=caller)
atexit.register(_cleanup)