优化项目结构, 增加表格和文档插件
This commit is contained in:
1 parent
0d6e543a6f
commit
685813b443
30 files changed
+539
-125
No files matched your search
@@ -0,0 +1,213 @@
|
||||
import os
|
||||
import platform
|
||||
import xml.dom.minidom as minidom
|
||||
import sys
|
||||
import shutil
|
||||
import zipfile
|
||||
|
||||
|
||||
def get_install_directory():
|
||||
"""Get the correct installation directory based on the platform."""
|
||||
system = platform.system()
|
||||
if system == "Windows":
|
||||
return os.path.join(os.environ.get("APPDATA"), "kingsoft", "wps", "jsaddons")
|
||||
elif system == "Linux":
|
||||
return os.path.join(
|
||||
os.path.expanduser("~"), ".local", "share", "Kingsoft", "wps", "jsaddons"
|
||||
)
|
||||
else:
|
||||
print(f"Unsupported platform: {system}")
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def create_authwebsite_xml(directory, node_name="xbotwps"):
|
||||
"""Create the authwebsite.xml file if it doesn't exist or update it if it exists."""
|
||||
file_path = os.path.join(directory, "authwebsite.xml")
|
||||
|
||||
# Create the basic XML structure if the file doesn't exist
|
||||
if not os.path.exists(file_path):
|
||||
# Create base XML structure
|
||||
content = '<?xml version="1.0" encoding="UTF-8"?>\n<websites>\n</websites>\n'
|
||||
with open(file_path, "w", encoding="utf-8") as f:
|
||||
f.write(content)
|
||||
print(f"Created basic authwebsite.xml structure at {file_path}")
|
||||
|
||||
try:
|
||||
# Parse the XML file
|
||||
dom = minidom.parse()
|
||||
|
||||
# Get the websites element
|
||||
websites_elements = dom.getElementsByTagName("websites")
|
||||
if not websites_elements:
|
||||
# If no websites element exists, create it
|
||||
websites = dom.createElement("websites")
|
||||
dom.appendChild(websites)
|
||||
else:
|
||||
websites = websites_elements[0]
|
||||
|
||||
# Check if a website node with the specified name already exists
|
||||
existing_nodes = []
|
||||
for website in websites.getElementsByTagName("website"):
|
||||
if website.getAttribute("name") == node_name:
|
||||
existing_nodes.append(website)
|
||||
|
||||
# Remove existing nodes with the same name
|
||||
for node in existing_nodes:
|
||||
websites.removeChild(node)
|
||||
|
||||
# Create and append the new website node
|
||||
new_website = dom.createElement("website")
|
||||
new_website.setAttribute("origin", "null")
|
||||
new_website.setAttribute("status", "enable")
|
||||
new_website.setAttribute("name", node_name)
|
||||
websites.appendChild(new_website)
|
||||
|
||||
# Save the updated XML file
|
||||
with open(file_path, "w", encoding="utf-8") as f:
|
||||
f.write(dom.toprettyxml(indent=" "))
|
||||
|
||||
print(f"Updated authwebsite.xml at {file_path}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error working with authwebsite.xml: {e}")
|
||||
# Create a completely new file with the correct structure and node
|
||||
content = (
|
||||
'<?xml version="1.0" encoding="UTF-8"?>\n'
|
||||
"<websites>\n"
|
||||
f' <website origin="null" status="enable" name="{node_name}"/>\n'
|
||||
"</websites>\n"
|
||||
)
|
||||
with open(file_path, "w", encoding="utf-8") as f:
|
||||
f.write(content)
|
||||
print(f"Created new authwebsite.xml at {file_path}")
|
||||
|
||||
|
||||
def create_publish_xml(
|
||||
directory, node_name="xbotwps", addon_path=None, verson="1.0.0", addon_type="wps"
|
||||
):
|
||||
"""Create the publish.xml file if it doesn't exist or update it if it exists."""
|
||||
file_path = os.path.join(directory, "publish.xml")
|
||||
if addon_path is None:
|
||||
raise ValueError("addon_path must be provided")
|
||||
|
||||
# Create the basic XML structure if the file doesn't exist
|
||||
if not os.path.exists(file_path):
|
||||
# Create base XML structure
|
||||
content = '<?xml version="1.0" encoding="UTF-8"?>\n<jsplugins>\n</jsplugins>\n'
|
||||
with open(file_path, "w", encoding="utf-8") as f:
|
||||
f.write(content)
|
||||
print(f"Created basic publish.xml structure at {file_path}")
|
||||
|
||||
try:
|
||||
# Parse the XML file
|
||||
dom = minidom.parse(file_path)
|
||||
|
||||
# Get the jsplugins element
|
||||
jsplugins_elements = dom.getElementsByTagName("jsplugins")
|
||||
if not jsplugins_elements:
|
||||
# If no jsplugins element exists, create it
|
||||
jsplugins = dom.createElement("jsplugins")
|
||||
dom.appendChild(jsplugins)
|
||||
else:
|
||||
jsplugins = jsplugins_elements[0]
|
||||
|
||||
# Check if a jsplugin node with the specified name already exists
|
||||
existing_nodes = []
|
||||
for jsplugin in jsplugins.getElementsByTagName("jsplugin"):
|
||||
if jsplugin.getAttribute("name") == node_name:
|
||||
existing_nodes.append(jsplugin)
|
||||
|
||||
# Remove existing nodes with the same name
|
||||
for node in existing_nodes:
|
||||
jsplugins.removeChild(node)
|
||||
|
||||
# Create and append the new jsplugin node
|
||||
new_jsplugin = dom.createElement("jsplugin")
|
||||
new_jsplugin.setAttribute("enable", "enable_dev")
|
||||
new_jsplugin.setAttribute("url", addon_path)
|
||||
new_jsplugin.setAttribute("install", "null")
|
||||
new_jsplugin.setAttribute("version", verson)
|
||||
new_jsplugin.setAttribute("type", addon_type)
|
||||
new_jsplugin.setAttribute("customDomain", "")
|
||||
new_jsplugin.setAttribute("name", node_name)
|
||||
jsplugins.appendChild(new_jsplugin)
|
||||
|
||||
# Save the updated XML file
|
||||
with open(file_path, "w", encoding="utf-8") as f:
|
||||
f.write(dom.toprettyxml(indent=" "))
|
||||
|
||||
print(f"Updated publish.xml at {file_path}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error working with publish.xml: {e}")
|
||||
# Create a completely new file with the correct structure and node
|
||||
content = (
|
||||
'<?xml version="1.0" encoding="UTF-8"?>\n'
|
||||
"<jsplugins>\n"
|
||||
f' <jsplugin enable="enable_dev" url="{addon_path}" install="null" version="1.0.0" type="wps" customDomain="" name="{node_name}"/>\n'
|
||||
"</jsplugins>\n"
|
||||
)
|
||||
with open(file_path, "w", encoding="utf-8") as f:
|
||||
f.write(content)
|
||||
print(f"Created new publish.xml at {file_path}")
|
||||
|
||||
|
||||
def extract_addon(addon_path, install_dir):
|
||||
"""Extract the addon archive to the installation directory."""
|
||||
addon_name = "xbotwps"
|
||||
extract_dir = os.path.join(install_dir, addon_name)
|
||||
|
||||
# Clean the existing directory if it exists
|
||||
if os.path.exists(extract_dir):
|
||||
print(f"Removing existing addon directory: {extract_dir}")
|
||||
shutil.rmtree(extract_dir)
|
||||
|
||||
# Create the extraction directory
|
||||
os.makedirs(extract_dir)
|
||||
|
||||
# Determine the archive type and extract accordingly
|
||||
file_ext = os.path.splitext(addon_path)[1].lower()
|
||||
|
||||
try:
|
||||
print(f"Extracting ZIP archive to {extract_dir}")
|
||||
with zipfile.ZipFile(addon_path, "r") as zip_ref:
|
||||
zip_ref.extractall(install_dir)
|
||||
print("Extraction completed successfully")
|
||||
|
||||
return True
|
||||
except Exception as e:
|
||||
print(f"Error extracting archive: {e}")
|
||||
return False
|
||||
|
||||
|
||||
def main():
|
||||
# 1. 将插件文件解压到插件安装目录下
|
||||
# 2. 从插件目录下读取package.json文件,获取插件名称, 版本号, 插件类型
|
||||
# 3. 创建authwebsite.xml文件,添加插件名称节点
|
||||
# 4. 创建publish.xml文件,添加插件名称节点
|
||||
|
||||
addon_path = r"C:\\Users\\32075\\OneDrive\\文档\\Code\\RPA_Script\\wps-js-extension\\XbotWps\\wps-addon-build\\xbotwps.zip"
|
||||
node_name = "xbotwps"
|
||||
install_dir = get_install_directory()
|
||||
print(f"Installation directory: {install_dir}")
|
||||
|
||||
# Create the installation directory if it doesn't exist
|
||||
if not os.path.exists(install_dir):
|
||||
os.makedirs(install_dir)
|
||||
print(f"Created installation directory: {install_dir}")
|
||||
|
||||
# Create the required XML files
|
||||
create_authwebsite_xml(install_dir, node_name)
|
||||
create_publish_xml(install_dir, node_name, addon_path)
|
||||
|
||||
# Extract the addon to the installation directory
|
||||
if extract_addon(addon_path, install_dir):
|
||||
print("Addon extracted successfully.")
|
||||
else:
|
||||
print("Failed to extract addon.")
|
||||
|
||||
print("Installation completed successfully.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in new issue
Block a user