misc.dump_dict(): Add function
Add a dump_dict() function to log the contents of a mapping in a structured way. Useful for debugging. Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
20a8ab91f7
commit
30f7b17e03
1 changed files with 23 additions and 1 deletions
|
|
@ -15,7 +15,7 @@ from jw.pkg.lib.Types import LoadTypes
|
|||
from . import log
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from typing import Any
|
||||
from typing import Any, Mapping
|
||||
|
||||
def update_symlink(target: str, link_name: str) -> None:
|
||||
try:
|
||||
|
|
@ -200,6 +200,28 @@ def multi_regex_edit(spec, strings: Iterable[str]) -> list[str]: # export
|
|||
def is_multi_value(obj: Any) -> bool: # export
|
||||
return isinstance(obj, Iterable) and not isinstance(obj, (str, bytes))
|
||||
|
||||
def dump_dict(prio: int, obj: Mapping | None, *args, **kwargs) -> None: # export
|
||||
import json
|
||||
caller = log.get_caller_pos(kwargs = kwargs)
|
||||
header = '---------- {}'.format(' '.join(args))
|
||||
log.log(prio, f',{header}', caller = caller)
|
||||
out = 'None' if obj is None else json.dumps(
|
||||
obj,
|
||||
sort_keys = True,
|
||||
ensure_ascii = False,
|
||||
indent = 4,
|
||||
separators = (',', ': '),
|
||||
)
|
||||
if out.startswith('{\n'):
|
||||
import textwrap
|
||||
assert out.endswith('}')
|
||||
out = out[2:]
|
||||
out = out[:-1]
|
||||
out = textwrap.dedent(out)
|
||||
for line in out.splitlines():
|
||||
log.log(prio, f'| {line}', caller = caller)
|
||||
log.log(prio, f'`{header}', caller = caller)
|
||||
|
||||
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)
|
||||
|
|
|
|||
Loading…
Reference in a new issue