File size: 14,919 Bytes
4450790 |
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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 |
#---------------------------------------------------------------------------------------------------------------------#
# Comfyroll Studio custom nodes by RockOfFire and Akatsuzi https://github.com/Suzie1/ComfyUI_Comfyroll_CustomNodes
# for ComfyUI https://github.com/comfyanonymous/ComfyUI
#---------------------------------------------------------------------------------------------------------------------#
import os
import csv
import io
from ..categories import icons
class AnyType(str):
"""A special type that can be connected to any other types. Credit to pythongosssss"""
def __ne__(self, __value: object) -> bool:
return False
any_type = AnyType("*")
#---------------------------------------------------------------------------------------------------------------------#
# Text Util Nodes
#---------------------------------------------------------------------------------------------------------------------#
class CR_SplitString:
@classmethod
def INPUT_TYPES(s):
return {"required": {
"text": ("STRING", {"multiline": False, "default": "text"}),
},
"optional": {
"delimiter": ("STRING", {"multiline": False, "default": ","}),
}
}
RETURN_TYPES = (any_type, any_type, any_type, any_type, "STRING", )
RETURN_NAMES = ("string_1", "string_2", "string_3", "string_4", "show_help", )
FUNCTION = "split"
CATEGORY = icons.get("Comfyroll/Utils/Text")
def split(self, text, delimiter=""):
# Split the text string
parts = text.split(delimiter)
strings = [part.strip() for part in parts[:4]]
string_1, string_2, string_3, string_4 = strings + [""] * (4 - len(strings))
show_help = "https://github.com/Suzie1/ComfyUI_Comfyroll_CustomNodes/wiki/Other-Nodes#cr-split-string"
return (string_1, string_2, string_3, string_4, show_help, )
#---------------------------------------------------------------------------------------------------------------------#
class CR_Text:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"text": ("STRING", {"default": '', "multiline": True}),
}
}
RETURN_TYPES = (any_type, "STRING", )
RETURN_NAMES = ("text", "show_help", )
FUNCTION = "text_multiline"
CATEGORY = icons.get("Comfyroll/Utils/Text")
def text_multiline(self, text):
show_help = "https://github.com/Suzie1/ComfyUI_Comfyroll_CustomNodes/wiki/Other-Nodes#cr-text"
return (text, show_help,)
#---------------------------------------------------------------------------------------------------------------------#
class CR_MultilineText:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"text": ("STRING", {"default": '', "multiline": True}),
"convert_from_csv": ("BOOLEAN", {"default": False}),
"csv_quote_char": ("STRING", {"default": "'", "choices": ["'", '"']}),
"remove_chars": ("BOOLEAN", {"default": False}),
"chars_to_remove": ("STRING", {"multiline": False, "default": ""}),
"split_string": ("BOOLEAN", {"default": False}),
}
}
RETURN_TYPES = (any_type, "STRING", )
RETURN_NAMES = ("multiline_text", "show_help", )
FUNCTION = "text_multiline"
CATEGORY = icons.get("Comfyroll/Utils/Text")
def text_multiline(self, text, chars_to_remove, split_string=False, remove_chars=False, convert_from_csv=False, csv_quote_char="'"):
new_text = []
# Remove trailing commas
text = text.rstrip(',')
if convert_from_csv:
# Convert CSV to multiline text
csv_reader = csv.reader(io.StringIO(text), quotechar=csv_quote_char)
for row in csv_reader:
new_text.extend(row)
if split_string:
if text.startswith("'") and text.endswith("'"):
text = text[1:-1] # Remove outer single quotes
values = [value.strip() for value in text.split("', '")]
new_text.extend(values)
elif text.startswith('"') and text.endswith('"'):
text = text[1:-1] # Remove outer single quotes
values = [value.strip() for value in text.split('", "')]
new_text.extend(values)
elif ',' in text and text.count("'") % 2 == 0:
# Assume it's a list-like string and split accordingly
text = text.replace("'", '') # Remove single quotes
values = [value.strip() for value in text.split(",")]
new_text.extend(values)
elif ',' in text and text.count('"') % 2 == 0:
# Assume it's a list-like string and split accordingly
text = text.replace('"', '') # Remove single quotes
values = [value.strip() for value in text.split(",")]
new_text.extend(values)
if convert_from_csv == False and split_string == False:
# Process multiline text
for line in io.StringIO(text):
if not line.strip().startswith('#'):
if not line.strip().startswith("\n"):
line = line.replace("\n", '')
if remove_chars:
# Remove quotes from each line
line = line.replace(chars_to_remove, '')
new_text.append(line)
new_text = "\n".join(new_text)
show_help = "https://github.com/Suzie1/ComfyUI_Comfyroll_CustomNodes/wiki/Other-Nodes#cr-multiline-text"
return (new_text, show_help,)
#---------------------------------------------------------------------------------------------------------------------#
class CR_SaveTextToFile:
@classmethod
def INPUT_TYPES(s):
return {"required": {
"multiline_text": ("STRING", {"multiline": True, "default": ""}),
"output_file_path": ("STRING", {"multiline": False, "default": ""}),
"file_name": ("STRING", {"multiline": False, "default": ""}),
"file_extension": (["txt", "csv"],),
}
}
RETURN_TYPES = ("STRING", )
RETURN_NAMES = ("show_help", )
OUTPUT_NODE= True
FUNCTION = 'save_list'
CATEGORY = icons.get("Comfyroll/Utils/Text")
def save_list(self, multiline_text, output_file_path, file_name, file_extension):
show_help = "https://github.com/Suzie1/ComfyUI_Comfyroll_CustomNodes/wiki/List-Nodes#cr-save-text-to-file"
filepath = output_file_path + "\\" + file_name + "." + file_extension
index = 1
if(output_file_path == "" or file_name == ""):
print(f"[Warning] CR Save Text List. No file details found. No file output.")
return ()
while os.path.exists(filepath):
if os.path.exists(filepath):
filepath = output_file_path + "\\" + file_name + "_" + str(index) + "." + file_extension
index = index + 1
else:
break
print(f"[Info] CR Save Text List: Saving to {filepath}")
if file_extension == "csv":
text_list = []
for i in multiline_text.split("\n"):
text_list.append(i.strip())
with open(filepath, "w", newline="") as csv_file:
csv_writer = csv.writer(csv_file)
# Write each line as a separate row in the CSV file
for line in text_list:
csv_writer.writerow([line])
else:
with open(filepath, "w", newline="") as text_file:
for line in multiline_text:
text_file.write(line)
return (show_help, )
#---------------------------------------------------------------------------------------------------------------------#
class CR_TextConcatenate:
@ classmethod
def INPUT_TYPES(cls):
return {"required": {
},
"optional": {
"text1": ("STRING", {"multiline": False, "default": "", "forceInput": True}),
"text2": ("STRING", {"multiline": False, "default": "", "forceInput": True}),
"separator": ("STRING", {"multiline": False, "default": ""}),
},
}
RETURN_TYPES = (any_type, "STRING", )
RETURN_NAMES = ("STRING", "show_help", )
FUNCTION = "concat_text"
CATEGORY = icons.get("Comfyroll/Utils/Text")
def concat_text(self, text1="", text2="", separator=""):
show_help = "https://github.com/Suzie1/ComfyUI_Comfyroll_CustomNodes/wiki/List-Nodes#cr-save-text-to-file"
return (text1 + separator + text2, )
#---------------------------------------------------------------------------------------------------------------------#
class CR_TextReplace:
@ classmethod
def INPUT_TYPES(cls):
return {
"required": {
"text": ("STRING", {"multiline": True, "default": "", "forceInput": True}),
},
"optional": {
"find1": ("STRING", {"multiline": False, "default": ""}),
"replace1": ("STRING", {"multiline": False, "default": ""}),
"find2": ("STRING", {"multiline": False, "default": ""}),
"replace2": ("STRING", {"multiline": False, "default": ""}),
"find3": ("STRING", {"multiline": False, "default": ""}),
"replace3": ("STRING", {"multiline": False, "default": ""}),
},
}
RETURN_TYPES = (any_type, "STRING", )
RETURN_NAMES = ("STRING", "show_help", )
FUNCTION = "replace_text"
CATEGORY = icons.get("Comfyroll/Utils/Text")
def replace_text(self, text, find1="", replace1="", find2="", replace2="", find3="", replace3=""):
show_help = "https://github.com/Suzie1/ComfyUI_Comfyroll_CustomNodes/wiki/List-Nodes#cr-text-replace"
text = text.replace(find1, replace1)
text = text.replace(find2, replace2)
text = text.replace(find3, replace3)
return (text, show_help)
#---------------------------------------------------------------------------------------------------------------------#
class CR_TextBlacklist:
@ classmethod
def INPUT_TYPES(cls):
return {
"required": {
"text": ("STRING", {"multiline": True, "default": "", "forceInput": True}),
"blacklist_words": ("STRING", {"multiline": True, "default": ""}),
},
"optional": {
"replacement_text": ("STRING", {"multiline": False, "default": ""}),
},
}
RETURN_TYPES = (any_type, "STRING", )
RETURN_NAMES = ("STRING", "show_help", )
FUNCTION = "replace_text"
CATEGORY = icons.get("Comfyroll/Utils/Text")
def replace_text(self, text, blacklist_words, replacement_text=""):
show_help = "https://github.com/Suzie1/ComfyUI_Comfyroll_CustomNodes/wiki/List-Nodes#cr-text-blacklist"
text_out = text
for line in blacklist_words.split('\n'): # Splitting based on line return
if line.strip():
text_out = text_out.replace(line.strip(), replacement_text)
return (text_out, show_help)
#---------------------------------------------------------------------------------------------------------------------#
class CR_TextOperation:
@ classmethod
def INPUT_TYPES(cls):
operations = ["uppercase", "lowercase", "capitalize", "invert_case", "reverse", "trim", "remove_spaces"]
return {
"required": {
"text": ("STRING", {"multiline": False, "default": "", "forceInput": True}),
"operation": (operations,),
},
}
RETURN_TYPES = (any_type, "STRING", )
RETURN_NAMES = ("STRING", "show_help", )
FUNCTION = "text_operation"
CATEGORY = icons.get("Comfyroll/Utils/Text")
def text_operation(self, text, operation):
show_help = "https://github.com/Suzie1/ComfyUI_Comfyroll_CustomNodes/wiki/List-Nodes#cr-text_operation"
if operation == "uppercase":
text_out = text.upper()
elif operation == "lowercase":
text_out = text.lower()
elif operation == "capitalize":
text_out = text.capitalize()
elif operation == "invert_case":
text_out = text.swapcase()
elif operation == "reverse":
text_out = text[::-1]
elif operation == "trim":
text_out = text.strip()
elif operation == "remove_spaces":
text_out = text.replace(" ", "")
else:
return "CR Text Operation: Invalid operation."
return (text_out, show_help, )
#---------------------------------------------------------------------------------------------------------------------#
class CR_TextLength:
@ classmethod
def INPUT_TYPES(cls):
return {
"required": {
"text": ("STRING", {"multiline": False, "default": "", "forceInput": True}),
},
}
RETURN_TYPES = ("INT", "STRING", )
RETURN_NAMES = ("INT", "show_help", )
FUNCTION = "len_text"
CATEGORY = icons.get("Comfyroll/Utils/Text")
def len_text(self, text):
show_help = "https://github.com/Suzie1/ComfyUI_Comfyroll_CustomNodes/wiki/List-Nodes#cr-text-length"
int_out = len(text)
return (int_out, show_help, )
#---------------------------------------------------------------------------------------------------------------------#
# MAPPINGS
#---------------------------------------------------------------------------------------------------------------------#
# For reference only, actual mappings are in __init__.py
'''
NODE_CLASS_MAPPINGS = {
### Utils Text
"CR Text": CR_Text,
"CR Multiline Text": CR_MultilineText,
"CR Split String": CR_SplitString,
"CR Text Concatenate": CR_TextConcatenate,
"CR Text Replace": CR_TextReplace,
"CR Text Blacklist": CR_TextBlacklist,
"CR Text Length": CR_TextLength,
"CR Text Operation": CR_TextOperation,
"CR Save Text To File": CR_SaveTextToFile,
}
'''
|