-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstring_conditional_append_node.py
More file actions
70 lines (62 loc) · 2.52 KB
/
string_conditional_append_node.py
File metadata and controls
70 lines (62 loc) · 2.52 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
import re
class StringConditionalAppendNode:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"input_string": ("STRING", {
"forceInput": True,
"tooltip": "The string to process"
}),
"strings_to_check": ("STRING", {
"multiline": True,
"default": "",
"placeholder": "Enter strings to check (one per line)",
"tooltip": "Enter strings to search for and append if not found (one per line)"
}),
"position": (["end", "beginning"], {
"default": "end",
"tooltip": "Where to add strings if not found"
}),
"match_case": ("BOOLEAN", {
"default": False,
"label_on": "enabled",
"label_off": "disabled",
"tooltip": "Whether the search should be case-sensitive"
}),
"separator": ("STRING", {
"default": ", ",
"tooltip": "Character(s) to use between text and appended strings"
})
}
}
RETURN_TYPES = ("STRING", "BOOLEAN")
RETURN_NAMES = ("output_string", "was_appended")
FUNCTION = "conditional_append"
CATEGORY = "utils/StringEssentials"
def conditional_append(self, input_string, strings_to_check, position, match_case, separator):
# Parse strings to check, one per line
strings_list = []
for line in strings_to_check.splitlines():
line = line.strip()
if line:
strings_list.append(line)
if not strings_list:
return (input_string, False)
result = input_string
was_any_appended = False
# Check and append each string if not found
for check_string in strings_list:
# Check if check_string exists in current result
if match_case:
string_found = check_string in result
else:
string_found = check_string.lower() in result.lower()
# If string not found, append it
if not string_found:
if position == "beginning":
result = check_string + separator + result
else: # end
result = result + separator + check_string
was_any_appended = True
return (result, was_any_appended)