From 3eac2d0a7b2e8d319f8d20aa1fae892b82fb008c Mon Sep 17 00:00:00 2001 From: kevin96666 Date: Fri, 1 Mar 2024 13:03:08 +0100 Subject: [PATCH] wip --- prestartup_script.py | 59 +++++++++++++++++++++++++++++++++++++++- sdfx.config.json.example | 2 +- sdfx_path.py | 23 ++++++++-------- 3 files changed, 71 insertions(+), 13 deletions(-) diff --git a/prestartup_script.py b/prestartup_script.py index ba8128c..929c29e 100644 --- a/prestartup_script.py +++ b/prestartup_script.py @@ -1,4 +1,7 @@ import argparse +import os +import sys +import json # Hack to pass extra args to main.py def parse_args_override(self, args=None, namespace=None): @@ -11,4 +14,58 @@ parser = argparse.ArgumentParser() parser.add_argument("--sdfx-config-file", type=str, default=None, help="Path for sdfx config file") args = parser.parse_args() -#args.sdfx_config_file +sdfx_config_file_name = 'sdfx.config.json' + +#=== Best effort to find sdfx_config_file_path === +def find_sdfx_config_path(): + if args.sdfx_config_file is not None: + if os.path.exists(args.sdfx_config_file): + return args.sdfx_config_file + path = os.path.abspath(os.path.dirname(__file__)) + config_path_found = None + try: + for _ in range(6): + config_path = os.path.join(path, sdfx_config_file_name) + if os.path.exists(config_path): + config_path_found = config_path + path = os.path.dirname(path) + except Exception as e: + print(f"Error File access : {e}") + if config_path_found is not None: + return config_path_found + raise FileNotFoundError() + +def load_boot_config(): + try: + sdfx_config_file = find_sdfx_config_path() + with open(sdfx_config_file, 'r') as json_file: + data = json.load(json_file) + #=== load port from config file === + port = data.get('args', {}).get('port', None) + if isinstance(port, int): + sys.argv.append("--port=" + str(port)) + #=== load listen from config file === + listen = data.get('args', {}).get('listen', None) + if isinstance(listen, str): + sys.argv.append("--listen=" + listen) + #=== load disable-xformers from config file === + dx = data.get('args', {}).get('disable-xformers', None) + if isinstance(dx, bool) and dx == True: + sys.argv.append("--disable-xformers") + #=== load preview-method from config file === + pm = data.get('args', {}).get('preview-method', None) + if isinstance(pm, str): + sys.argv.append("--preview-method=" + pm) + #=== load enable-cors-header from config file === + ch = data.get('args', {}).get('enable-cors-header', None) + if isinstance(ch, bool) and ch == True: + sys.argv.append("--enable-cors-header") + + except json.JSONDecodeError as e: + print(f"[SDFXBridgeForComfyUI] FATAL -> {sdfx_config_file_name} is not valid, it will not be loaded !") + except FileNotFoundError: + print(f"[SDFXBridgeForComfyUI] FATAL -> {sdfx_config_file_name} not found and not specified") + except Exception as e: + print(f"[SDFXBridgeForComfyUI] FATAL -> : {e}") + +load_boot_config() \ No newline at end of file diff --git a/sdfx.config.json.example b/sdfx.config.json.example index b703ffc..8054d3a 100644 --- a/sdfx.config.json.example +++ b/sdfx.config.json.example @@ -2,8 +2,8 @@ "args": { "disable-xformers": false, "preview-method": "taesd", - "listen": true, "enable-cors-header": true, + "listen": "127.0.0.1", "port": 8188 }, diff --git a/sdfx_path.py b/sdfx_path.py index 0d00ec5..0b7b63f 100644 --- a/sdfx_path.py +++ b/sdfx_path.py @@ -1,7 +1,11 @@ import os +import argparse import json import folder_paths -from .prestartup_script import args + +parser = argparse.ArgumentParser() +parser.add_argument("--sdfx-config-file", type=str, default=None, help="Path for sdfx config file") +args = parser.parse_args() sdfx_config_file_name = 'sdfx.config.json' @@ -37,16 +41,13 @@ def get_sdfx_absolute_path(path): sdfx_config_path = find_sdfx_config_path() except FileNotFoundError: sdfx_config_path = os.path.abspath(os.path.dirname(__file__)) + return os.path.join(sdfx_config_path, path) parent_path = os.path.dirname(sdfx_config_path) return os.path.join(parent_path, path) -isPathFound = True -try: - gallery_path = get_sdfx_absolute_path('data/media/gallery') - workflows_path = get_sdfx_absolute_path('data/media/worflows') - templates_path = get_sdfx_absolute_path('data/media/gallery') -except FileNotFoundError: - isPathFound = False +gallery_path = get_sdfx_absolute_path('data/media/gallery') +workflows_path = get_sdfx_absolute_path('data/media/workflows') +templates_path = get_sdfx_absolute_path('data/media/templates') #=== add all sdfx path to comfy folder_paths === def load_sdfx_extra_path_config(): @@ -108,10 +109,10 @@ def load_sdfx_extra_path_config(): print(f"[SDFXBridgeForComfyUI] FATAL -> : {e}") def get_gallery_path(): - return None if not isPathFound else gallery_path + return gallery_path def get_workflows_path(): - return None if not isPathFound else workflows_path + return workflows_path def get_templates_path(): - return None if not isPathFound else templates_path \ No newline at end of file + return templates_path \ No newline at end of file