-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnode_dict_preview.py
More file actions
96 lines (77 loc) · 2.38 KB
/
node_dict_preview.py
File metadata and controls
96 lines (77 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# encoding: utf-8
"""
"""
import typing as _t
from inspect import cleandoc as _cleandoc
from pprint import pformat as _pformat
from frozendict import deepfreeze as _deepfreeze, frozendict as _frozendict
from . import _meta
from .docstring_formatter import format_docstring as _format_docstring
from .enums import DataTypes as _DataTypes
from .funcs_common import _show_text_on_node, _verify_input_dict_into_new
def _to_regular_dict_recursive_copy(input_dict: dict = None) -> dict:
"""
To simplify preview, turns frozen dicts back to regular ones.
Copies the dict in process and does so recursively, but not truly deep: if value is another dict,
it will be processed, too. But dicts within other objects (like lists) won't be found.
"""
if not input_dict:
return dict()
return {
k: (
_to_regular_dict_recursive_copy(v)
# In py3.10, frozendict isn't a dict, but is a `typing.Mapping`.
# So, this many types to check against:
if isinstance(v, (dict, _frozendict, _t.Mapping))
else v
)
for k, v in input_dict.items()
}
def _preview_single_entry(key: str, value) -> str:
if isinstance(value, str):
return f"{key}:\n{value}"
value_repr = _pformat(value)
return f"{key}:\n{value_repr}"
def _preview_message(input_dict: dict = None):
input_dict = _to_regular_dict_recursive_copy(_verify_input_dict_into_new(input_dict))
parts: _t.List[str] = [
_preview_single_entry(k, v) for k, v in input_dict.items()
]
return '\n\n'.join(parts)
# --------------------------------------
_dict = dict
_input_types = _deepfreeze({
'required': {},
'optional': {
'dict': _DataTypes.input_dict(tooltip="A Format-Dictionary."),
},
'hidden': {
'unique_id': 'UNIQUE_ID', # used for text display at the bottom of the node
},
})
class StringConstructorDictPreview:
"""
Show the contents of a Format-Dict.
"""
NODE_NAME = 'StringConstructorDictPreview'
CATEGORY = _meta.category_dict
DESCRIPTION = _format_docstring(_cleandoc(__doc__))
OUTPUT_NODE = True
FUNCTION = 'main'
RETURN_TYPES = (str(_DataTypes.DICT), )
RETURN_NAMES = (_DataTypes.DICT.lower(), )
# OUTPUT_TOOLTIPS = tuple()
@classmethod
def INPUT_TYPES(cls):
return _input_types
@staticmethod
def main(
dict: _t.Dict[str, _t.Any] = None,
unique_id: str = None,
):
if dict is None:
dict = _dict()
if unique_id:
status_text = _preview_message(dict)
_show_text_on_node(status_text, unique_id)
return (dict, )