ADD 增加自定义编码读取文件节点
This commit is contained in:
parent
cc0b3da745
commit
a6a30ca42b
|
|
@ -1,3 +1,4 @@
|
|||
from .nodes.text import LoadText
|
||||
from .nodes.traverse_folder import TraverseFolder
|
||||
from .nodes.unload_all_models import UnloadAllModels
|
||||
from .nodes.string_empty_judgement import StringEmptyJudgement
|
||||
|
|
@ -23,6 +24,7 @@ NODE_CLASS_MAPPINGS = {
|
|||
"StringEmptyJudgement": StringEmptyJudgement,
|
||||
"unloadAllModels": UnloadAllModels,
|
||||
"TraverseFolder": TraverseFolder,
|
||||
"LoadTextCustom": LoadText
|
||||
}
|
||||
|
||||
# A dictionary that contains the friendly/humanly readable titles for the nodes
|
||||
|
|
@ -37,5 +39,6 @@ NODE_DISPLAY_NAME_MAPPINGS = {
|
|||
"VideoPointCompute": "视频帧位计算",
|
||||
"StringEmptyJudgement": "字符串是否为空",
|
||||
"unloadAllModels": "卸载所有已加载模型",
|
||||
"TraverseFolder": "遍历文件夹"
|
||||
"TraverseFolder": "遍历文件夹",
|
||||
"LoadTextCustom": "读取文本文件"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,107 @@
|
|||
import json
|
||||
import os
|
||||
import folder_paths
|
||||
|
||||
|
||||
def get_allowed_dirs():
|
||||
dir = os.path.abspath(os.path.join(__file__, "../../user"))
|
||||
file = os.path.join(dir, "text_file_dirs.json")
|
||||
with open(file, "r") as f:
|
||||
return json.loads(f.read())
|
||||
|
||||
|
||||
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 LoadText:
|
||||
@classmethod
|
||||
def INPUT_TYPES(s):
|
||||
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
|
||||
}]
|
||||
}],
|
||||
}]
|
||||
}),
|
||||
"encoding": ("STRING", {"default": "utf-8"}),
|
||||
},
|
||||
}
|
||||
|
||||
RETURN_TYPES = ("STRING",)
|
||||
|
||||
FUNCTION = "load"
|
||||
|
||||
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(),)
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"input": "$input/**/*.txt",
|
||||
"output": "$output/**/*.txt",
|
||||
"temp": "$temp/**/*.txt"
|
||||
}
|
||||
Loading…
Reference in New Issue