Clean up the code a bit

This commit is contained in:
Dimitar Tonev
2023-08-22 21:16:26 +02:00
parent c01ac61bc1
commit 2a819fabf0
3 changed files with 41 additions and 52 deletions
-3
View File
@@ -145,6 +145,3 @@ cython_debug/
*.sql
*.sqlite
*.xml
/workflow_api.json
/workflow_api.py
/custom_nodes/
+7 -4
View File
@@ -11,9 +11,12 @@ from typing import Dict, List, Any, Callable, Tuple
import black
sys.path.append('../')
sys.path.append('.')
from utils import import_custom_nodes, add_comfyui_directory_to_sys_path, get_value_at_index, add_extra_model_paths
try:
from utils import import_custom_nodes, find_path, add_comfyui_directory_to_sys_path, add_extra_model_paths, get_value_at_index
except ImportError:
sys.path.append('.')
from utils import import_custom_nodes, find_path, add_comfyui_directory_to_sys_path, add_extra_model_paths, get_value_at_index
from nodes import NODE_CLASS_MAPPINGS
@@ -307,7 +310,7 @@ class CodeGenerator:
"""
# Get the source code of the utils functions as a string
func_strings = []
for func in [add_comfyui_directory_to_sys_path, get_value_at_index, add_extra_model_paths]:
for func in [find_path, add_comfyui_directory_to_sys_path, add_extra_model_paths, get_value_at_index]:
func_strings.append(f'\n{inspect.getsource(func)}')
# Define static import statements required for the script
static_imports = ['import os', 'import random', 'from pathlib import Path', 'import sys', 'from typing import Sequence, Mapping, Any, Union',
@@ -434,7 +437,7 @@ if __name__ == '__main__':
# Update class parameters here
input_file = 'workflow_api.json'
output_file = 'workflow_api.py'
queue_size = 10
queue_size = 2
# Convert ComfyUI workflow to Python
ComfyUItoPython(input_file=input_file, output_file=output_file, queue_size=queue_size)
+34 -45
View File
@@ -28,62 +28,51 @@ def import_custom_nodes() -> None:
# Initializing custom nodes
init_custom_nodes()
def find_path(name: str, path: str = None) -> Union[Path, None]:
"""
Recursively looks at parent folders starting from the given path until it finds the given name.
Returns the path as a Path object if found, or None otherwise.
"""
# If no path is given, use the current working directory
if path is None:
path = os.getcwd()
# Check if the current directory contains the name
if name in os.listdir(path):
path_name = os.path.join(path, name)
print(f"{name} found: {path_name}")
return Path(path_name)
# Get the parent directory
parent_directory = os.path.dirname(path)
# If the parent directory is the same as the current directory, we've reached the root and stop the search
if parent_directory == path:
return None
# Recursively call the function with the parent directory
return find_path(name, parent_directory)
def add_comfyui_directory_to_sys_path() -> None:
"""
Recursively looks at parent folders starting from the current working directory until it finds 'ComfyUI'.
Once found, the directory is added to sys.path.
Add 'ComfyUI' to the sys.path
"""
start_path = os.getcwd() # Get the current working directory
def search_directory(path: str) -> None:
# Check if the current directory contains 'ComfyUI'
if 'ComfyUI' in os.listdir(path):
directory_path = os.path.join(path, 'ComfyUI')
sys.path.append(directory_path)
print(f"ComfyUI found and added to sys.path: {directory_path}")
# Get the parent directory
parent_directory = os.path.dirname(path)
# If the parent directory is the same as the current directory, we've reached the root and stop the search
if parent_directory == path:
return
# Recursively call the function with the parent directory
search_directory(parent_directory)
# Start the search from the current working directory
search_directory(start_path)
comfyui_path = find_path('ComfyUI')
if comfyui_path is not None and os.path.isdir(comfyui_path):
sys.path.append(comfyui_path)
print(f"'{comfyui_path}' added to sys.path")
def add_extra_model_paths() -> Path:
"""
Parse the optional extra_model_paths.yaml file and add the parsed paths to the sys.path.
"""
from pathlib import Path
from main import load_extra_path_config
def find_config_file(path: str, name: str = "extra_model_paths.yaml") -> Path:
# Check if the current directory contains the file
if name in os.listdir(path):
directory_path = os.path.join(path, name)
print(f"{name} found: {directory_path}")
return Path(directory_path)
# Get the parent directory
parent_directory = os.path.dirname(path)
# If the parent directory is the same as the current directory, we've reached the root and stop the search
if parent_directory == path:
return
# Recursively call the function with the parent directory
return find_config_file(parent_directory)
start_path = os.getcwd() # Get the current working directory
file = find_config_file(start_path)
extra_model_paths = find_path("extra_model_paths.yaml")
if os.path.isfile(file):
load_extra_path_config(file)
if extra_model_paths is not None:
load_extra_path_config(extra_model_paths)
else:
print("Could not find the extra_model_paths config file.")