-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathload_text.py
92 lines (70 loc) · 2.97 KB
/
load_text.py
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
import os
class LoadTextFromFolder:
@classmethod
def INPUT_TYPES(cls):
"""Define input parameters for the node"""
default_dir = "Bjornulf/Text"
available_files = []
if os.path.exists(default_dir):
available_files = [f for f in os.listdir(default_dir)
if f.lower().endswith('.txt')]
if not available_files:
available_files = ["no_files_found"]
return {
"required": {
"text_file": (available_files, {"default": available_files[0]}),
}
}
RETURN_TYPES = ("STRING", "STRING", "STRING")
RETURN_NAMES = ("text", "filename", "full_path")
FUNCTION = "load_text"
CATEGORY = "Bjornulf"
def load_text(self, text_file):
try:
if text_file == "no_files_found":
raise ValueError("No text files found in Bjornulf/Text folder")
filepath = os.path.join("Bjornulf/Text", text_file)
# Check if file exists
if not os.path.exists(filepath):
raise ValueError(f"File not found: {filepath}")
# Get absolute path
full_path = os.path.abspath(filepath)
# Get just the filename
filename = os.path.basename(filepath)
# Read text from file
with open(filepath, 'r', encoding='utf-8') as file:
text = file.read()
return (text, filename, full_path)
except (OSError, IOError) as e:
raise ValueError(f"Error loading file: {str(e)}")
class LoadTextFromPath:
@classmethod
def INPUT_TYPES(cls):
"""Define input parameters for the node"""
return {
"required": {
"file_path": ("STRING", {"default": "Bjornulf/Text/example.txt"}),
}
}
RETURN_TYPES = ("STRING", "STRING", "STRING")
RETURN_NAMES = ("text", "filename", "full_path")
FUNCTION = "load_text"
CATEGORY = "Bjornulf"
def load_text(self, file_path):
try:
# Validate file extension
if not file_path.lower().endswith('.txt'):
raise ValueError("File must be a .txt file")
# Check if file exists
if not os.path.exists(file_path):
raise ValueError(f"File not found: {file_path}")
# Get absolute path
full_path = os.path.abspath(file_path)
# Get just the filename
filename = os.path.basename(file_path)
# Read text from file
with open(file_path, 'r', encoding='utf-8') as file:
text = file.read()
return (text, filename, full_path)
except (OSError, IOError) as e:
raise ValueError(f"Error loading file: {str(e)}")