zk-Armor commited on
Commit
0fa11d9
·
verified ·
1 Parent(s): c26f82b

Upload ComfyUI/comfy_config

Browse files
ComfyUI/comfy_config/config_parser.py ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from pathlib import Path
3
+ from typing import Optional
4
+
5
+ from pydantic_settings import PydanticBaseSettingsSource, TomlConfigSettingsSource
6
+
7
+ from comfy_config.types import (
8
+ ComfyConfig,
9
+ ProjectConfig,
10
+ PyProjectConfig,
11
+ PyProjectSettings
12
+ )
13
+
14
+ def validate_and_extract_os_classifiers(classifiers: list) -> list:
15
+ os_classifiers = [c for c in classifiers if c.startswith("Operating System :: ")]
16
+ if not os_classifiers:
17
+ return []
18
+
19
+ os_values = [c[len("Operating System :: ") :] for c in os_classifiers]
20
+ valid_os_prefixes = {"Microsoft", "POSIX", "MacOS", "OS Independent"}
21
+
22
+ for os_value in os_values:
23
+ if not any(os_value.startswith(prefix) for prefix in valid_os_prefixes):
24
+ return []
25
+
26
+ return os_values
27
+
28
+
29
+ def validate_and_extract_accelerator_classifiers(classifiers: list) -> list:
30
+ accelerator_classifiers = [c for c in classifiers if c.startswith("Environment ::")]
31
+ if not accelerator_classifiers:
32
+ return []
33
+
34
+ accelerator_values = [c[len("Environment :: ") :] for c in accelerator_classifiers]
35
+
36
+ valid_accelerators = {
37
+ "GPU :: NVIDIA CUDA",
38
+ "GPU :: AMD ROCm",
39
+ "GPU :: Intel Arc",
40
+ "NPU :: Huawei Ascend",
41
+ "GPU :: Apple Metal",
42
+ }
43
+
44
+ for accelerator_value in accelerator_values:
45
+ if accelerator_value not in valid_accelerators:
46
+ return []
47
+
48
+ return accelerator_values
49
+
50
+
51
+ """
52
+ Extract configuration from a custom node directory's pyproject.toml file or a Python file.
53
+
54
+ This function reads and parses the pyproject.toml file in the specified directory
55
+ to extract project and ComfyUI-specific configuration information. If no
56
+ pyproject.toml file is found, it creates a minimal configuration using the
57
+ folder name as the project name. If a Python file is provided, it uses the
58
+ file name (without extension) as the project name.
59
+
60
+ Args:
61
+ path (str): Path to the directory containing the pyproject.toml file, or
62
+ path to a .py file. If pyproject.toml doesn't exist in a directory,
63
+ the folder name will be used as the default project name. If a .py
64
+ file is provided, the filename (without .py extension) will be used
65
+ as the project name.
66
+
67
+ Returns:
68
+ Optional[PyProjectConfig]: A PyProjectConfig object containing:
69
+ - project: Basic project information (name, version, dependencies, etc.)
70
+ - tool_comfy: ComfyUI-specific configuration (publisher_id, models, etc.)
71
+ Returns None if configuration extraction fails or if the provided file
72
+ is not a Python file.
73
+
74
+ Notes:
75
+ - If pyproject.toml is missing in a directory, creates a default config with folder name
76
+ - If a .py file is provided, creates a default config with filename (without extension)
77
+ - Returns None for non-Python files
78
+
79
+ Example:
80
+ >>> from comfy_config import config_parser
81
+ >>> # For directory
82
+ >>> custom_node_dir = os.path.dirname(os.path.realpath(__file__))
83
+ >>> project_config = config_parser.extract_node_configuration(custom_node_dir)
84
+ >>> print(project_config.project.name) # "my_custom_node" or name from pyproject.toml
85
+ >>>
86
+ >>> # For single-file Python node file
87
+ >>> py_file_path = os.path.realpath(__file__) # "/path/to/my_node.py"
88
+ >>> project_config = config_parser.extract_node_configuration(py_file_path)
89
+ >>> print(project_config.project.name) # "my_node"
90
+ """
91
+ def extract_node_configuration(path) -> Optional[PyProjectConfig]:
92
+ if os.path.isfile(path):
93
+ file_path = Path(path)
94
+
95
+ if file_path.suffix.lower() != '.py':
96
+ return None
97
+
98
+ project_name = file_path.stem
99
+ project = ProjectConfig(name=project_name)
100
+ comfy = ComfyConfig()
101
+ return PyProjectConfig(project=project, tool_comfy=comfy)
102
+
103
+ folder_name = os.path.basename(path)
104
+ toml_path = Path(path) / "pyproject.toml"
105
+
106
+ if not toml_path.exists():
107
+ project = ProjectConfig(name=folder_name)
108
+ comfy = ComfyConfig()
109
+ return PyProjectConfig(project=project, tool_comfy=comfy)
110
+
111
+ raw_settings = load_pyproject_settings(toml_path)
112
+
113
+ project_data = raw_settings.project
114
+
115
+ tool_data = raw_settings.tool
116
+ comfy_data = tool_data.get("comfy", {}) if tool_data else {}
117
+
118
+ dependencies = project_data.get("dependencies", [])
119
+ supported_comfyui_frontend_version = ""
120
+ for dep in dependencies:
121
+ if isinstance(dep, str) and dep.startswith("comfyui-frontend-package"):
122
+ supported_comfyui_frontend_version = dep.removeprefix("comfyui-frontend-package")
123
+ break
124
+
125
+ supported_comfyui_version = comfy_data.get("requires-comfyui", "")
126
+
127
+ classifiers = project_data.get('classifiers', [])
128
+ supported_os = validate_and_extract_os_classifiers(classifiers)
129
+ supported_accelerators = validate_and_extract_accelerator_classifiers(classifiers)
130
+
131
+ project_data['supported_os'] = supported_os
132
+ project_data['supported_accelerators'] = supported_accelerators
133
+ project_data['supported_comfyui_frontend_version'] = supported_comfyui_frontend_version
134
+ project_data['supported_comfyui_version'] = supported_comfyui_version
135
+
136
+ return PyProjectConfig(project=project_data, tool_comfy=comfy_data)
137
+
138
+
139
+ def load_pyproject_settings(toml_path: Path) -> PyProjectSettings:
140
+ class PyProjectLoader(PyProjectSettings):
141
+ @classmethod
142
+ def settings_customise_sources(
143
+ cls,
144
+ settings_cls,
145
+ init_settings: PydanticBaseSettingsSource,
146
+ env_settings: PydanticBaseSettingsSource,
147
+ dotenv_settings: PydanticBaseSettingsSource,
148
+ file_secret_settings: PydanticBaseSettingsSource,
149
+ ):
150
+ return (TomlConfigSettingsSource(settings_cls, toml_path),)
151
+
152
+ return PyProjectLoader()
ComfyUI/comfy_config/types.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pydantic import BaseModel, Field, field_validator
2
+ from pydantic_settings import BaseSettings, SettingsConfigDict
3
+ from typing import List, Optional
4
+
5
+ # IMPORTANT: The type definitions specified in pyproject.toml for custom nodes
6
+ # must remain synchronized with the corresponding files in the https://github.com/Comfy-Org/comfy-cli/blob/main/comfy_cli/registry/types.py.
7
+ # Any changes to one must be reflected in the other to maintain consistency.
8
+
9
+ class NodeVersion(BaseModel):
10
+ changelog: str
11
+ dependencies: List[str]
12
+ deprecated: bool
13
+ id: str
14
+ version: str
15
+ download_url: str
16
+
17
+
18
+ class Node(BaseModel):
19
+ id: str
20
+ name: str
21
+ description: str
22
+ author: Optional[str] = None
23
+ license: Optional[str] = None
24
+ icon: Optional[str] = None
25
+ repository: Optional[str] = None
26
+ tags: List[str] = Field(default_factory=list)
27
+ latest_version: Optional[NodeVersion] = None
28
+
29
+
30
+ class PublishNodeVersionResponse(BaseModel):
31
+ node_version: NodeVersion
32
+ signedUrl: str
33
+
34
+
35
+ class URLs(BaseModel):
36
+ homepage: str = Field(default="", alias="Homepage")
37
+ documentation: str = Field(default="", alias="Documentation")
38
+ repository: str = Field(default="", alias="Repository")
39
+ issues: str = Field(default="", alias="Issues")
40
+
41
+
42
+ class Model(BaseModel):
43
+ location: str
44
+ model_url: str
45
+
46
+
47
+ class ComfyConfig(BaseModel):
48
+ publisher_id: str = Field(default="", alias="PublisherId")
49
+ display_name: str = Field(default="", alias="DisplayName")
50
+ icon: str = Field(default="", alias="Icon")
51
+ models: List[Model] = Field(default_factory=list, alias="Models")
52
+ includes: List[str] = Field(default_factory=list)
53
+ web: Optional[str] = None
54
+ banner_url: str = ""
55
+
56
+ class License(BaseModel):
57
+ file: str = ""
58
+ text: str = ""
59
+
60
+
61
+ class ProjectConfig(BaseModel):
62
+ name: str = ""
63
+ description: str = ""
64
+ version: str = "1.0.0"
65
+ requires_python: str = Field(default=">= 3.9", alias="requires-python")
66
+ dependencies: List[str] = Field(default_factory=list)
67
+ license: License = Field(default_factory=License)
68
+ urls: URLs = Field(default_factory=URLs)
69
+ supported_os: List[str] = Field(default_factory=list)
70
+ supported_accelerators: List[str] = Field(default_factory=list)
71
+ supported_comfyui_version: str = ""
72
+ supported_comfyui_frontend_version: str = ""
73
+
74
+ @field_validator('license', mode='before')
75
+ @classmethod
76
+ def validate_license(cls, v):
77
+ if isinstance(v, str):
78
+ return License(text=v)
79
+ elif isinstance(v, dict):
80
+ return License(**v)
81
+ elif isinstance(v, License):
82
+ return v
83
+ else:
84
+ return License()
85
+
86
+
87
+ class PyProjectConfig(BaseModel):
88
+ project: ProjectConfig = Field(default_factory=ProjectConfig)
89
+ tool_comfy: ComfyConfig = Field(default_factory=ComfyConfig)
90
+
91
+
92
+ class PyProjectSettings(BaseSettings):
93
+ project: dict = Field(default_factory=dict)
94
+
95
+ tool: dict = Field(default_factory=dict)
96
+
97
+ model_config = SettingsConfigDict(extra='allow')