diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..299274b --- /dev/null +++ b/__init__.py @@ -0,0 +1,12 @@ +from line_counter import TextFileLineCounter + + +NODE_CLASS_MAPPINGS = { + "TextFileLineCounter": TextFileLineCounter, +} + +NODE_DISPLAY_NAME_MAPPINGS = { + "TextFileLineCounter": "🤑 Text File Line Counter", +} + +__all__ = ['NODE_CLASS_MAPPINGS', 'NODE_DISPLAY_NAME_MAPPINGS'] \ No newline at end of file diff --git a/line_counter.py b/line_counter.py new file mode 100644 index 0000000..e385498 --- /dev/null +++ b/line_counter.py @@ -0,0 +1,50 @@ +import os +class TextFileLineCounter(): + def __init__(self): + pass + + RETURN_TYPES = ("INT","NUMBER","STRING") # Ensure this matches ComfyUI's expectations + RETURN_NAMES = () + FUNCTION = "count_lines" + + CATEGORY = "line_counter" + @classmethod + def INPUT_TYPES(cls): + return { + "required": { + "file_path": ("STRING", {"default": "", "multiline": False}), + }, + } + + @classmethod + def OUTPUT_TYPES(cls): + return { + "outputs": { + "line_count_int": ("INT",), + "line_count_number": ("NUMBER",), + "line_count_string": ("STRING",), + }, + } + + def count_lines(self, file_path: str) -> dict: + if not os.path.isfile(file_path): + raise FileNotFoundError(f"The file at path '{file_path}' does not exist.") + with open(file_path, 'r', encoding='utf-8') as file: + lines = file.readlines() + line_count = len([line for line in lines if line.strip() != ""]) - 1 + return { + "line_count_int": line_count, # Integer + "line_count_number": float(line_count), # Number (float) + "line_count_string": str(line_count) # String + } + + def test(self): + return () + +NODE_CLASS_MAPPINGS = { +"TextFileLineCounter": TextFileLineCounter, +} + +NODE_DISPLAY_NAME_MAPPINGS = { +"TextFileLineCounter": "🤑 Text File Line Counter", +}