Skip to content

Commit e2a9e3b

Browse files
authored
v1.1.0: Safe mode (#3)
2 parents 95fa516 + b3b1a20 commit e2a9e3b

File tree

5 files changed

+229
-66
lines changed

5 files changed

+229
-66
lines changed

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
## `TODO`
22

33
The pack is considered feature-complete.
4-
No other features planned. To do some fancy stuff with dictionaries, specialized node packs are recommended instead.
4+
No other features planned (but I have nothing against improvements: feel free to [Pull Request](../../pulls)).
5+
6+
To do some fancy stuff with dictionaries, specialized node packs are recommended instead.
7+
8+
# v1.1.0
9+
10+
- `✨New feature` Safe-formatting mode:
11+
- When an invalid pattern included in the formatted template - instead of throwing an error, leaves this part of the template as-is. Useful for templates with curly braces in them intended to stay: JSON/CSS.
512

613
# v1.0.4
714

README.md

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,16 +159,33 @@ neg_common
159159

160160
### When you need curly braces themselves
161161

162-
To have the literal curly-brace characters inside the **formatted** prompt, you need to "escape" them: whenever you need one, you type it twice (`'{{'` or `'}}'`). Then, after formatting, it will turn to `'{'` or `'}'`, respectively.
162+
#### Safe mode `✨New in v1.1.0`
163163

164-
Keep in mind though, that with [Recursive formatting](#recursive-formatting-), any `{{text}}` will become `{text}` after very first iteration, and thus on the next one, it still will be treated as a placeholder to put a string with a `text` key into.
164+
The simplest solution - just enable `safe_format` toggle. It changes the node behavior: whenever formatter encounters any `{text inside curly braces}` that cannot be formatted _(for any reason: the dict misses this key, or it's an outright invalid pattern)_, it doesn't throw an error and instead simply leaves this part of template as-is.
165+
166+
It's extremely useful for code-like templates (e.g., containing JSON or CSS).
167+
168+
However, even for proper `{patterns}` referencing proper dict keys, the manual escaping (see below) also applies.
169+
170+
> [!NOTE]
171+
> Keep in mind that any spaces inside braces immediately make this pattern invalid.
172+
>
173+
> Thus, if you want to have something like: `{{my_key}}` _(so, `{my_key}` to be replaced + it's wrapped into another set of braces which you want to have in the output)_, the easiest way to do it without escaping is simply enabling safe mode and adding a couple of spaces inside the braces you want to stay. So: `{ {my_key} }` - the innermost part will be replaced with the value of `my_key` item from the dict, while the outer braces will stay.
174+
175+
#### Manual escaping - double braces
176+
177+
If you want to explicitly control which braces perform formatting and which don't - to have the literal curly-brace characters inside the prompt **after** formatting, you need to [escape](https://docs.python.org/3/library/string.html#format-string-syntax) them: whenever you need one, you type it twice (`'{{'` or `'}}'`). Then, after formatting, it will turn to `'{'` or `'}'`, respectively.
178+
179+
Keep in mind though, that with escaping approach and [Recursive formatting](#recursive-formatting-), any `{{text}}` will become `{text}` after very first iteration, and thus on the next one, it still will be treated as a placeholder to put a value of the `text` key into.
165180

166181
However, this might be exactly what you want for...
167182

168183
### Dynamic pattern aka conditional formatting
169184

170185
In other words, you build a prompt, where **keys themselves** are compiled from pieces. For example:
171-
- Your main text template has {{character_`{active_char}`}} pattern somewhere inside it.
186+
- Your main text template has one of the following patterns somewhere inside it:
187+
- {{character_`{active_char}`}} - if safe mode is off;
188+
- {character_`{active_char}`} - if safe mode is on.
172189
- You also have a string named `active_char` in your dict, which you simply set to a number.
173190
- Also-also, you have strings named `character_1`, `character_2`, etc.
174191
- Then, with recursive formatting, depending on just a **single** value you set the `active_char` element to, the following happens:

docstring_formatter.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@
88
import re as _re
99

1010

11-
_re_indent_match = _re.compile("(\t*)( +)(\t*)(.*?)$").match
12-
_re_tab_indent_match = _re.compile("(\t+)(.*?)$").match
11+
_re_indent_match = _re.compile(r"(\t*)( +)(\t*)(.*?)$").match
12+
_re_tab_indent_match = _re.compile(r"(\t+)(.*?)$").match
1313
_re_list_line_match = _re.compile(
14-
"(\s*)("
15-
"[-*•]+"
16-
"|"
17-
"[a-zA-Z]\s*[.)]"
18-
"|"
19-
"[0-9+]\s*[.)]"
20-
")\s+"
14+
r"(\s*)("
15+
r"[-*•]+"
16+
r"|"
17+
r"[a-zA-Z]\s*[.)]"
18+
r"|"
19+
r"[0-9+]\s*[.)]"
20+
r")\s+"
2121
).match
2222

2323

node_formatter.py

Lines changed: 180 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
# encoding: utf-8
22
"""
3+
Code for ``StringConstructorFormatter`` node.
34
"""
45

56
import typing as _t
67

8+
from dataclasses import dataclass as _dataclass, field as _field
79
from inspect import cleandoc as _cleandoc
10+
import re as _re
811
import sys as _sys
912

1013
from frozendict import deepfreeze as _deepfreeze
@@ -15,42 +18,171 @@
1518
from .docstring_formatter import format_docstring as _format_docstring
1619
from .enums import DataTypes as _DataTypes
1720
from .funcs_common import _show_text_on_node, _verify_input_dict
18-
# from .funcs_formatter import formatter as _formatter
1921

2022

21-
_RECURSION_LIMIT = max(int(_sys.getrecursionlimit()), 1) # You can externally monkey-patch it... but if it blows up, your fault 🤷🏻‍♂
23+
_RECURSION_LIMIT = max(int(_sys.getrecursionlimit()), 1) # You can externally monkey-patch it... but if it blows up, your fault 🤷🏻‍♂️single
2224

25+
__dataclass_slots_args = dict() if _sys.version_info < (3, 10) else dict(slots=True)
2326

24-
def _recursive_format(template: str, format_dict: _t.Dict[str, _t.Any], show: bool = True, unique_id: str = None) -> str:
27+
_re_formatting_keyword_match = _re.compile( # Pre-compiled regex match to extract ``{keyword}`` patterns
28+
r'(?P<prefix>.*?)'
29+
r'(?P<open_brackets>\{+)'
30+
r'(?P<inside_brackets>[^{}]+)'
31+
r'(?P<closed_brackets>\}+)'
32+
r'(?P<suffix>[^{}].*)?$',
33+
# flags=_re.DOTALL | _re.IGNORECASE,
34+
flags = _re.DOTALL, # We need dot to match new lines, too
35+
).match
36+
37+
38+
@_dataclass(**__dataclass_slots_args)
39+
class _Formatter:
2540
"""
26-
It's not actually recursive - because, you know, any recursion could be turned into iteration,
27-
and good boys do that. 😊
41+
A callable (function-like) class, which does the actual formatting, while respecting all the options.
42+
43+
It's made as a class to split the formatting into two stages:
44+
- First, the instance is properly initialized with the shared arguments (the methods to call are conditionally assigned depending on options);
45+
- Then, the actual instance is treated as a function - it needs to be called with the formatted string as the only argument.
46+
47+
It's done this way to avoid extra conditions in the loop + to organize the convoluted mess of intertwined functions
48+
into a more readable code.
2849
"""
29-
assert isinstance(_RECURSION_LIMIT, int) and _RECURSION_LIMIT > 0
30-
31-
prev: str = ''
32-
new: str = template
33-
for i in range(_RECURSION_LIMIT):
34-
if prev == new:
35-
break
36-
prev = new
37-
new = new.format_map(format_dict)
38-
if prev == new:
39-
return new
40-
41-
msg = (
42-
f"Recursion limit ({_RECURSION_LIMIT}) reached on attempt to format a string: {template!r}\n"
43-
f"Last two formatting attempts:\n{prev!r}\n{new!r}"
44-
)
45-
if show and unique_id:
46-
_show_text_on_node(msg, unique_id)
47-
raise RecursionError(msg)
48-
# noinspection PyUnreachableCode
49-
return '' # just to be safe
50+
format_dict: _t.Optional[_t.Dict[str, _t.Any]] = None
5051

51-
# --------------------------------------
52+
recursive: bool = False
53+
safe: bool = True
54+
55+
show_status: bool = True
56+
unique_node_id: str = None
57+
58+
__format_single: _t.Callable[[str], str] = _field(init=False, repr=False, compare=False, default=lambda x: x)
59+
_format: _t.Callable[[str], str] = _field(init=False, repr=False, compare=False, default=lambda x: x)
60+
61+
def __post_init__(self): # called by dataclass init
62+
if self.format_dict is None:
63+
self.format_dict = dict()
64+
65+
format_dict = self.format_dict
66+
_verify_input_dict(format_dict)
67+
self.__format_single = self.__format_single_safe if self.safe else self.__format_single_unsafe
68+
69+
if format_dict:
70+
self._format = self.__format_recursive if self.recursive else self.__format_single
71+
else:
72+
self._format = self.__dummy_return_intact
73+
74+
@staticmethod
75+
def __dummy_return_intact(template: str) -> str:
76+
return template
77+
78+
def __format_single_unsafe(self, template: str):
79+
return template.format_map(self.format_dict)
80+
81+
def __format_single_safe_parts_gen(self, template: str) -> _t.Generator[str, None, None]:
82+
"""
83+
EAFP: https://docs.python.org/3/glossary.html#term-EAFP
84+
85+
Instead of pre-escaping the whole template
86+
(which would require basically re-implementing the entire format-parsing logic),
87+
let's extract individual formatted pieces, actually try formatting them one by one,
88+
and return anything that cannot be formatted as-is - without any processing at all.
89+
90+
This generator returns such pieces - formatted or intact.
91+
"""
92+
format_dict = self.format_dict
93+
to_piece_template = '{{{}}}'.format
94+
95+
suffix: str = template
96+
while suffix:
97+
match = _re_formatting_keyword_match(suffix)
98+
if not match:
99+
break
100+
101+
prefix = match.group('prefix')
102+
open_brackets = match.group('open_brackets')
103+
inside_brackets = match.group('inside_brackets')
104+
closed_brackets = match.group('closed_brackets')
105+
suffix = match.group('suffix')
106+
107+
if prefix:
108+
yield prefix
109+
110+
piece_template = to_piece_template(inside_brackets)
111+
# noinspection PyBroadException
112+
try:
113+
formatted_piece = piece_template.format_map(format_dict)
114+
except Exception:
115+
# If, for ANY reason, we're unable to format the piece, return the template piece intact:
116+
yield open_brackets
117+
yield inside_brackets
118+
yield closed_brackets
119+
continue
52120

53-
_dict = dict
121+
# The key is found. Treat the piece as the actual formatting pattern.
122+
# Formatting "eats" one set of brackets either way:
123+
open_brackets = open_brackets[:-1]
124+
closed_brackets = closed_brackets[:-1]
125+
126+
# Now, even though we've succeeded, the template might've been pre-escaped.
127+
if open_brackets and closed_brackets:
128+
# The keyword is already pre-escaped. Return it intact:
129+
formatted_piece = inside_brackets
130+
131+
yield open_brackets
132+
yield formatted_piece
133+
yield closed_brackets
134+
135+
if suffix:
136+
yield suffix
137+
138+
def __format_single_safe(self, template: str) -> str:
139+
"""
140+
Format the pattern (single iteration of formatting) in the safe mode.
141+
Safe mode preserves any unknown ``{text patterns}`` inside curly brackets if they cannot be formatted.
142+
Correctly handles any formatting patterns natively supported by python
143+
(even the most fancy ones, involving ':', '!', attribute or index access, etc.).
144+
145+
Useful when JSON/CSS-like code is in the formatted template.
146+
"""
147+
return ''.join(self.__format_single_safe_parts_gen(template))
148+
149+
def __format_recursive(self, template: str) -> str:
150+
"""
151+
It's not actually recursive - because, you know, any recursion could be turned into iteration,
152+
and good boys do that. 😊
153+
"""
154+
assert isinstance(_RECURSION_LIMIT, int) and _RECURSION_LIMIT > 0
155+
156+
format_single_func = self.__format_single
157+
158+
prev: str = ''
159+
new: str = template
160+
for i in range(_RECURSION_LIMIT):
161+
if prev == new:
162+
return new
163+
prev = new
164+
new = format_single_func(new)
165+
166+
msg = (
167+
f"Recursion limit ({_RECURSION_LIMIT}) reached on attempt to format a string: {template!r}\n"
168+
f"Last two formatting attempts:\n{prev!r}\n{new!r}"
169+
)
170+
if self.show_status and self.unique_node_id:
171+
_show_text_on_node(msg, self.unique_node_id)
172+
raise RecursionError(msg)
173+
# noinspection PyUnreachableCode
174+
return '' # just to be extra-safe, if RecursionError is treated as warning
175+
176+
def __call__(self, template: str) -> str:
177+
if not isinstance(template, str):
178+
raise TypeError(f"Not a string: {template!r}")
179+
180+
out_text = self._format(template) if template else ''
181+
if self.show_status and self.unique_node_id:
182+
_show_text_on_node(out_text, self.unique_node_id)
183+
return out_text
184+
185+
# --------------------------------------
54186

55187
_input_types = _deepfreeze({
56188
'required': {
@@ -61,20 +193,22 @@ def _recursive_format(template: str, format_dict: _t.Dict[str, _t.Any], show: bo
61193
"{char1_long}\n{char2_long}"
62194
)}),
63195
'recursive_format': (_IO.BOOLEAN, {'default': False, 'label_on': '❗ yes', 'label_off': 'no', 'tooltip': (
64-
"Do recursive format - i.e., allow the chunks from the dictionary to reference other chunks - which in turn "
65-
"lets you do really crazy stuff like building entire HIERARCHIES of sub-prompts, all linked to the same "
66-
"wording typed in one place.\n\n"
67-
"The exclamation mark reminds you that with great power comes great responsibility!\n"
68-
"You can end up with chunks cross-referencing each other in a loop. In such case, the node will just error out "
69-
"and won't let you crash the entire ComfyUI - but still, it's your responsibility to prevent "
70-
"such looping dictionaries."
196+
"Do recursive format - i.e., allow the chunks from the dictionary to reference other chunks."
197+
)}),
198+
'safe_format': (_IO.BOOLEAN, {'default': True, 'label_on': 'yes', 'label_off': 'no', 'tooltip': (
199+
"Safe mode: If a specific {text pattern} can't be formatted "
200+
"(it doesn't exist in the dict or isn't a valid formatting pattern at all), "
201+
"leave it as-is.\n"
202+
"On: invalid {patterns} preserved intact.\n"
203+
"Off: invalid {patterns} raise an error.\n\n"
204+
"Safe mode is recommended for templates with JSON, CSS, or other literal curly brackets."
71205
)}),
72206
'show_status': (_IO.BOOLEAN, {'default': True, 'label_on': 'formatted string', 'label_off': 'no', 'tooltip': (
73207
"Show the final string constructed from the text-template and format-dictionary?"
74208
)}),
75209
},
76210
'optional': {
77-
'dict': _DataTypes.input_dict(tooltip=(
211+
'dict': _DataTypes.input_dict(tooltip=( # It's not actually optional, but is here since there's no dict-widget
78212
"The dictionary to take named sub-strings from. It could be left unconnected, if the pattern doesn't reference "
79213
"any sub-strings - then, this node acts exactly the same as a regular string-primitive node."
80214
)),
@@ -106,20 +240,17 @@ def INPUT_TYPES(cls):
106240

107241
@staticmethod
108242
def main(
109-
template: str, recursive_format: bool = False, show_status: bool = False, dict: _t.Dict[str, _t.Any] = None,
243+
template: str,
244+
recursive_format: bool = False,
245+
safe_format: bool = True,
246+
show_status: bool = False,
247+
dict: _t.Dict[str, _t.Any] = None, #actually, required - but it's here to keep the declared params order
110248
unique_id: str = None
111249
) -> _t.Tuple[str]:
112-
if dict is None:
113-
dict = _dict()
114-
_verify_input_dict(dict)
115-
if not isinstance(template, str):
116-
raise TypeError(f"Not a string: {template!r}")
117-
118-
out_text = (
119-
_recursive_format(template, dict, show_status, unique_id)
120-
if recursive_format
121-
else template.format_map(dict)
250+
formatter = _Formatter(
251+
format_dict=dict,
252+
recursive=recursive_format, safe=safe_format,
253+
show_status=show_status, unique_node_id=unique_id,
122254
)
123-
if show_status and unique_id:
124-
_show_text_on_node(out_text, unique_id)
255+
out_text = formatter(template)
125256
return (out_text, )

pyproject.toml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
[project]
22
name = "string-constructor"
3+
version = "1.1.0"
34
description = "String formatting (compiling text from pieces) made for humans:\n• Turn parts of prompt into a dictionary - to share them all across the entire workflow as a single connection.\n• Compose your prompt in-place from these snippets and regular text - with just one text field.\n• Easily update the dictionary down the line - get different prompts from the same template.\n• Reference text chunks within each other to build complex hierarchies of self-updating prompts.\n• A true time-saver for regional prompting (aka area composition)."
4-
version = "1.0.4"
5+
license = {file = "LICENSE.md"}
6+
readme = "README.md"
57
authors = [
68
{name = "Lex Darlog"}
79
]
8-
license = {file = "LICENSE.md"}
9-
readme = "README.md"
10+
requires-python = ">=3.7"
1011
dependencies = ["frozendict"]
12+
classifiers = [
13+
# https://docs.comfy.org/registry/specifications#classifiers-recommended
14+
"Operating System :: OS Independent",
15+
]
1116

1217
[project.urls]
1318
Repository = "https://github.com/Lex-DRL/ComfyUI-StringConstructor"
19+
"Feedback/Bugs/Feature Requests" = "https://github.com/Lex-DRL/ComfyUI-StringConstructor/issues"
1420
# Used by Comfy Registry https://comfyregistry.org
1521

1622
[tool.comfy]
1723
PublisherId = "lex-drl"
1824
DisplayName = "String Constructor (Text-Formatting)"
19-
Icon = ""
25+
# Icon = "https://raw.githubusercontent.com/username/repo/main/icon.png"
26+
# Banner = "https://raw.githubusercontent.com/username/repo/main/banner.png"
27+
# requires-comfyui = ">=0.3.51" # Node schema v3 released # TODO

0 commit comments

Comments
 (0)