Merge 5162e9094816f6df23f27643c7a04a8494d38e8f into 4977f203fa8e9e3ab22884c8ace8f9b540d48952

This commit is contained in:
yoinked 2025-08-19 18:06:25 +08:00 committed by GitHub
commit ad78f8e7fe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 39 additions and 2 deletions

View File

@ -1,5 +1,6 @@
import re import re
import os
import folder_paths
from comfy.comfy_types.node_typing import IO from comfy.comfy_types.node_typing import IO
class StringConcatenate(): class StringConcatenate():
@ -331,6 +332,40 @@ class RegexReplace():
result = re.sub(regex_pattern, replace, string, count=count, flags=flags) result = re.sub(regex_pattern, replace, string, count=count, flags=flags)
return result, return result,
class LoadText():
DESCRIPTION = "Load a string from a file."
@classmethod
def INPUT_TYPES(s):
input_dir = folder_paths.get_input_directory()
files = [f for f in os.listdir(input_dir) if os.path.isfile(os.path.join(input_dir, f))]
files = folder_paths.filter_files_content_types(files, ["text"])
return {
"required": {"file": (sorted(files), {"text_upload": True})},
}
RETURN_TYPES = (IO.STRING,)
FUNCTION = "execute"
CATEGORY = "utils/string"
def execute(self, file):
text_path = folder_paths.get_annotated_filepath(file)
string = open(text_path, 'r', encoding='utf-8').read() #enforcing utf-8 is odd, good enough
return (string,)
@classmethod
def IS_CHANGED(cls, file):
text_path = folder_paths.get_annotated_filepath(file)
mod_time = os.path.getmtime(text_path)
# use mod time (inspired by video nodes)
return mod_time
@classmethod
def VALIDATE_INPUTS(cls, file):
if not folder_paths.exists_annotated_filepath(file):
return "Invalid text file: {}".format(file)
return True
NODE_CLASS_MAPPINGS = { NODE_CLASS_MAPPINGS = {
"StringConcatenate": StringConcatenate, "StringConcatenate": StringConcatenate,
"StringSubstring": StringSubstring, "StringSubstring": StringSubstring,
@ -343,6 +378,7 @@ NODE_CLASS_MAPPINGS = {
"RegexMatch": RegexMatch, "RegexMatch": RegexMatch,
"RegexExtract": RegexExtract, "RegexExtract": RegexExtract,
"RegexReplace": RegexReplace, "RegexReplace": RegexReplace,
"LoadText": LoadText
} }
NODE_DISPLAY_NAME_MAPPINGS = { NODE_DISPLAY_NAME_MAPPINGS = {
@ -357,4 +393,5 @@ NODE_DISPLAY_NAME_MAPPINGS = {
"RegexMatch": "Regex Match", "RegexMatch": "Regex Match",
"RegexExtract": "Regex Extract", "RegexExtract": "Regex Extract",
"RegexReplace": "Regex Replace", "RegexReplace": "Regex Replace",
"LoadText": "Load Text"
} }

View File

@ -141,7 +141,7 @@ def get_directory_by_type(type_name: str) -> str | None:
return get_input_directory() return get_input_directory()
return None return None
def filter_files_content_types(files: list[str], content_types: List[Literal["image", "video", "audio", "model"]]) -> list[str]: def filter_files_content_types(files: list[str], content_types: List[Literal["image", "video", "audio", "model", "text"]]) -> list[str]:
""" """
Example: Example:
files = os.listdir(folder_paths.get_input_directory()) files = os.listdir(folder_paths.get_input_directory())