Add `indent: str` suport to `Context.pformat()` using `textwrap`

aio_abandons
Tyler Goodlet 2024-08-22 20:19:55 -04:00
parent 7859e743cc
commit 59f4024242
1 changed files with 12 additions and 2 deletions

View File

@ -46,6 +46,7 @@ from dataclasses import (
from functools import partial from functools import partial
import inspect import inspect
from pprint import pformat from pprint import pformat
import textwrap
from typing import ( from typing import (
Any, Any,
AsyncGenerator, AsyncGenerator,
@ -335,6 +336,7 @@ class Context:
extra_fields: dict[str, Any]|None = None, extra_fields: dict[str, Any]|None = None,
# ^-TODO-^ some built-in extra state fields # ^-TODO-^ some built-in extra state fields
# we'll want in some devx specific cases? # we'll want in some devx specific cases?
indent: str|None = None,
) -> str: ) -> str:
ds: str = '=' ds: str = '='
@ -354,7 +356,6 @@ class Context:
show_error_fields=True show_error_fields=True
) )
fmtstr: str = ( fmtstr: str = (
f'<Context(\n'
# f'\n' # f'\n'
# f' ---\n' # f' ---\n'
f' |_ipc: {self.dst_maddr}\n' f' |_ipc: {self.dst_maddr}\n'
@ -401,11 +402,20 @@ class Context:
f' {key}{ds}{val!r}\n' f' {key}{ds}{val!r}\n'
) )
if indent:
fmtstr = textwrap.indent(
fmtstr,
prefix=indent,
)
return ( return (
'<Context(\n'
+
fmtstr fmtstr
+ +
')>\n' f'{indent})>\n'
) )
# NOTE: making this return a value that can be passed to # NOTE: making this return a value that can be passed to
# `eval()` is entirely **optional** dawggg B) # `eval()` is entirely **optional** dawggg B)
# https://docs.python.org/3/library/functions.html#repr # https://docs.python.org/3/library/functions.html#repr