From 845f2fbc91a927876d57ee839d3eedf0c61bb24b Mon Sep 17 00:00:00 2001 From: Level Pixel Dev Date: Mon, 3 Feb 2025 18:15:08 +0600 Subject: [PATCH] Added FindValueFromFile --- nodes/io/text_inputs_LP.py | 51 +++++++++++++++++++++++++++++++++++++- web/js/widgets.js | 34 +++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 1 deletion(-) diff --git a/nodes/io/text_inputs_LP.py b/nodes/io/text_inputs_LP.py index de6a4f8..b21da8d 100644 --- a/nodes/io/text_inputs_LP.py +++ b/nodes/io/text_inputs_LP.py @@ -1,4 +1,4 @@ - +import os class Text: @@ -42,12 +42,61 @@ class String: def string(STRING): return STRING, +class FindValueFromFile: + + def __init__(self): + pass + + @classmethod + def INPUT_TYPES(s): + + return {"required": { + "key": ("STRING", {"default": '', "multiline": False}), + "input_path": ("STRING", {"default": '', "multiline": False}) + } + } + + RETURN_TYPES = ("STRING", "BOOLEAN") + RETURN_NAMES = ("Value STRING", "Value received BOOL") + + FUNCTION = "find_value_from_file" + CATEGORY = "LevelPixel/IO" + + def find_value_from_file(self, key, input_path=None): + valueString = "" + boolResult = True + log = [] + log.append("") + try: + with open(os.path.normpath(input_path), 'r', encoding='utf-8') as file: + for line in file: + line = line.strip() + + if "-->" in line: + keyLine, valueLine = line.split("-->", 1) + if keyLine.strip() == key: + valueString = valueLine.strip() + break + except FileNotFoundError: + log[0] = log[0] + f"Error: File not found at {input_path}" + boolResult = False + except Exception as e: + log[0] = log[0] + f"Error: {e}" + boolResult = False + + if valueString == "": + boolResult = False + + return {"ui": {"text": valueString, "log": log,}, "result": (valueString, boolResult,)} + NODE_CLASS_MAPPINGS = { "Text|LP": Text, "String|LP": String, + "FindValueFromFile|LP": FindValueFromFile, } NODE_DISPLAY_NAME_MAPPINGS = { "Text|LP": "Text [LP]", "String|LP": "String [LP]", + "FindValueFromFile|LP": "Find Value From File [LP]", } \ No newline at end of file diff --git a/web/js/widgets.js b/web/js/widgets.js index 25d5e56..1b3da8e 100644 --- a/web/js/widgets.js +++ b/web/js/widgets.js @@ -205,3 +205,37 @@ app.registerExtension({ } }, }); + + +app.registerExtension({ + name: "levelpixel.FindValueFromFile", + async beforeRegisterNodeDef(nodeType, nodeData, app) { + levelpixel.addStatusTagHandler(nodeType); + + if (nodeData.name === "FindValueFromFile|LP") { + const onExecuted = nodeType.prototype.onExecuted; + nodeType.prototype.onExecuted = function (message) { + const r = onExecuted?.apply?.(this, arguments); + + const pos = this.widgets.findIndex((w) => w.name === "log"); + if (pos !== -1) { + for (let i = pos; i < this.widgets.length; i++) { + this.widgets[i].onRemove?.(); + } + this.widgets.length = pos; + } + + for (const list of message.log) { + const w = ComfyWidgets["STRING"](this, "log", ["STRING", { multiline: true }], app).widget; + w.inputEl.readOnly = true; + w.inputEl.style.opacity = 0.6; + w.value = list; + } + + this.onResize?.(this.size); + + return r; + }; + } + }, +}); \ No newline at end of file