161 lines
5.5 KiB
Python
161 lines
5.5 KiB
Python
import os
|
|
import shutil
|
|
|
|
annotating_js_files = [
|
|
"../efficiency-nodes-comfyui/js/node_options/addLinks.js",
|
|
"../efficiency-nodes-comfyui/js/node_options/addScripts.js",
|
|
"../efficiency-nodes-comfyui/js/node_options/addXYinputs.js",
|
|
"../efficiency-nodes-comfyui/js/node_options/modelInfo.js",
|
|
"../efficiency-nodes-comfyui/js/node_options/setResolution.js",
|
|
"../efficiency-nodes-comfyui/js/node_options/swapLoaders.js",
|
|
"../efficiency-nodes-comfyui/js/node_options/swapSamplers.js",
|
|
"../efficiency-nodes-comfyui/js/node_options/swapScripts.js",
|
|
"../efficiency-nodes-comfyui/js/node_options/swapXYinputs.js",
|
|
"../efficiency-nodes-comfyui/js/node_options/common/modelInfoDialog.js",
|
|
]
|
|
|
|
impact_wildcard_py = "../ComfyUI-Impact-Pack/modules/impact/wildcards.py"
|
|
widgethider_js = "../efficiency-nodes-comfyui/js/widgethider.js"
|
|
new_widgethider_js = "./user_css/widgethider.js"
|
|
|
|
read_css_folder = "./user_css/"
|
|
write_css_folder = "../../../python_embeded/Lib/site-packages/comfyui_frontend_package/static/"
|
|
|
|
efficeincy_nodes_py = "../efficiency-nodes-comfyui/efficiency_nodes.py"
|
|
efficeincy_nodes_target = '"ksampler_output_image": (["Images","Plot"],),},'
|
|
efficeincy_nodes_replacement = '"ksampler_output_image": (["Images","Plot"], {"default": "Plot"}),},'
|
|
|
|
def is_file_exist(filepath):
|
|
if not os.path.isfile(filepath):
|
|
print(f"File does not exist: {filepath}")
|
|
return False
|
|
return True
|
|
|
|
def annotate_file(js_file):
|
|
if not is_file_exist(js_file):
|
|
return
|
|
|
|
contents = []
|
|
modified = False
|
|
if os.path.isfile(js_file):
|
|
with open(js_file, 'r', encoding='UTF8') as f:
|
|
contents = f.readlines()
|
|
with open(js_file, 'w', encoding='UTF8') as f:
|
|
for c in contents:
|
|
if c[:5] == "//** ":
|
|
f.write(c)
|
|
else:
|
|
f.write("//** "+c)
|
|
modified = True
|
|
|
|
if modified:
|
|
print(f"File has been modified: {js_file}")
|
|
|
|
|
|
def restore_annotate_file(js_file):
|
|
if not is_file_exist(js_file):
|
|
return
|
|
|
|
contents = []
|
|
modified = False
|
|
if os.path.isfile(js_file):
|
|
with open(js_file, 'r', encoding='UTF8') as f:
|
|
contents = f.readlines()
|
|
with open(js_file, 'w', encoding='UTF8') as f:
|
|
for c in contents:
|
|
if c[:5] == "//** ":
|
|
f.write(c[5:])
|
|
modified = True
|
|
else:
|
|
f.write(c)
|
|
|
|
if modified:
|
|
print(f"File has been modified: {js_file}")
|
|
|
|
def annotate_wildcard(py_file):
|
|
if not is_file_exist(py_file):
|
|
return
|
|
|
|
contents = []
|
|
modified = False
|
|
if os.path.isfile(py_file):
|
|
with open(py_file, 'r', encoding='UTF8') as f:
|
|
contents = f.readlines()
|
|
with open(py_file, 'w', encoding='UTF8') as f:
|
|
for c in contents:
|
|
if 'print(f"CLIP: {str.join(' in c and not "## from ED ##" in c:
|
|
f.write("## from ED ##" + c)
|
|
modified = True
|
|
else:
|
|
f.write(c)
|
|
|
|
if modified:
|
|
print(f"File has been modified: {js_file}")
|
|
|
|
|
|
|
|
def replace_py_file(py_file, target_line, replacement_line):
|
|
if not is_file_exist(py_file):
|
|
return
|
|
|
|
with open(py_file, 'r', encoding='utf-8') as file:
|
|
lines = file.readlines()
|
|
|
|
modified = False
|
|
new_lines = []
|
|
|
|
for line in lines:
|
|
stripped = line.strip()
|
|
if stripped == target_line:
|
|
indent = line[:line.index(target_line)]
|
|
new_lines.append(indent + replacement_line+"\n")
|
|
modified = True
|
|
else:
|
|
new_lines.append(line)
|
|
|
|
if modified:
|
|
with open(py_file, 'w', encoding='utf-8') as file:
|
|
file.writelines(new_lines)
|
|
print(f"File has been modified: {py_file}")
|
|
|
|
def widgethider_js_copy():
|
|
shutil.copy2(new_widgethider_js, widgethider_js)
|
|
print(f"File copied: {widgethider_js}")
|
|
|
|
def copy_user_css():
|
|
if os.path.isdir(write_css_folder):
|
|
if os.path.isfile(write_css_folder + "user.css"):
|
|
os.remove(write_css_folder + "user.css")
|
|
shutil.copy2(read_css_folder + "user.css", write_css_folder + "user.css")
|
|
print(f"File copied: {write_css_folder + "user.css"}")
|
|
else:
|
|
print(f"Directory does not exist - {write_css_folder}")
|
|
|
|
def restore_user_css():
|
|
if os.path.isdir(write_css_folder):
|
|
if os.path.isfile(write_css_folder + "user.css"):
|
|
os.remove(write_css_folder + "user.css")
|
|
if os.path.isfile(write_css_folder + "user.css.old"):
|
|
os.rename(write_css_folder + "user.css.old", write_css_folder + "user.css")
|
|
else:
|
|
with open(write_css_folder + "user.css", 'w', encoding='UTF8') as f:
|
|
f.write("/* Put custom styles here */")
|
|
|
|
|
|
|
|
|
|
try:
|
|
printout = "copy user.css and disable unnecessary js files"
|
|
print ("\n")
|
|
widgethider_js_copy()
|
|
copy_user_css()
|
|
for file in annotating_js_files:
|
|
annotate_file(file)
|
|
annotate_wildcard(impact_wildcard_py)
|
|
replace_py_file(efficeincy_nodes_py, efficeincy_nodes_target, efficeincy_nodes_replacement)
|
|
print(f"\n\nEfficiency Nodes ED: Attempting to {printout} success!\n\n")
|
|
|
|
except Exception as e:
|
|
print(f"\n\n\n[ERROR] efficiency nodes ED: An error occurred while annotating the file.\n{e}")
|
|
|