remove unnecessary try/catch and handle single-file python node

This commit is contained in:
Terry Jia 2025-06-02 20:51:33 -04:00
parent 5a55f20f70
commit 49836fb7f1

View File

@ -14,61 +14,73 @@ from comfy_config.types import (
) )
""" """
Extract configuration from a custom node directory's pyproject.toml file. Extract configuration from a custom node directory's pyproject.toml file or a Python file.
This function reads and parses the pyproject.toml file in the specified directory This function reads and parses the pyproject.toml file in the specified directory
to extract project and ComfyUI-specific configuration information. If no to extract project and ComfyUI-specific configuration information. If no
pyproject.toml file is found, it creates a minimal configuration using the pyproject.toml file is found, it creates a minimal configuration using the
folder name as the project name. folder name as the project name. If a Python file is provided, it uses the
file name (without extension) as the project name.
Args: Args:
path (str): Path to the directory containing the pyproject.toml file. path (str): Path to the directory containing the pyproject.toml file, or
If pyproject.toml doesn't exist, the folder name will be used path to a .py file. If pyproject.toml doesn't exist in a directory,
as the default project name. the folder name will be used as the default project name. If a .py
file is provided, the filename (without .py extension) will be used
as the project name.
Returns: Returns:
Optional[PyProjectConfig]: A PyProjectConfig object containing: Optional[PyProjectConfig]: A PyProjectConfig object containing:
- project: Basic project information (name, version, dependencies, etc.) - project: Basic project information (name, version, dependencies, etc.)
- tool_comfy: ComfyUI-specific configuration (publisher_id, models, etc.) - tool_comfy: ComfyUI-specific configuration (publisher_id, models, etc.)
Returns None if configuration extraction fails. Returns None if configuration extraction fails or if the provided file
is not a Python file.
Notes: Notes:
- If pyproject.toml is missing, creates a default config with folder name - If pyproject.toml is missing in a directory, creates a default config with folder name
- If a .py file is provided, creates a default config with filename (without extension)
- Returns None for non-Python files
Example: Example:
>>> from comfy_config import config_parser >>> from comfy_config import config_parser
>>> # For directory
>>> custom_node_dir = os.path.dirname(os.path.realpath(__file__)) >>> custom_node_dir = os.path.dirname(os.path.realpath(__file__))
>>> project_config = config_parser.extract_node_configuration(custom_node_dir) >>> project_config = config_parser.extract_node_configuration(custom_node_dir)
>>> print(project_config.project.name) # "my_custom_node" or name from pyproject.toml >>> print(project_config.project.name) # "my_custom_node" or name from pyproject.toml
>>> nodes.EXTENSION_WEB_DIRS[project_config.project.name] = js_dir >>>
>>> # For single-file Python node file
>>> py_file_path = os.path.realpath(__file__) # "/path/to/my_node.py"
>>> project_config = config_parser.extract_node_configuration(py_file_path)
>>> print(project_config.project.name) # "my_node"
""" """
def extract_node_configuration(path) -> Optional[PyProjectConfig]: def extract_node_configuration(path) -> Optional[PyProjectConfig]:
if os.path.isfile(path):
file_path = Path(path)
if file_path.suffix.lower() != '.py':
return None
project_name = file_path.stem
project = ProjectConfig(name=project_name)
comfy = ComfyConfig()
return PyProjectConfig(project=project, tool_comfy=comfy)
folder_name = os.path.basename(path) folder_name = os.path.basename(path)
toml_path = Path(path) / "pyproject.toml" toml_path = Path(path) / "pyproject.toml"
if not toml_path.exists(): if not toml_path.exists():
logging.warning("No pyproject.toml file found, using folder name as project name") project = ProjectConfig(name=folder_name)
comfy = ComfyConfig()
return PyProjectConfig(project=project, tool_comfy=comfy)
try: raw_settings = load_pyproject_settings(toml_path)
project = ProjectConfig(name=folder_name)
comfy = ComfyConfig()
return PyProjectConfig(project=project, tool_comfy=comfy)
except ValidationError as e:
logging.error(f"Failed to create default configuration: {e}")
return None
try: project_data = raw_settings.project
raw_settings = load_pyproject_settings(toml_path)
project_data = raw_settings.project tool_data = raw_settings.tool
comfy_data = tool_data.get("comfy", {}) if tool_data else {}
tool_data = raw_settings.tool return PyProjectConfig(project=project_data, tool_comfy=comfy_data)
comfy_data = tool_data.get("comfy", {}) if tool_data else {}
return PyProjectConfig(project=project_data, tool_comfy=comfy_data)
except Exception as e:
logging.error(f"Failed to load configuration from {toml_path}: {e}")
return None
def load_pyproject_settings(toml_path: Path) -> PyProjectSettings: def load_pyproject_settings(toml_path: Path) -> PyProjectSettings: