diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b83b7b..6d6a77d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,3 +3,4 @@ MVP nodes: - `Format-Dict from Text` - `Format-Dict Preview` +- `String Formatter` diff --git a/__init__.py b/__init__.py index 1a01136..df80d2b 100644 --- a/__init__.py +++ b/__init__.py @@ -6,14 +6,17 @@ import typing as _t from .node_dict_from_text import StringConstructorDictFromText from .node_dict_preview import StringConstructorDictPreview +from .node_formatter import StringConstructorFormatter NODE_CLASS_MAPPINGS: _t.Dict[str, type] = { 'StringConstructorDictFromText': StringConstructorDictFromText, 'StringConstructorDictPreview': StringConstructorDictPreview, + 'StringConstructorFormatter': StringConstructorFormatter, } NODE_DISPLAY_NAME_MAPPINGS: _t.Dict[str, str] = { 'StringConstructorDictFromText': "Format-Dict from Text", 'StringConstructorDictPreview': "Preview Format-Dict", + 'StringConstructorFormatter': "String Formatter", } __all__ = ['NODE_CLASS_MAPPINGS', 'NODE_DISPLAY_NAME_MAPPINGS'] diff --git a/funcs_formatter.py b/funcs_formatter.py new file mode 100644 index 0000000..433c635 --- /dev/null +++ b/funcs_formatter.py @@ -0,0 +1,24 @@ +# encoding: utf-8 +""" +The actual code (MVC pattern) for `StringConstructorFormatter` node. +""" + +import typing as _t + +from .enums import T as _T +from .funcs_common import _show_text_on_node, _verify_input_dict + + +def formatter(template: str, format_dict: _t.Dict[str, _T] = None, show: bool = True, unique_id: str = None) -> _t.Tuple[str]: + format_dict = _verify_input_dict(format_dict) + if not isinstance(template, str): + raise TypeError(f"Not a string: {template!r}") + out_text = template.format_map(format_dict) + print(template) + print(format_dict) + print(out_text) + + if show and unique_id: + _show_text_on_node(out_text, unique_id) + + return (out_text, ) diff --git a/node_formatter.py b/node_formatter.py new file mode 100644 index 0000000..c80fed2 --- /dev/null +++ b/node_formatter.py @@ -0,0 +1,68 @@ +# encoding: utf-8 +""" +""" + +import typing as _t + +from inspect import cleandoc as _cleandoc + +from frozendict import deepfreeze as _deepfreeze + +from comfy.comfy_types.node_typing import IO as _IO + +from . import _meta +from .docstring_formatter import format_docstring as _format_docstring +from .enums import DataTypes as _DataTypes +from .funcs_formatter import formatter as _formatter + + +# A tiny optimization by reusing the same immutable dict: +_input_types = _deepfreeze({ + 'required': { + 'template': (_IO.STRING, {'multiline': True, 'tooltip': ( + "Type the text template. " + "To reference named substrings from format-dictionary, use this syntax: {substring_name}. For example:\n\n" + "score_9, score_8_up, score_7_up, {char1_short}, standing next to {char2_short},\n" + "{char1_long}\n{char2_long}" + )}), + 'show_status': (_IO.BOOLEAN, {'default': True, 'label_on': 'formatted string', 'label_off': 'no', 'tooltip': ( + "Show the final string constructed from the text-template and format-dictionary?" + )}), + }, + 'optional': { + 'str_dict': _DataTypes.input_str_dict(tooltip=( + "The dictionary to take named sub-strings from. It could be left unconnected, if the pattern doesn't reference " + "any sub-strings - then, this node acts exactly the same as a regular string-primitive node." + )), + }, + 'hidden': { + 'unique_id': 'UNIQUE_ID', # used for text display at the bottom of the node + }, +}) + + +class StringConstructorFormatter: + """ + Construct the formatted string from template and format-dictionary. + """ + NODE_NAME = 'StringConstructorFormatter' + CATEGORY = _meta.category + DESCRIPTION = _format_docstring(_cleandoc(__doc__)) + + OUTPUT_NODE = True + + FUNCTION = 'main' + RETURN_TYPES = (_IO.STRING, ) + RETURN_NAMES = ('string', ) + # OUTPUT_TOOLTIPS = tuple() + + @classmethod + def INPUT_TYPES(cls): + return _input_types + + def main( + self, template: str, show_status: bool, + str_dict: _t.Dict[str, _t.Any] = None, + unique_id: str = None, + ): + return _formatter(template, str_dict, show=show_status, unique_id=unique_id)