Upload salia_if_else_string_replacer.py
Browse files
salia_if_else_string_replacer.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class IfElseStringReplacer:
|
| 2 |
+
"""
|
| 3 |
+
if_else_string_replacer
|
| 4 |
+
|
| 5 |
+
- If substitute is exactly "" -> returns fallback
|
| 6 |
+
- Otherwise replaces all occurrences of search_for_token inside replaceable
|
| 7 |
+
with substitute (Python str.replace, global replacement).
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
@classmethod
|
| 11 |
+
def INPUT_TYPES(cls):
|
| 12 |
+
return {
|
| 13 |
+
"required": {
|
| 14 |
+
# If you want this to be socket-only (no textbox), keep forceInput=True.
|
| 15 |
+
# If you want to also be able to type it manually, remove forceInput.
|
| 16 |
+
"substitute": ("STRING", {"default": "", "forceInput": True}),
|
| 17 |
+
|
| 18 |
+
# Token to search for inside "replaceable"
|
| 19 |
+
"search_for_token": ("STRING", {"default": "COLOR", "multiline": False}),
|
| 20 |
+
|
| 21 |
+
# Used only when substitute == ""
|
| 22 |
+
"fallback": ("STRING", {"default": "", "multiline": True}),
|
| 23 |
+
|
| 24 |
+
# The template string that contains the token 0..N times
|
| 25 |
+
"replaceable": ("STRING", {"default": "", "multiline": True}),
|
| 26 |
+
}
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
RETURN_TYPES = ("STRING",)
|
| 30 |
+
RETURN_NAMES = ("string",)
|
| 31 |
+
FUNCTION = "run"
|
| 32 |
+
CATEGORY = "utils/string"
|
| 33 |
+
|
| 34 |
+
def run(self, substitute, search_for_token, fallback, replaceable):
|
| 35 |
+
# Normalize to safe strings (ComfyUI usually already gives strings, but this is defensive)
|
| 36 |
+
substitute = "" if substitute is None else str(substitute)
|
| 37 |
+
search_for_token = "" if search_for_token is None else str(search_for_token)
|
| 38 |
+
fallback = "" if fallback is None else str(fallback)
|
| 39 |
+
replaceable = "" if replaceable is None else str(replaceable)
|
| 40 |
+
|
| 41 |
+
# 1) If substitute is exactly empty -> fallback
|
| 42 |
+
if substitute == "":
|
| 43 |
+
return (fallback,)
|
| 44 |
+
|
| 45 |
+
# 2) If token is empty, do nothing (avoids Python inserting between every char)
|
| 46 |
+
if search_for_token == "":
|
| 47 |
+
return (replaceable,)
|
| 48 |
+
|
| 49 |
+
# 3) Replace all occurrences
|
| 50 |
+
out = replaceable.replace(search_for_token, substitute)
|
| 51 |
+
return (out,)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
NODE_CLASS_MAPPINGS = {
|
| 55 |
+
"if_else_string_replacer": IfElseStringReplacer,
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
NODE_DISPLAY_NAME_MAPPINGS = {
|
| 59 |
+
"if_else_string_replacer": "If/Else String Replacer",
|
| 60 |
+
}
|