Merge pull request #56 from StableLlama/enhance_string_save
Allow appending for text string saving
This commit is contained in:
@@ -958,6 +958,8 @@ class PathSaveStringFile(ComfyNodeABC):
|
||||
|
||||
This node takes a string and saves it to the specified path as a text file.
|
||||
Optionally, you can choose to create the directory if it doesn't exist.
|
||||
If 'append' is True, the text is appended to an existing file instead of
|
||||
overwriting it.
|
||||
"""
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
@@ -968,6 +970,7 @@ class PathSaveStringFile(ComfyNodeABC):
|
||||
},
|
||||
"optional": {
|
||||
"create_dirs": (IO.BOOLEAN, {"default": True}),
|
||||
"append": (IO.BOOLEAN, {"default": False}),
|
||||
"encoding": (IO.STRING, {"default": "utf-8"}),
|
||||
}
|
||||
}
|
||||
@@ -979,7 +982,7 @@ class PathSaveStringFile(ComfyNodeABC):
|
||||
FUNCTION = "save_text"
|
||||
OUTPUT_NODE = True
|
||||
|
||||
def save_text(self, text: str, path: str, create_dirs: bool = True, encoding: str = "utf-8"):
|
||||
def save_text(self, text: str, path: str, create_dirs: bool = True, append: bool = False, encoding: str = "utf-8"):
|
||||
if not path:
|
||||
print("Basic data handling: Save failed - no path specified")
|
||||
return (False,)
|
||||
@@ -990,10 +993,12 @@ class PathSaveStringFile(ComfyNodeABC):
|
||||
if directory and create_dirs and not os.path.exists(directory):
|
||||
os.makedirs(directory)
|
||||
|
||||
with open(path, "w", encoding=encoding) as f:
|
||||
mode = "a" if append else "w"
|
||||
with open(path, mode, encoding=encoding) as f:
|
||||
f.write(text)
|
||||
|
||||
print(f"Basic data handling: Successfully saved text to {path}")
|
||||
action = "appended" if append else "saved"
|
||||
print(f"Basic data handling: Successfully {action} text to {path}")
|
||||
return (True,)
|
||||
except Exception as e:
|
||||
print(f"Basic data handling: Error saving text file: {e}")
|
||||
|
||||
@@ -57,6 +57,25 @@ def test_path_load_save_string_file(tmp_path):
|
||||
# Test error handling
|
||||
assert load_node.load_text(str(tmp_path / "nonexistent.txt")) == ("", False)
|
||||
|
||||
def test_path_save_string_file_append(tmp_path):
|
||||
# Test appending to an existing file
|
||||
save_node = PathSaveStringFile()
|
||||
load_node = PathLoadStringFile()
|
||||
file_path = str(tmp_path / "append_test.txt")
|
||||
|
||||
# Save initial content
|
||||
assert save_node.save_text("First line\n", file_path) == (True,)
|
||||
|
||||
# Append to the file
|
||||
assert save_node.save_text("Second line\n", file_path, append=True) == (True,)
|
||||
|
||||
# Verify the content was appended, not overwritten
|
||||
assert load_node.load_text(file_path) == ("First line\nSecond line\n", True)
|
||||
|
||||
# Verify default behavior still overwrites
|
||||
assert save_node.save_text("Overwritten", file_path) == (True,)
|
||||
assert load_node.load_text(file_path) == ("Overwritten", True)
|
||||
|
||||
|
||||
def test_path_abspath():
|
||||
node = PathAbspath()
|
||||
|
||||
Reference in New Issue
Block a user