forked from nathanwoodburn/firewalletbrowser
feat: Add custom plugins
This commit is contained in:
64
plugin.py
64
plugin.py
@@ -3,10 +3,12 @@ import json
|
||||
import importlib
|
||||
import sys
|
||||
import hashlib
|
||||
import subprocess
|
||||
|
||||
|
||||
def listPlugins():
|
||||
plugins = []
|
||||
customPlugins = []
|
||||
for file in os.listdir("plugins"):
|
||||
if file.endswith(".py"):
|
||||
if file != "main.py":
|
||||
@@ -14,9 +16,40 @@ def listPlugins():
|
||||
if "info" not in dir(plugin):
|
||||
continue
|
||||
details = plugin.info
|
||||
details["link"] = file[:-3]
|
||||
details["source"] = "built-in"
|
||||
details["link"] = f"plugins/{file[:-3]}"
|
||||
plugins.append(details)
|
||||
|
||||
# Check for imported plugins
|
||||
if not os.path.exists("user_data/plugins.json"):
|
||||
with open("user_data/plugins.json", "w") as f:
|
||||
json.dump([], f)
|
||||
|
||||
with open("user_data/plugins.json", "r") as f:
|
||||
importurls = json.load(f)
|
||||
|
||||
for importurl in importurls:
|
||||
# Get only repo name
|
||||
importPath = importurl.split("/")[-1].removesuffix(".git")
|
||||
|
||||
# Git clone into customPlugins/<importPath>
|
||||
if not os.path.exists(f"customPlugins/{importPath}"):
|
||||
os.system(f"git clone {importurl} customPlugins/{importPath}")
|
||||
else:
|
||||
os.system(f"cd customPlugins/{importPath} && git pull")
|
||||
|
||||
# Import plugins from customPlugins/<importPath>
|
||||
for file in os.listdir(f"customPlugins/{importPath}"):
|
||||
if file.endswith(".py"):
|
||||
if file != "main.py":
|
||||
plugin = importlib.import_module(f"customPlugins.{importPath}."+file[:-3])
|
||||
if "info" not in dir(plugin):
|
||||
continue
|
||||
details = plugin.info
|
||||
details["source"] = importPath
|
||||
details["link"] = f"customPlugins/{importPath}/{file[:-3]}"
|
||||
plugins.append(details)
|
||||
|
||||
# Verify plugin signature
|
||||
signatures = []
|
||||
try:
|
||||
@@ -39,10 +72,7 @@ def listPlugins():
|
||||
|
||||
|
||||
def pluginExists(plugin: str):
|
||||
for file in os.listdir("plugins"):
|
||||
if file == plugin+".py":
|
||||
return True
|
||||
return False
|
||||
return os.path.exists(plugin+".py")
|
||||
|
||||
|
||||
def verifyPlugin(plugin: str):
|
||||
@@ -66,7 +96,7 @@ def verifyPlugin(plugin: str):
|
||||
def hashPlugin(plugin: str):
|
||||
BUF_SIZE = 65536
|
||||
sha256 = hashlib.sha256()
|
||||
with open("plugins/"+plugin+".py", 'rb') as f:
|
||||
with open(plugin+".py", 'rb') as f:
|
||||
while True:
|
||||
data = f.read(BUF_SIZE)
|
||||
if not data:
|
||||
@@ -76,7 +106,7 @@ def hashPlugin(plugin: str):
|
||||
|
||||
|
||||
def getPluginData(pluginStr: str):
|
||||
plugin = importlib.import_module("plugins."+pluginStr)
|
||||
plugin = importlib.import_module(pluginStr.replace("/","."))
|
||||
|
||||
# Check if the plugin is verified
|
||||
signatures = []
|
||||
@@ -89,6 +119,18 @@ def getPluginData(pluginStr: str):
|
||||
json.dump(signatures, f)
|
||||
|
||||
info = plugin.info
|
||||
info["source"] = "built-in"
|
||||
|
||||
# Check if the plugin is in customPlugins
|
||||
if pluginStr.startswith("customPlugins"):
|
||||
# Get git url for dir
|
||||
print(f"cd customPlugins/{pluginStr.split('/')[-2]} && git remote get-url origin")
|
||||
url = subprocess.check_output(f"cd customPlugins/{pluginStr.split('/')[-2]} && git remote get-url origin", shell=True).decode("utf-8").strip()
|
||||
info["source"] = url
|
||||
|
||||
|
||||
|
||||
|
||||
# Hash the plugin file
|
||||
pluginHash = hashPlugin(pluginStr)
|
||||
if pluginHash not in signatures:
|
||||
@@ -100,12 +142,12 @@ def getPluginData(pluginStr: str):
|
||||
|
||||
|
||||
def getPluginFunctions(plugin: str):
|
||||
plugin = importlib.import_module("plugins."+plugin)
|
||||
plugin = importlib.import_module(plugin.replace("/","."))
|
||||
return plugin.functions
|
||||
|
||||
|
||||
def runPluginFunction(plugin: str, function: str, params: dict, authentication: str):
|
||||
plugin_module = importlib.import_module("plugins."+plugin)
|
||||
plugin_module = importlib.import_module(plugin.replace("/","."))
|
||||
if function not in plugin_module.functions:
|
||||
return {"error": "Function not found"}
|
||||
|
||||
@@ -141,12 +183,12 @@ def runPluginFunction(plugin: str, function: str, params: dict, authentication:
|
||||
|
||||
|
||||
def getPluginFunctionInputs(plugin: str, function: str):
|
||||
plugin = importlib.import_module("plugins."+plugin)
|
||||
plugin = importlib.import_module(plugin.replace("/","."))
|
||||
return plugin.functions[function]["params"]
|
||||
|
||||
|
||||
def getPluginFunctionReturns(plugin: str, function: str):
|
||||
plugin = importlib.import_module("plugins."+plugin)
|
||||
plugin = importlib.import_module(plugin.replace("/","."))
|
||||
return plugin.functions[function]["returns"]
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user