Merge branch 'dev': v1.0.0

This commit is contained in:
Lex Darlog (DRL)
2025-07-21 20:14:46 +05:00
12 changed files with 134 additions and 42 deletions
+13 -4
View File
@@ -1,3 +1,16 @@
# `TODO`
- Recursive formatting.
- Multi-item nodes.
# v1.0.0
The minimally-complete version.
New node:
- `Add ANY to Format-Dict (Advanced)`
The Format-Dict data type name changed from `STR_DICT` to just `DICT` - for potential compatibility with other nodes.
# v0.0.1
MVP nodes:
@@ -5,7 +18,3 @@ MVP nodes:
- `Format-Dict from Text`
- `Format-Dict Preview`
- `String Formatter`
⚠️ Active `TODO:`
- Recursive formatting.
- Append non-string to Format-Dict.
+3 -3
View File
@@ -5,8 +5,8 @@
# `String Constructor` (Text-Formatting) nodes
There's already a plenty of string-formatting nodes for ComfyUI. But this node pack takes a different approach:
- First, you prepare a dictionary of named sub-strings (chunks of text).
- Then, right before `CLIP Text Encode`, you construct a final prompt from these parts, using python's [string formatting syntax](https://docs.python.org/3/library/string.html#format-examples)... **Don't panic!** You don't need to be a programmer - just look below for a quick introduction.
- First, you prepare a dictionary of named sub-strings (chunks of text with assigned keyword/tag to reference them with).
- Then, right before `CLIP Text Encode`, you construct a final prompt fom this dictionary of available parts, connected **as a single input**. The compilation itself is done using python's [string formatting syntax](https://docs.python.org/3/library/string.html#format-examples)... **Don't panic!** You don't need to be a programmer - just look below for a quick introduction.
- Unlike many other _(giant uber-mega)_ node packs, this one strictly adheres to the modular philosophy: it's minimal and self-sufficient.
[🔄 Updates ChangeLog](CHANGELOG.md)
@@ -24,4 +24,4 @@ For those who are unfamiliar with python's string formatting, the rules are very
> [!NOTE]
> For advanced users:
> Internally, a built-in `str.format()` is called with keyword arguments from `str_dict`, which is literally just a dict... though, an immutable one.
> So any formatting patterns are available (like `{float_value:.3f}`). However, currently there are no nodes to append non-string values (planned, TODO).
> So any formatting patterns are available (like `{float_value:.3f}`) + `Add ANY to Format-Dict` node is there for exactly that.
+3
View File
@@ -4,18 +4,21 @@
import typing as _t
from .node_dict_add_any import StringConstructorDictAddAny
from .node_dict_add_string import StringConstructorDictAddString
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] = {
'StringConstructorDictAddAny': StringConstructorDictAddAny,
'StringConstructorDictAddString': StringConstructorDictAddString,
'StringConstructorDictFromText': StringConstructorDictFromText,
'StringConstructorDictPreview': StringConstructorDictPreview,
'StringConstructorFormatter': StringConstructorFormatter,
}
NODE_DISPLAY_NAME_MAPPINGS: _t.Dict[str, str] = {
'StringConstructorDictAddAny': "Add ANY to Format-Dict (Advanced)",
'StringConstructorDictAddString': "Add String to Format-Dict",
'StringConstructorDictFromText': "Format-Dict from Text",
'StringConstructorDictPreview': "Preview Format-Dict",
+3 -3
View File
@@ -22,7 +22,7 @@ class __BaseEnum(_StrEnum):
class DataTypes(__BaseEnum):
"""Additional data-types defined byt the node pack."""
STR_DICT = 'STR_DICT'
DICT = 'DICT'
@staticmethod
def __custom_input_type_dict(_dict_args: _t.Tuple[_t.Dict[str, T], ...], kwargs: _t.Dict[str, T]) -> _t.Dict[str, T]:
@@ -34,5 +34,5 @@ class DataTypes(__BaseEnum):
return res_dict
@classmethod
def input_str_dict(cls, *_dicts: _t.Dict[str, T], **kwargs: T) -> _t.Tuple[_t.Union['DataTypes', str], _t.Dict[str, T]]:
return cls.STR_DICT, cls.__custom_input_type_dict(_dicts, kwargs)
def input_dict(cls, *_dicts: _t.Dict[str, T], **kwargs: T) -> _t.Tuple[_t.Union['DataTypes', str], _t.Dict[str, T]]:
return cls.DICT, cls.__custom_input_type_dict(_dicts, kwargs)
+27
View File
@@ -0,0 +1,27 @@
# encoding: utf-8
"""
The actual code (MVC pattern) for `StringConstructorDictAddAny` node.
"""
import typing as _t
from frozendict import deepfreeze as _deepfreeze
from .funcs_common import _raise_from_errors_dict, _validate_key, _verify_input_dict
def set_dict_any_type_item(
name: str, value: _t.Any = None, in_dict: _t.Dict[str, _t.Any] = None,
# unique_id: str = None,
) -> _t.Tuple[_t.Dict[str, _t.Any]]:
"""Update/append an item of any type to the dict."""
out_dict: _t.Dict[str, _t.Any] = _verify_input_dict(in_dict)
if value is None:
return (_deepfreeze(in_dict), ) # No need to create another dict instance if it's already frozen
errors_dict: _t.Dict[_t.Any, str] = dict()
name = _validate_key(name, errors_dict)
_raise_from_errors_dict(errors_dict)
new_dict: _t.Dict[str, _t.Any] = {name: value}
out_dict.update(new_dict)
return (_deepfreeze(out_dict), )
+4 -3
View File
@@ -10,12 +10,13 @@ from frozendict import deepfreeze as _deepfreeze
from .enums import T as _T
from .funcs_common import _raise_from_errors_dict, _validate_key, _verify_input_dict
def set_dict_string(
name: str, value: str, strip_lines=True, in_dict: _t.Dict[str, _T] = None,
# unique_id: str = None,
):
) -> _t.Tuple[_t.Dict[str, _t.Union[_T, str]]]:
"""Update/append a string to the dict."""
out_dict: _t.Dict[str, _T] = _verify_input_dict(in_dict)
out_dict: _t.Dict[str, _t.Union[_T, str]] = _verify_input_dict(in_dict)
errors_dict: _t.Dict[_t.Any, str] = dict()
name = _validate_key(name, errors_dict)
_raise_from_errors_dict(errors_dict)
@@ -26,4 +27,4 @@ def set_dict_string(
)
new_dict: _t.Dict[str, str] = {name: value}
out_dict.update(new_dict)
return (_deepfreeze(out_dict),)
return (_deepfreeze(out_dict), )
+58
View File
@@ -0,0 +1,58 @@
# 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_dict_add_any import set_dict_any_type_item as _set_dict_any_type_item
from .node_dict_add_string import _input_types as _input_types_str
# A tiny optimization by reusing the same immutable dict:
_input_types = _deepfreeze({
'required': {
'name': (_IO.STRING, {'tooltip': (
"Name of the non-string item inserted into the dict. "
"It must comprise only of latin letters, digits and underscores + it can't start with a digit."
)}),
},
'optional': {
'dict': _input_types_str['optional']['dict'],
'value': (_IO.ANY, {"forceInput": True, 'tooltip': "The actual non-string item to add into the dict."}),
},
# 'hidden': {
# 'unique_id': 'UNIQUE_ID', # used for text display at the bottom of the node
# },
})
class StringConstructorDictAddAny:
"""Add/update a non-string item to the Format-Dict - to do some advanced formatting."""
NODE_NAME = 'StringConstructorDictAddAny'
CATEGORY = _meta.category
DESCRIPTION = _format_docstring(_cleandoc(__doc__))
FUNCTION = 'main'
RETURN_TYPES = (_DataTypes.DICT, )
RETURN_NAMES = (_DataTypes.DICT.lower(), )
# OUTPUT_TOOLTIPS = tuple()
@classmethod
def INPUT_TYPES(cls):
return _input_types
def main(
self, name: str, value: _t.Any = None,
dict: _t.Dict[str, _t.Any] = None,
# unique_id: str = None,
):
return _set_dict_any_type_item(name, value, in_dict=dict)
+7 -12
View File
@@ -23,16 +23,13 @@ _input_types = _deepfreeze({
"Name of the substring inserted into the dict. "
"It must comprise only of latin letters, digits and underscores + it can't start with a digit."
)}),
'cleanup': (_IO.BOOLEAN, {'default': True, 'label_on': 'extra spaces', 'label_off': 'no', 'tooltip': (
'cleanup': (_IO.BOOLEAN, {'default': True, 'label_on': 'leading/trailing spaces', 'label_off': 'no', 'tooltip': (
"When enabled, each line in the sub-string is stripped from any spaces at its start and end."
)}),
'string': (_IO.STRING, {'multiline': True, 'tooltip': "The actual sub-string to set into the dict."}),
'string': (_IO.STRING, {'multiline': True, 'tooltip': "The actual sub-string to add into the dict."}),
},
'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."
)),
'dict': _DataTypes.input_dict(tooltip="An optional Format-Dictionary to extend/update."),
},
# 'hidden': {
# 'unique_id': 'UNIQUE_ID', # used for text display at the bottom of the node
@@ -46,11 +43,9 @@ class StringConstructorDictAddString:
CATEGORY = _meta.category
DESCRIPTION = _format_docstring(_cleandoc(__doc__))
OUTPUT_NODE = True # Just to show the status message even if not connected to anything
FUNCTION = 'main'
RETURN_TYPES = (_DataTypes.STR_DICT, )
RETURN_NAMES = (_DataTypes.STR_DICT.lower(), )
RETURN_TYPES = (_DataTypes.DICT, )
RETURN_NAMES = (_DataTypes.DICT.lower(), )
# OUTPUT_TOOLTIPS = tuple()
@classmethod
@@ -59,7 +54,7 @@ class StringConstructorDictAddString:
def main(
self, name: str, cleanup: bool, string: str,
str_dict: _t.Dict[str, _t.Any] = None,
dict: _t.Dict[str, _t.Any] = None,
# unique_id: str = None,
):
return _set_dict_string(name, string, strip_lines=cleanup, in_dict=str_dict)
return _set_dict_string(name, string, strip_lines=cleanup, in_dict=dict)
+7 -8
View File
@@ -14,12 +14,13 @@ from . import _meta
from .docstring_formatter import format_docstring as _format_docstring
from .enums import DataTypes as _DataTypes
from .funcs_dict_from_text import parse_dict_from_text as _parse_dict_from_text
from .node_dict_add_string import _input_types as _input_types_str
# A tiny optimization by reusing the same immutable dict:
_input_types = _deepfreeze({
'required': {
'cleanup': (_IO.BOOLEAN, {'default': True, 'label_on': 'extra spaces', 'label_off': 'no', 'tooltip': (
'cleanup': (_IO.BOOLEAN, {'default': True, 'label_on': 'leading/trailing spaces', 'label_off': 'no', 'tooltip': (
"When enabled, each line in each sub-string is stripped from any spaces at its start and end."
)}),
'strings': (_IO.STRING, {'multiline': True, 'tooltip': (
@@ -32,9 +33,7 @@ _input_types = _deepfreeze({
)}),
},
'optional': {
'str_dict': _DataTypes.input_str_dict(tooltip=(
"An optional input dictionary. If it's connected, it will be expanded/updated with new strings."
)),
'dict': _input_types_str['optional']['dict'],
},
'hidden': {
'unique_id': 'UNIQUE_ID', # used for text display at the bottom of the node
@@ -53,8 +52,8 @@ class StringConstructorDictFromText:
OUTPUT_NODE = True # Just to show the status message even if not connected to anything
FUNCTION = 'main'
RETURN_TYPES = (_DataTypes.STR_DICT, )
RETURN_NAMES = (_DataTypes.STR_DICT.lower(), )
RETURN_TYPES = (_DataTypes.DICT, )
RETURN_NAMES = (_DataTypes.DICT.lower(), )
# OUTPUT_TOOLTIPS = tuple()
@classmethod
@@ -63,7 +62,7 @@ class StringConstructorDictFromText:
def main(
self, cleanup: bool, strings: str, show_status: bool,
str_dict: _t.Dict[str, _t.Any] = None,
dict: _t.Dict[str, _t.Any] = None,
unique_id: str = None,
):
return _parse_dict_from_text(strings, strip_lines=cleanup, show=show_status, in_dict=str_dict, unique_id=unique_id)
return _parse_dict_from_text(strings, strip_lines=cleanup, show=show_status, in_dict=dict, unique_id=unique_id)
+5 -5
View File
@@ -17,7 +17,7 @@ from .funcs_dict_preview import preview_str_dict as _preview_str_dict
# A tiny optimization by reusing the same immutable dict:
_input_types = _deepfreeze({
'required': {
'str_dict': _DataTypes.input_str_dict(tooltip="A dictionary of named substrings."),
'dict': _DataTypes.input_dict(tooltip="A Format-Dictionary."),
},
# 'optional': {},
'hidden': {
@@ -37,8 +37,8 @@ class StringConstructorDictPreview:
OUTPUT_NODE = True
FUNCTION = 'main'
RETURN_TYPES = (_DataTypes.STR_DICT, )
RETURN_NAMES = (_DataTypes.STR_DICT.lower(), )
RETURN_TYPES = (_DataTypes.DICT, )
RETURN_NAMES = (_DataTypes.DICT.lower(), )
# OUTPUT_TOOLTIPS = tuple()
@classmethod
@@ -46,7 +46,7 @@ class StringConstructorDictPreview:
return _input_types
def main(
self, str_dict: _t.Dict[str, _t.Any],
self, dict: _t.Dict[str, _t.Any],
unique_id: str = None,
):
return _preview_str_dict(str_dict, unique_id=unique_id)
return _preview_str_dict(dict, unique_id=unique_id)
+3 -3
View File
@@ -30,7 +30,7 @@ _input_types = _deepfreeze({
)}),
},
'optional': {
'str_dict': _DataTypes.input_str_dict(tooltip=(
'dict': _DataTypes.input_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."
)),
@@ -62,7 +62,7 @@ class StringConstructorFormatter:
def main(
self, template: str, show_status: bool,
str_dict: _t.Dict[str, _t.Any] = None,
dict: _t.Dict[str, _t.Any] = None,
unique_id: str = None,
):
return _formatter(template, str_dict, show=show_status, unique_id=unique_id)
return _formatter(template, dict, show=show_status, unique_id=unique_id)
+1 -1
View File
@@ -1,7 +1,7 @@
[project]
name = "string-constructor"
description = "Build your dictionary of named strings (text chunks), then easily compile different prompt variations from them - with simple string formatting.\nUnlike string-formatting nodes from other popular packs, this one is designed for specifying your sub-strings once, then dragging the entire \"library\" of them across the entire workflow (like a bus), updating it in-place as necessary. This is especially handy for regional prompting aka area composition."
version = "0.0.1"
version = "1.0.0"
authors = [
{name = "Lex Darlog"}
]