Add underneath logic for adding 3rd party plugins

This commit is contained in:
Rikko 2022-08-10 02:00:03 +05:30
parent 4f8180d06d
commit 256fc8a23b

View file

@ -9,6 +9,7 @@ import json
import os import os
import asyncio import asyncio
import re import re
import string
from typing import Union, Optional from typing import Union, Optional
@ -29,6 +30,8 @@ REGEXP = {
VERSION = "0.1.1" VERSION = "0.1.1"
GITHUB_REPO_LINK = "https://github.com/bombsquad-community/plugin-manager/" GITHUB_REPO_LINK = "https://github.com/bombsquad-community/plugin-manager/"
THIRD_PARTY_CATEGORY_URL = "https://github.com/{repository}/{content_type}/main/category.json"
_CACHE = {} _CACHE = {}
@ -39,6 +42,9 @@ def setup_config():
if "Installed Plugins" not in ba.app.config["Community Plugin Manager"]: if "Installed Plugins" not in ba.app.config["Community Plugin Manager"]:
ba.app.config["Community Plugin Manager"]["Installed Plugins"] = {} ba.app.config["Community Plugin Manager"]["Installed Plugins"] = {}
is_config_updated = True is_config_updated = True
if "Custom Categories" not in ba.app.config["Community Plugin Manager"]:
ba.app.config["Community Plugin Manager"]["Custom Categories"] = []
is_config_updated = True
for plugin_name in ba.app.config["Community Plugin Manager"]["Installed Plugins"].keys(): for plugin_name in ba.app.config["Community Plugin Manager"]["Installed Plugins"].keys():
plugin = PluginLocal(plugin_name) plugin = PluginLocal(plugin_name)
if not plugin.is_installed: if not plugin.is_installed:
@ -83,8 +89,9 @@ def play_sound():
class Category: class Category:
def __init__(self, meta_url): def __init__(self, meta_url, is_3rd_party=False):
self.meta_url = meta_url self.meta_url = meta_url
self.is_3rd_party = is_3rd_party
self.request_headers = HEADERS self.request_headers = HEADERS
self._metadata = _CACHE.get("categories", {}).get(meta_url, {}).get("metadata") self._metadata = _CACHE.get("categories", {}).get(meta_url, {}).get("metadata")
self._plugins = _CACHE.get("categories", {}).get(meta_url, {}).get("plugins") self._plugins = _CACHE.get("categories", {}).get(meta_url, {}).get("plugins")
@ -120,7 +127,11 @@ class Category:
if self._metadata is None: if self._metadata is None:
await self.fetch_metadata() await self.fetch_metadata()
self._plugins = ([ self._plugins = ([
Plugin(plugin_info, f"{await self.get_plugins_base_url()}/{plugin_info[0]}.py") Plugin(
plugin_info,
f"{await self.get_plugins_base_url()}/{plugin_info[0]}.py",
is_3rd_party=self.is_3rd_party,
)
for plugin_info in self._metadata["plugins"].items() for plugin_info in self._metadata["plugins"].items()
]) ])
self.set_category_global_cache("plugins", self._plugins) self.set_category_global_cache("plugins", self._plugins)
@ -195,7 +206,7 @@ class PluginLocal:
except KeyError: except KeyError:
pass pass
else: else:
ba.app.config.commit() self.save()
@property @property
def version(self): def version(self):
@ -273,6 +284,7 @@ class PluginLocal:
ba.app.config["Plugins"][entry_point] = {} ba.app.config["Plugins"][entry_point] = {}
ba.app.config["Plugins"][entry_point]["enabled"] = True ba.app.config["Plugins"][entry_point]["enabled"] = True
# await self._set_status(to_enable=True) # await self._set_status(to_enable=True)
self.save()
ba.screenmessage("Plugin Enabled") ba.screenmessage("Plugin Enabled")
async def disable(self): async def disable(self):
@ -283,6 +295,7 @@ class PluginLocal:
# XXX: The below logic is more accurate but less efficient, since it actually # XXX: The below logic is more accurate but less efficient, since it actually
# reads the local plugin file and parses entry points from it. # reads the local plugin file and parses entry points from it.
# await self._set_status(to_enable=False) # await self._set_status(to_enable=False)
self.save()
ba.screenmessage("Plugin Disabled") ba.screenmessage("Plugin Disabled")
def set_version(self, version): def set_version(self, version):
@ -316,11 +329,12 @@ class PluginVersion:
class Plugin: class Plugin:
def __init__(self, plugin, url): def __init__(self, plugin, url, is_3rd_party=False):
""" """
Initialize a plugin from network repository. Initialize a plugin from network repository.
""" """
self.name, self.info = plugin self.name, self.info = plugin
self.is_3rd_party = is_3rd_party
self.install_path = os.path.join(PLUGIN_DIRECTORY, f"{self.name}.py") self.install_path = os.path.join(PLUGIN_DIRECTORY, f"{self.name}.py")
self.download_url = url.format(content_type="raw") self.download_url = url.format(content_type="raw")
self.view_url = url.format(content_type="blob") self.view_url = url.format(content_type="blob")
@ -587,6 +601,14 @@ class PluginManager:
category = Category(plugin_category_url) category = Category(plugin_category_url)
request = category.fetch_metadata() request = category.fetch_metadata()
requests.append(request) requests.append(request)
for repository in ba.app.config["Community Plugin Manager"]["Custom Categories"]:
plugin_category_url = THIRD_PARTY_CATEGORY_URL.replace("{repository}", "$repository")
plugin_category_url = string.Template(plugin_category_url).safe_substitute({
"repository": repository,
})
category = Category(plugin_category_url, is_3rd_party=True)
request = category.fetch_metadata()
requests.append(request)
categories = await asyncio.gather(*requests) categories = await asyncio.gather(*requests)
all_plugins = [] all_plugins = []