Browse Source

refactor: cm_api -> cm_global.APIs

pull/284/head
dr.lt.data 11 months ago
parent
commit
097a832e7e
  1. 33
      __init__.py
  2. 112
      glob/cm_global.py
  3. 4
      js/node_fixer.js
  4. 14
      prestartup_script.py

33
__init__.py

@ -1,6 +1,8 @@
import configparser
import mimetypes
import shutil
import traceback
import folder_paths
import os
import sys
@ -18,9 +20,10 @@ import re
import signal
import nodes
import torch
import cm_global
version = [1, 18]
version = [1, 19]
version_str = f"V{version[0]}.{version[1]}" + (f'.{version[2]}' if len(version) > 2 else '')
print(f"### Loading: ComfyUI-Manager ({version_str})")
@ -283,6 +286,8 @@ def print_comfyui_version():
current_branch = repo.active_branch.name
comfy_ui_hash = repo.head.commit.hexsha
cm_global.variables['comfyui.revision'] = comfy_ui_revision
try:
if int(comfy_ui_revision) < comfy_ui_required_revision:
print(f"\n\n## [WARN] ComfyUI-Manager: Your ComfyUI version ({comfy_ui_revision}) is too old. Please update to the latest version. ##\n\n")
@ -290,6 +295,18 @@ def print_comfyui_version():
pass
comfy_ui_commit_date = repo.head.commit.committed_datetime.date()
# process on_revision_detected -->
for k, f in cm_global.variables['cm.on_revision_detected_handler']:
try:
f(comfy_ui_revision)
except Exception:
print(f"[ERROR] '{k}' on_revision_detected_handler")
traceback.print_exc()
del cm_global.variables['cm.on_revision_detected_handler']
# <--
if current_branch == "master":
print(f"### ComfyUI Revision: {comfy_ui_revision} [{comfy_ui_hash[:8]}] | Released on '{comfy_ui_commit_date}'")
else:
@ -1987,11 +2004,6 @@ async def share_art(request):
}, content_type='application/json', status=200)
def register_api(k, f):
sys.CM_api[k] = f
def sanitize(data):
return data.replace("<", "&lt;").replace(">", "&gt;")
@ -2027,7 +2039,8 @@ async def _confirm_try_install(sender, custom_node_url, msg):
def confirm_try_install(sender, custom_node_url, msg):
asyncio.run(_confirm_try_install(sender, custom_node_url, msg))
register_api('cm.try-install-custom-node', confirm_try_install)
cm_global.register_api('cm.try-install-custom-node', confirm_try_install)
import asyncio
@ -2058,3 +2071,9 @@ WEB_DIRECTORY = "js"
NODE_CLASS_MAPPINGS = {}
__all__ = ['NODE_CLASS_MAPPINGS']
cm_global.register_extension('ComfyUI-Manager',
{'version': version,
'name': 'ComfyUI Manager',
'nodes': {'Terminal Log //CM'},
'description': 'It provides the ability to manage custom nodes in ComfyUI.', })

112
glob/cm_global.py

@ -0,0 +1,112 @@
import traceback
#
# Global Var
#
# Usage:
# import cm_global
# cm_global.variables['comfyui.revision'] = 1832
# print(f"log mode: {cm_global.variables['logger.enabled']}")
#
variables = {}
#
# Global API
#
# Usage:
# [register API]
# import cm_global
#
# def api_hello(msg):
# print(f"hello: {msg}")
# return msg
#
# cm_global.register_api('hello', api_hello)
#
# [use API]
# import cm_global
#
# test = cm_global.try_call(api='hello', msg='an example')
# print(f"'{test}' is returned")
#
APIs = {}
def register_api(k, f):
global APIs
APIs[k] = f
def try_call(**kwargs):
if 'api' in kwargs:
api_name = kwargs['api']
try:
api = APIs.get(api_name)
if api is not None:
del kwargs['api']
return api(**kwargs)
else:
print(f"WARN: The '{kwargs['api']}' API has not been registered.")
except Exception as e:
print(f"ERROR: An exception occurred while calling the '{api_name}' API.")
raise e
else:
return None
#
# Extension Info
#
# Usage:
# import cm_global
#
# cm_global.extension_infos['my_extension'] = {'version': [0, 1], 'name': 'me', 'description': 'example extension', }
#
extension_infos = {}
on_extension_registered_handlers = {}
def register_extension(extension_name, v):
global extension_infos
global on_extension_registered_handlers
extension_infos[extension_name] = v
if extension_name in on_extension_registered_handlers:
for k, f in on_extension_registered_handlers[extension_name]:
try:
f(extension_name, v)
except Exception:
print(f"[ERROR] '{k}' on_extension_registered_handlers")
traceback.print_exc()
del on_extension_registered_handlers[extension_name]
def add_on_extension_registered(k, extension_name, f):
global on_extension_registered_handlers
if extension_name in extension_infos:
try:
v = extension_infos[extension_name]
f(extension_name, v)
except Exception:
print(f"[ERROR] '{k}' on_extension_registered_handler")
traceback.print_exc()
else:
if extension_name not in on_extension_registered_handlers:
on_extension_registered_handlers[extension_name] = []
on_extension_registered_handlers[extension_name].append((k, f))
def add_on_revision_detected(k, f):
if 'comfyui.revision' in variables:
try:
f(variables['comfyui.revision'])
except Exception:
print(f"[ERROR] '{k}' on_revision_detected_handler")
traceback.print_exc()
else:
variables['cm.on_revision_detected_handler'].append((k, f))

4
js/node_fixer.js

@ -65,9 +65,5 @@ app.registerExtension({
},
});
});
},
nodeCreated(node, app) {
console.log('test');
}
});

14
prestartup_script.py

@ -7,13 +7,18 @@ import threading
import re
import locale
import platform
import traceback
sys.CM_api = {}
glob_path = os.path.join(os.path.dirname(__file__), "glob")
sys.path.append(glob_path)
import cm_global
message_collapses = []
import_failed_extensions = set()
cm_global.variables['cm.on_revision_detected_handler'] = []
def register_message_collapse(f):
@ -21,13 +26,16 @@ def register_message_collapse(f):
message_collapses.append(f)
def is_import_failed_extension(x):
def is_import_failed_extension(name):
global import_failed_extensions
return x in import_failed_extensions
return name in import_failed_extensions
sys.__comfyui_manager_register_message_collapse = register_message_collapse
sys.__comfyui_manager_is_import_failed_extension = is_import_failed_extension
cm_global.register_api('cm.register_message_collapse', register_message_collapse)
cm_global.register_api('cm.is_import_failed_extension', is_import_failed_extension)
comfyui_manager_path = os.path.dirname(__file__)
custom_nodes_path = os.path.abspath(os.path.join(comfyui_manager_path, ".."))

Loading…
Cancel
Save