mirror of
https://git.datalinker.icu/comfyanonymous/ComfyUI
synced 2026-09-06 11:47:03 +08:00
use pydantic_settings
This commit is contained in:
parent
52962f6192
commit
5a55f20f70
@ -1,14 +1,16 @@
|
|||||||
import os
|
import os
|
||||||
|
from pathlib import Path
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
import tomllib
|
|
||||||
|
|
||||||
from pydantic import ValidationError
|
from pydantic import ValidationError
|
||||||
|
from pydantic_settings import PydanticBaseSettingsSource, TomlConfigSettingsSource
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from comfy_config.types import (
|
from comfy_config.types import (
|
||||||
|
ComfyConfig,
|
||||||
ProjectConfig,
|
ProjectConfig,
|
||||||
PyProjectConfig,
|
PyProjectConfig,
|
||||||
|
PyProjectSettings
|
||||||
)
|
)
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@ -40,42 +42,46 @@ Example:
|
|||||||
>>> 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
|
>>> nodes.EXTENSION_WEB_DIRS[project_config.project.name] = js_dir
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
def extract_node_configuration(path) -> Optional[PyProjectConfig]:
|
def extract_node_configuration(path) -> Optional[PyProjectConfig]:
|
||||||
folder_name = os.path.basename(path)
|
folder_name = os.path.basename(path)
|
||||||
toml_path = os.path.join(path, "pyproject.toml")
|
toml_path = Path(path) / "pyproject.toml"
|
||||||
|
|
||||||
if not os.path.isfile(toml_path):
|
if not toml_path.exists():
|
||||||
logging.warning(
|
logging.warning("No pyproject.toml file found, using folder name as project name")
|
||||||
"No pyproject.toml file found in the current directory, will use custom node folder name as project name as default.")
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
project = ProjectConfig(name=folder_name)
|
project = ProjectConfig(name=folder_name)
|
||||||
return PyProjectConfig(project=project)
|
comfy = ComfyConfig()
|
||||||
|
return PyProjectConfig(project=project, tool_comfy=comfy)
|
||||||
except ValidationError as e:
|
except ValidationError as e:
|
||||||
logging.error(f"Failed to create default configuration: {e}")
|
logging.error(f"Failed to create default configuration: {e}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(toml_path, "rb") as f:
|
raw_settings = load_pyproject_settings(toml_path)
|
||||||
data = tomllib.load(f)
|
|
||||||
|
project_data = raw_settings.project
|
||||||
|
|
||||||
|
tool_data = raw_settings.tool
|
||||||
|
comfy_data = tool_data.get("comfy", {}) if tool_data else {}
|
||||||
|
|
||||||
|
return PyProjectConfig(project=project_data, tool_comfy=comfy_data)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f"Failed to read pyproject.toml: {e}")
|
logging.error(f"Failed to load configuration from {toml_path}: {e}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
try:
|
|
||||||
config_data = {
|
|
||||||
"project": data.get("project", {}),
|
|
||||||
"tool_comfy": data.get("tool", {}).get("comfy", {})
|
|
||||||
}
|
|
||||||
|
|
||||||
return PyProjectConfig(**config_data)
|
def load_pyproject_settings(toml_path: Path) -> PyProjectSettings:
|
||||||
|
class PyProjectLoader(PyProjectSettings):
|
||||||
|
@classmethod
|
||||||
|
def settings_customise_sources(
|
||||||
|
cls,
|
||||||
|
settings_cls,
|
||||||
|
init_settings: PydanticBaseSettingsSource,
|
||||||
|
env_settings: PydanticBaseSettingsSource,
|
||||||
|
dotenv_settings: PydanticBaseSettingsSource,
|
||||||
|
file_secret_settings: PydanticBaseSettingsSource,
|
||||||
|
):
|
||||||
|
return (TomlConfigSettingsSource(settings_cls, toml_path),)
|
||||||
|
|
||||||
except ValidationError as e:
|
return PyProjectLoader()
|
||||||
logging.error(f"Validation error while parsing configuration: {e}")
|
|
||||||
logging.error(f"Validation details: {e.errors()}")
|
|
||||||
return None
|
|
||||||
except Exception as e:
|
|
||||||
logging.error(f"Unexpected error while parsing configuration: {e}")
|
|
||||||
return None
|
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
from pydantic import BaseModel, Field
|
from pydantic import BaseModel, Field
|
||||||
|
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||||
from typing import List, Optional
|
from typing import List, Optional
|
||||||
|
|
||||||
# IMPORTANT: The type definitions specified in pyproject.toml for custom nodes
|
# IMPORTANT: The type definitions specified in pyproject.toml for custom nodes
|
||||||
@ -69,3 +70,11 @@ class ProjectConfig(BaseModel):
|
|||||||
class PyProjectConfig(BaseModel):
|
class PyProjectConfig(BaseModel):
|
||||||
project: ProjectConfig = Field(default_factory=ProjectConfig)
|
project: ProjectConfig = Field(default_factory=ProjectConfig)
|
||||||
tool_comfy: ComfyConfig = Field(default_factory=ComfyConfig)
|
tool_comfy: ComfyConfig = Field(default_factory=ComfyConfig)
|
||||||
|
|
||||||
|
|
||||||
|
class PyProjectSettings(BaseSettings):
|
||||||
|
project: dict = Field(default_factory=dict)
|
||||||
|
|
||||||
|
tool: dict = Field(default_factory=dict)
|
||||||
|
|
||||||
|
model_config = SettingsConfigDict()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user