Spaces:
Runtime error
Runtime error
Update app/custom_node_manager.py
Browse files- app/custom_node_manager.py +134 -134
app/custom_node_manager.py
CHANGED
|
@@ -1,134 +1,134 @@
|
|
| 1 |
-
from __future__ import annotations
|
| 2 |
-
|
| 3 |
-
import os
|
| 4 |
-
import folder_paths
|
| 5 |
-
import glob
|
| 6 |
-
from aiohttp import web
|
| 7 |
-
import json
|
| 8 |
-
import logging
|
| 9 |
-
from functools import lru_cache
|
| 10 |
-
|
| 11 |
-
from
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
# Extra locale files to load into main.json
|
| 15 |
-
EXTRA_LOCALE_FILES = [
|
| 16 |
-
"nodeDefs.json",
|
| 17 |
-
"commands.json",
|
| 18 |
-
"settings.json",
|
| 19 |
-
]
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
def safe_load_json_file(file_path: str) -> dict:
|
| 23 |
-
if not os.path.exists(file_path):
|
| 24 |
-
return {}
|
| 25 |
-
|
| 26 |
-
try:
|
| 27 |
-
with open(file_path, "r", encoding="utf-8") as f:
|
| 28 |
-
return json.load(f)
|
| 29 |
-
except json.JSONDecodeError:
|
| 30 |
-
logging.error(f"Error loading {file_path}")
|
| 31 |
-
return {}
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
class CustomNodeManager:
|
| 35 |
-
@lru_cache(maxsize=1)
|
| 36 |
-
def build_translations(self):
|
| 37 |
-
"""Load all custom nodes translations during initialization. Translations are
|
| 38 |
-
expected to be loaded from `locales/` folder.
|
| 39 |
-
|
| 40 |
-
The folder structure is expected to be the following:
|
| 41 |
-
- custom_nodes/
|
| 42 |
-
- custom_node_1/
|
| 43 |
-
- locales/
|
| 44 |
-
- en/
|
| 45 |
-
- main.json
|
| 46 |
-
- commands.json
|
| 47 |
-
- settings.json
|
| 48 |
-
|
| 49 |
-
returned translations are expected to be in the following format:
|
| 50 |
-
{
|
| 51 |
-
"en": {
|
| 52 |
-
"nodeDefs": {...},
|
| 53 |
-
"commands": {...},
|
| 54 |
-
"settings": {...},
|
| 55 |
-
...{other main.json keys}
|
| 56 |
-
}
|
| 57 |
-
}
|
| 58 |
-
"""
|
| 59 |
-
|
| 60 |
-
translations = {}
|
| 61 |
-
|
| 62 |
-
for folder in folder_paths.get_folder_paths("custom_nodes"):
|
| 63 |
-
# Sort glob results for deterministic ordering
|
| 64 |
-
for custom_node_dir in sorted(glob.glob(os.path.join(folder, "*/"))):
|
| 65 |
-
locales_dir = os.path.join(custom_node_dir, "locales")
|
| 66 |
-
if not os.path.exists(locales_dir):
|
| 67 |
-
continue
|
| 68 |
-
|
| 69 |
-
for lang_dir in glob.glob(os.path.join(locales_dir, "*/")):
|
| 70 |
-
lang_code = os.path.basename(os.path.dirname(lang_dir))
|
| 71 |
-
|
| 72 |
-
if lang_code not in translations:
|
| 73 |
-
translations[lang_code] = {}
|
| 74 |
-
|
| 75 |
-
# Load main.json
|
| 76 |
-
main_file = os.path.join(lang_dir, "main.json")
|
| 77 |
-
node_translations = safe_load_json_file(main_file)
|
| 78 |
-
|
| 79 |
-
# Load extra locale files
|
| 80 |
-
for extra_file in EXTRA_LOCALE_FILES:
|
| 81 |
-
extra_file_path = os.path.join(lang_dir, extra_file)
|
| 82 |
-
key = extra_file.split(".")[0]
|
| 83 |
-
json_data = safe_load_json_file(extra_file_path)
|
| 84 |
-
if json_data:
|
| 85 |
-
node_translations[key] = json_data
|
| 86 |
-
|
| 87 |
-
if node_translations:
|
| 88 |
-
translations[lang_code] = merge_json_recursive(
|
| 89 |
-
translations[lang_code], node_translations
|
| 90 |
-
)
|
| 91 |
-
|
| 92 |
-
return translations
|
| 93 |
-
|
| 94 |
-
def add_routes(self, routes, webapp, loadedModules):
|
| 95 |
-
|
| 96 |
-
@routes.get("/workflow_templates")
|
| 97 |
-
async def get_workflow_templates(request):
|
| 98 |
-
"""Returns a web response that contains the map of custom_nodes names and their associated workflow templates. The ones without templates are omitted."""
|
| 99 |
-
files = [
|
| 100 |
-
file
|
| 101 |
-
for folder in folder_paths.get_folder_paths("custom_nodes")
|
| 102 |
-
for file in glob.glob(
|
| 103 |
-
os.path.join(folder, "*/example_workflows/*.json")
|
| 104 |
-
)
|
| 105 |
-
]
|
| 106 |
-
workflow_templates_dict = (
|
| 107 |
-
{}
|
| 108 |
-
) # custom_nodes folder name -> example workflow names
|
| 109 |
-
for file in files:
|
| 110 |
-
custom_nodes_name = os.path.basename(
|
| 111 |
-
os.path.dirname(os.path.dirname(file))
|
| 112 |
-
)
|
| 113 |
-
workflow_name = os.path.splitext(os.path.basename(file))[0]
|
| 114 |
-
workflow_templates_dict.setdefault(custom_nodes_name, []).append(
|
| 115 |
-
workflow_name
|
| 116 |
-
)
|
| 117 |
-
return web.json_response(workflow_templates_dict)
|
| 118 |
-
|
| 119 |
-
# Serve workflow templates from custom nodes.
|
| 120 |
-
for module_name, module_dir in loadedModules:
|
| 121 |
-
workflows_dir = os.path.join(module_dir, "example_workflows")
|
| 122 |
-
if os.path.exists(workflows_dir):
|
| 123 |
-
webapp.add_routes(
|
| 124 |
-
[
|
| 125 |
-
web.static(
|
| 126 |
-
"/api/workflow_templates/" + module_name, workflows_dir
|
| 127 |
-
)
|
| 128 |
-
]
|
| 129 |
-
)
|
| 130 |
-
|
| 131 |
-
@routes.get("/i18n")
|
| 132 |
-
async def get_i18n(request):
|
| 133 |
-
"""Returns translations from all custom nodes' locales folders."""
|
| 134 |
-
return web.json_response(self.build_translations())
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
import folder_paths
|
| 5 |
+
import glob
|
| 6 |
+
from aiohttp import web
|
| 7 |
+
import json
|
| 8 |
+
import logging
|
| 9 |
+
from functools import lru_cache
|
| 10 |
+
|
| 11 |
+
from ut.json_util import merge_json_recursive
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# Extra locale files to load into main.json
|
| 15 |
+
EXTRA_LOCALE_FILES = [
|
| 16 |
+
"nodeDefs.json",
|
| 17 |
+
"commands.json",
|
| 18 |
+
"settings.json",
|
| 19 |
+
]
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def safe_load_json_file(file_path: str) -> dict:
|
| 23 |
+
if not os.path.exists(file_path):
|
| 24 |
+
return {}
|
| 25 |
+
|
| 26 |
+
try:
|
| 27 |
+
with open(file_path, "r", encoding="utf-8") as f:
|
| 28 |
+
return json.load(f)
|
| 29 |
+
except json.JSONDecodeError:
|
| 30 |
+
logging.error(f"Error loading {file_path}")
|
| 31 |
+
return {}
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
class CustomNodeManager:
|
| 35 |
+
@lru_cache(maxsize=1)
|
| 36 |
+
def build_translations(self):
|
| 37 |
+
"""Load all custom nodes translations during initialization. Translations are
|
| 38 |
+
expected to be loaded from `locales/` folder.
|
| 39 |
+
|
| 40 |
+
The folder structure is expected to be the following:
|
| 41 |
+
- custom_nodes/
|
| 42 |
+
- custom_node_1/
|
| 43 |
+
- locales/
|
| 44 |
+
- en/
|
| 45 |
+
- main.json
|
| 46 |
+
- commands.json
|
| 47 |
+
- settings.json
|
| 48 |
+
|
| 49 |
+
returned translations are expected to be in the following format:
|
| 50 |
+
{
|
| 51 |
+
"en": {
|
| 52 |
+
"nodeDefs": {...},
|
| 53 |
+
"commands": {...},
|
| 54 |
+
"settings": {...},
|
| 55 |
+
...{other main.json keys}
|
| 56 |
+
}
|
| 57 |
+
}
|
| 58 |
+
"""
|
| 59 |
+
|
| 60 |
+
translations = {}
|
| 61 |
+
|
| 62 |
+
for folder in folder_paths.get_folder_paths("custom_nodes"):
|
| 63 |
+
# Sort glob results for deterministic ordering
|
| 64 |
+
for custom_node_dir in sorted(glob.glob(os.path.join(folder, "*/"))):
|
| 65 |
+
locales_dir = os.path.join(custom_node_dir, "locales")
|
| 66 |
+
if not os.path.exists(locales_dir):
|
| 67 |
+
continue
|
| 68 |
+
|
| 69 |
+
for lang_dir in glob.glob(os.path.join(locales_dir, "*/")):
|
| 70 |
+
lang_code = os.path.basename(os.path.dirname(lang_dir))
|
| 71 |
+
|
| 72 |
+
if lang_code not in translations:
|
| 73 |
+
translations[lang_code] = {}
|
| 74 |
+
|
| 75 |
+
# Load main.json
|
| 76 |
+
main_file = os.path.join(lang_dir, "main.json")
|
| 77 |
+
node_translations = safe_load_json_file(main_file)
|
| 78 |
+
|
| 79 |
+
# Load extra locale files
|
| 80 |
+
for extra_file in EXTRA_LOCALE_FILES:
|
| 81 |
+
extra_file_path = os.path.join(lang_dir, extra_file)
|
| 82 |
+
key = extra_file.split(".")[0]
|
| 83 |
+
json_data = safe_load_json_file(extra_file_path)
|
| 84 |
+
if json_data:
|
| 85 |
+
node_translations[key] = json_data
|
| 86 |
+
|
| 87 |
+
if node_translations:
|
| 88 |
+
translations[lang_code] = merge_json_recursive(
|
| 89 |
+
translations[lang_code], node_translations
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
return translations
|
| 93 |
+
|
| 94 |
+
def add_routes(self, routes, webapp, loadedModules):
|
| 95 |
+
|
| 96 |
+
@routes.get("/workflow_templates")
|
| 97 |
+
async def get_workflow_templates(request):
|
| 98 |
+
"""Returns a web response that contains the map of custom_nodes names and their associated workflow templates. The ones without templates are omitted."""
|
| 99 |
+
files = [
|
| 100 |
+
file
|
| 101 |
+
for folder in folder_paths.get_folder_paths("custom_nodes")
|
| 102 |
+
for file in glob.glob(
|
| 103 |
+
os.path.join(folder, "*/example_workflows/*.json")
|
| 104 |
+
)
|
| 105 |
+
]
|
| 106 |
+
workflow_templates_dict = (
|
| 107 |
+
{}
|
| 108 |
+
) # custom_nodes folder name -> example workflow names
|
| 109 |
+
for file in files:
|
| 110 |
+
custom_nodes_name = os.path.basename(
|
| 111 |
+
os.path.dirname(os.path.dirname(file))
|
| 112 |
+
)
|
| 113 |
+
workflow_name = os.path.splitext(os.path.basename(file))[0]
|
| 114 |
+
workflow_templates_dict.setdefault(custom_nodes_name, []).append(
|
| 115 |
+
workflow_name
|
| 116 |
+
)
|
| 117 |
+
return web.json_response(workflow_templates_dict)
|
| 118 |
+
|
| 119 |
+
# Serve workflow templates from custom nodes.
|
| 120 |
+
for module_name, module_dir in loadedModules:
|
| 121 |
+
workflows_dir = os.path.join(module_dir, "example_workflows")
|
| 122 |
+
if os.path.exists(workflows_dir):
|
| 123 |
+
webapp.add_routes(
|
| 124 |
+
[
|
| 125 |
+
web.static(
|
| 126 |
+
"/api/workflow_templates/" + module_name, workflows_dir
|
| 127 |
+
)
|
| 128 |
+
]
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
@routes.get("/i18n")
|
| 132 |
+
async def get_i18n(request):
|
| 133 |
+
"""Returns translations from all custom nodes' locales folders."""
|
| 134 |
+
return web.json_response(self.build_translations())
|