Skip to content

Commit dfac5d1

Browse files
committed
revised default dumper object_hook
1 parent dde3420 commit dfac5d1

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

pcapkit/dumpkit/common.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
classes.
1111
1212
"""
13+
import collections
1314
import datetime
1415
import decimal
1516
import enum
@@ -19,12 +20,13 @@
1920
import aenum
2021

2122
from pcapkit.corekit.infoclass import Info
23+
from pcapkit.corekit.multidict import MultiDict, OrderedMultiDict
2224
from pcapkit.utilities.logging import logger
2325

2426
__all__ = ['make_dumper']
2527

2628
if TYPE_CHECKING:
27-
from typing import Any, TextIO, Type
29+
from typing import Any, TextIO, Type, DefaultDict
2830

2931
from dictdumper.dumper import Dumper
3032
from typing_extensions import Literal
@@ -62,14 +64,21 @@ def object_hook(self, o: 'Any') -> 'Any':
6264
return o.to_dict()
6365
if isinstance(o, (ipaddress.IPv4Address, ipaddress.IPv6Address)):
6466
return str(o)
67+
if isinstance(o, (MultiDict, OrderedMultiDict)):
68+
temp = collections.defaultdict(list) # type: DefaultDict[str, list[Any]]
69+
for key, val in o.items(multi=True):
70+
if isinstance(key, (enum.Enum, aenum.Enum)):
71+
key = f'{type(key).__name__}::{key.name} [{key.value}]'
72+
temp[key].append(val)
73+
return temp
6574
if isinstance(o, (enum.Enum, aenum.Enum)):
6675
addon = {key: val for key, val in o.__dict__.items() if not key.startswith('_')}
6776
if addon:
6877
return {
69-
'enum': f'<{type(o).__name__}::{o.name} ({o.value})',
78+
'enum': f'{type(o).__name__}::{o.name} [{o.value}]',
7079
**addon,
7180
}
72-
return f'{type(o).__name__}::{o.name} ({o.value})'
81+
return f'{type(o).__name__}::{o.name} [{o.value}]'
7382
return super(type(self), self).object_hook(o) # type: ignore[unreachable]
7483

7584
def default(self, o: 'Any') -> 'Literal["fallback"]': # pylint: disable=unused-argument

0 commit comments

Comments
 (0)