diff --git a/comfy_extras/nodes_string.py b/comfy_extras/nodes_string.py index b1a8ceef0..278d922de 100644 --- a/comfy_extras/nodes_string.py +++ b/comfy_extras/nodes_string.py @@ -1,5 +1,6 @@ import re - +import os +import folder_paths from comfy.comfy_types.node_typing import IO class StringConcatenate(): @@ -331,6 +332,40 @@ class RegexReplace(): result = re.sub(regex_pattern, replace, string, count=count, flags=flags) 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 = { "StringConcatenate": StringConcatenate, "StringSubstring": StringSubstring, @@ -343,6 +378,7 @@ NODE_CLASS_MAPPINGS = { "RegexMatch": RegexMatch, "RegexExtract": RegexExtract, "RegexReplace": RegexReplace, + "LoadText": LoadText } NODE_DISPLAY_NAME_MAPPINGS = { @@ -357,4 +393,5 @@ NODE_DISPLAY_NAME_MAPPINGS = { "RegexMatch": "Regex Match", "RegexExtract": "Regex Extract", "RegexReplace": "Regex Replace", + "LoadText": "Load Text" } diff --git a/folder_paths.py b/folder_paths.py index 9ec952940..1665de586 100644 --- a/folder_paths.py +++ b/folder_paths.py @@ -141,7 +141,7 @@ def get_directory_by_type(type_name: str) -> str | None: return get_input_directory() 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: files = os.listdir(folder_paths.get_input_directory())