Skip to content

Commit 2a819fa

Browse files
committed
Clean up the code a bit
1 parent c01ac61 commit 2a819fa

File tree

3 files changed

+37
-48
lines changed

3 files changed

+37
-48
lines changed

.gitignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,3 @@ cython_debug/
145145
*.sql
146146
*.sqlite
147147
*.xml
148-
/workflow_api.json
149-
/workflow_api.py
150-
/custom_nodes/

comfyui_to_python.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@
1111
import black
1212

1313
sys.path.append('../')
14-
sys.path.append('.')
1514

16-
from utils import import_custom_nodes, add_comfyui_directory_to_sys_path, get_value_at_index, add_extra_model_paths
15+
try:
16+
from utils import import_custom_nodes, find_path, add_comfyui_directory_to_sys_path, add_extra_model_paths, get_value_at_index
17+
except ImportError:
18+
sys.path.append('.')
19+
from utils import import_custom_nodes, find_path, add_comfyui_directory_to_sys_path, add_extra_model_paths, get_value_at_index
1720

1821
from nodes import NODE_CLASS_MAPPINGS
1922

@@ -307,7 +310,7 @@ def assemble_python_code(self, import_statements: set, speical_functions_code: L
307310
"""
308311
# Get the source code of the utils functions as a string
309312
func_strings = []
310-
for func in [add_comfyui_directory_to_sys_path, get_value_at_index, add_extra_model_paths]:
313+
for func in [find_path, add_comfyui_directory_to_sys_path, add_extra_model_paths, get_value_at_index]:
311314
func_strings.append(f'\n{inspect.getsource(func)}')
312315
# Define static import statements required for the script
313316
static_imports = ['import os', 'import random', 'from pathlib import Path', 'import sys', 'from typing import Sequence, Mapping, Any, Union',
@@ -434,7 +437,7 @@ def execute(self):
434437
# Update class parameters here
435438
input_file = 'workflow_api.json'
436439
output_file = 'workflow_api.py'
437-
queue_size = 10
440+
queue_size = 2
438441

439442
# Convert ComfyUI workflow to Python
440443
ComfyUItoPython(input_file=input_file, output_file=output_file, queue_size=queue_size)

utils.py

Lines changed: 30 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -28,62 +28,51 @@ def import_custom_nodes() -> None:
2828
# Initializing custom nodes
2929
init_custom_nodes()
3030

31-
def add_comfyui_directory_to_sys_path() -> None:
31+
def find_path(name: str, path: str = None) -> Union[Path, None]:
3232
"""
33-
Recursively looks at parent folders starting from the current working directory until it finds 'ComfyUI'.
34-
Once found, the directory is added to sys.path.
33+
Recursively looks at parent folders starting from the given path until it finds the given name.
34+
Returns the path as a Path object if found, or None otherwise.
3535
"""
36-
start_path = os.getcwd() # Get the current working directory
36+
# If no path is given, use the current working directory
37+
if path is None:
38+
path = os.getcwd()
39+
40+
# Check if the current directory contains the name
41+
if name in os.listdir(path):
42+
path_name = os.path.join(path, name)
43+
print(f"{name} found: {path_name}")
44+
return Path(path_name)
3745

38-
def search_directory(path: str) -> None:
39-
# Check if the current directory contains 'ComfyUI'
40-
if 'ComfyUI' in os.listdir(path):
41-
directory_path = os.path.join(path, 'ComfyUI')
42-
sys.path.append(directory_path)
43-
print(f"ComfyUI found and added to sys.path: {directory_path}")
46+
# Get the parent directory
47+
parent_directory = os.path.dirname(path)
4448

45-
# Get the parent directory
46-
parent_directory = os.path.dirname(path)
49+
# If the parent directory is the same as the current directory, we've reached the root and stop the search
50+
if parent_directory == path:
51+
return None
4752

48-
# If the parent directory is the same as the current directory, we've reached the root and stop the search
49-
if parent_directory == path:
50-
return
53+
# Recursively call the function with the parent directory
54+
return find_path(name, parent_directory)
5155

52-
# Recursively call the function with the parent directory
53-
search_directory(parent_directory)
5456

55-
# Start the search from the current working directory
56-
search_directory(start_path)
57+
def add_comfyui_directory_to_sys_path() -> None:
58+
"""
59+
Add 'ComfyUI' to the sys.path
60+
"""
61+
comfyui_path = find_path('ComfyUI')
62+
if comfyui_path is not None and os.path.isdir(comfyui_path):
63+
sys.path.append(comfyui_path)
64+
print(f"'{comfyui_path}' added to sys.path")
5765

5866
def add_extra_model_paths() -> Path:
5967
"""
6068
Parse the optional extra_model_paths.yaml file and add the parsed paths to the sys.path.
6169
"""
62-
from pathlib import Path
6370
from main import load_extra_path_config
6471

65-
def find_config_file(path: str, name: str = "extra_model_paths.yaml") -> Path:
66-
# Check if the current directory contains the file
67-
if name in os.listdir(path):
68-
directory_path = os.path.join(path, name)
69-
print(f"{name} found: {directory_path}")
70-
return Path(directory_path)
71-
72-
# Get the parent directory
73-
parent_directory = os.path.dirname(path)
74-
75-
# If the parent directory is the same as the current directory, we've reached the root and stop the search
76-
if parent_directory == path:
77-
return
78-
79-
# Recursively call the function with the parent directory
80-
return find_config_file(parent_directory)
81-
82-
start_path = os.getcwd() # Get the current working directory
83-
file = find_config_file(start_path)
72+
extra_model_paths = find_path("extra_model_paths.yaml")
8473

85-
if os.path.isfile(file):
86-
load_extra_path_config(file)
74+
if extra_model_paths is not None:
75+
load_extra_path_config(extra_model_paths)
8776
else:
8877
print("Could not find the extra_model_paths config file.")
8978

0 commit comments

Comments
 (0)