-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathglobal_variables.py
160 lines (138 loc) · 5.67 KB
/
global_variables.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import os
import re
import folder_paths
import logging
class Everything(str):
def __ne__(self, __value: object) -> bool:
return False
# Define VAR_PATTERN at module level if not already defined
VAR_PATTERN = re.compile(r'^([a-zA-Z_][a-zA-Z0-9_]*)\s*=\s*(.+)$')
class SaveGlobalVariables:
def __init__(self):
self.base_dir = os.path.join(folder_paths.base_path, 'Bjornulf')
self.global_vars_dir = os.path.join(self.base_dir, 'GlobalVariables')
os.makedirs(self.global_vars_dir, exist_ok=True)
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"variables": ("STRING", {"multiline": True, "default": ""}),
"mode": (["append", "overwrite"], {"default": "append"}),
},
"optional": {
"filename": ("STRING", {"default": ""}),
},
}
RETURN_TYPES = ("STRING",)
RETURN_NAMES = ("connect_to_workflow",)
FUNCTION = "save_variables"
OUTPUT_NODE = True
CATEGORY = "Bjornulf"
def save_variables(self, variables, mode, filename=""):
# Determine target file path
if filename.strip():
filename_clean = os.path.basename(filename.strip())
if not filename_clean.endswith('.txt'):
filename_clean += '.txt'
file_path = os.path.join(self.global_vars_dir, filename_clean)
else:
file_path = os.path.join(self.base_dir, 'GlobalVariables.txt')
# Validate and parse input variables
valid_vars = {}
errors = []
for line in variables.split('\n'):
line = line.strip()
if not line:
continue
match = VAR_PATTERN.match(line)
if match:
var_name, var_value = match.groups()
logging.info(f"VALID syntax for Variable : {line}")
valid_vars[var_name] = line
else:
logging.info(f"Invalid syntax for Variable : {line}")
errors.append(f"Invalid syntax: {line}")
if errors:
print("\n".join(errors))
# Merge based on mode
if mode == "append":
# Always read existing variables first
existing_vars = {}
if os.path.exists(file_path):
try:
with open(file_path, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip()
if match := VAR_PATTERN.match(line):
var_name = match.group(1)
existing_vars[var_name] = line
except Exception as e:
logging.info(f"Error reading existing file: {e}")
merged_vars = {**existing_vars, **valid_vars}
else: # overwrite
merged_vars = valid_vars
# Ensure directory exists before writing
os.makedirs(os.path.dirname(file_path), exist_ok=True)
try:
with open(file_path, 'w', encoding='utf-8') as f:
if merged_vars:
f.write('\n'.join(merged_vars.values()) + '\n')
else:
f.write('') # Create empty file if no variables
except Exception as e:
logging.info(f"Error writing to file: {e}")
return ("",)
class LoadGlobalVariables:
def __init__(self):
self.base_dir = os.path.join(folder_paths.base_path, 'Bjornulf')
self.global_vars_dir = os.path.join(self.base_dir, 'GlobalVariables')
os.makedirs(self.global_vars_dir, exist_ok=True)
@classmethod
def INPUT_TYPES(cls):
var_files = []
try:
var_files = [f[:-4] for f in os.listdir(cls.global_vars_dir())
if f.endswith('.txt') and os.path.isfile(os.path.join(cls.global_vars_dir(), f))]
var_files.sort()
except FileNotFoundError:
pass
return {
"required": {
"seed": ("INT", {"default": -1, "min": -1, "max": 0x7FFFFFFFFFFFFFFF}),
},
"optional": {
"filename": ("STRING", {"default": ""}),
"file_list": (["default"] + var_files, {"default": "default"}),
"connect_to_workflow": (Everything("*"), {"forceInput": True}),
},
}
RETURN_TYPES = ("STRING",)
RETURN_NAMES = ("variables",)
FUNCTION = "load_variables"
CATEGORY = "Bjornulf"
@classmethod
def global_vars_dir(cls):
return os.path.join(folder_paths.base_path, 'Bjornulf', 'GlobalVariables')
def load_variables(self, seed, connect_to_workflow="", filename="", file_list="default"):
# First check if filename is provided and not empty
if filename.strip():
target_file = filename.strip()
else:
# If filename is empty, use file_list
target_file = file_list.strip()
if target_file and target_file != "default":
# Ensure .txt extension
if not target_file.endswith('.txt'):
target_file += '.txt'
# Load from GlobalVariables subdirectory
file_path = os.path.join(self.global_vars_dir, target_file)
else:
# Load default GlobalVariables.txt from base directory
file_path = os.path.join(self.base_dir, 'GlobalVariables.txt')
# Return empty string if file doesn't exist
if not os.path.exists(file_path):
return ("",)
# Read and return file contents
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
content = f.read().strip()
return (content,)