PERF 合并读取文本文件, 读取图片同类型节点

This commit is contained in:
2025-07-15 17:31:39 +08:00
parent 42f3db768c
commit 0b542b5d3f
3 changed files with 37 additions and 133 deletions

View File

@@ -5,89 +5,16 @@ import time
import folder_paths
def get_allowed_dirs():
return {
"input": "$input/**/*.txt",
"output": "$output/**/*.txt",
"temp": "$temp/**/*.txt"
}
def get_valid_dirs():
return get_allowed_dirs().keys()
def get_dir_from_name(name):
dirs = get_allowed_dirs()
if name not in dirs:
raise KeyError(name + " dir not found")
path = dirs[name]
path = path.replace("$input", folder_paths.get_input_directory())
path = path.replace("$output", folder_paths.get_output_directory())
path = path.replace("$temp", folder_paths.get_temp_directory())
return path
def is_child_dir(parent_path, child_path):
parent_path = os.path.abspath(parent_path)
child_path = os.path.abspath(child_path)
return os.path.commonpath([parent_path]) == os.path.commonpath([parent_path, child_path])
def get_real_path(dir):
dir = dir.replace("/**/", "/")
dir = os.path.abspath(dir)
dir = os.path.split(dir)[0]
return dir
def get_file(root_dir, file):
if file == "[none]" or not file or not file.strip():
raise ValueError("No file")
root_dir = get_dir_from_name(root_dir)
root_dir = get_real_path(root_dir)
if not os.path.exists(root_dir):
os.mkdir(root_dir)
full_path = os.path.join(root_dir, file)
if not is_child_dir(root_dir, full_path):
raise ReferenceError()
return full_path
class LoadTextLocal:
class LoadText:
@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": {
"root_dir": (list(get_valid_dirs()), {}),
"file": (["[none]"], {
"pysssss.binding": [{
"source": "root_dir",
"callback": [{
"type": "set",
"target": "$this.disabled",
"value": True
}, {
"type": "fetch",
"url": "/pysssss/text-file/{$source.value}",
"then": [{
"type": "set",
"target": "$this.options.values",
"value": "$result"
}, {
"type": "validate-combo"
}, {
"type": "set",
"target": "$this.disabled",
"value": False
}]
}],
}]
}),
"file_path": ("STRING", {"default": "/path/to/file"}),
"file": (sorted(files),),
"encoding": ("STRING", {"default": "utf-8"}),
},
}
@@ -98,35 +25,9 @@ class LoadTextLocal:
CATEGORY = "不忘科技-自定义节点🚩/文本"
@classmethod
def VALIDATE_INPUTS(self, root_dir, file, **kwargs):
if file == "[none]" or not file or not file.strip():
return True
get_file(root_dir, file)
return True
def load(self, root_dir, file, encoding):
with open(get_file(root_dir, file), "r", encoding=encoding) as f:
return (f.read(),)
class LoadTextOnline:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"file_path": ("STRING", {"default": "input/"}),
"encoding": ("STRING", {"default": "utf-8"}),
}
}
RETURN_TYPES = ("STRING",)
FUNCTION = "load"
CATEGORY = "不忘科技-自定义节点🚩/文本"
def load(self, file_path, encoding):
def load(self, file_path, file, encoding):
if not (file_path and len(file_path.strip()) > 0 and file_path != "/path/to/file"):
file_path = folder_paths.get_annotated_filepath(file)
with open(file_path, "r", encoding=encoding) as f:
return (f.read(),)