mirror of
https://github.com/dw-0/kiauh.git
synced 2026-03-23 02:48:35 +05:00
fix(extensions_menu): prevent extension index collisions during loading (#783)
* fix(extensions_menu): prevent extension index collisions during loading * feat(extensions_menu): add GITHUB_ISSUES_URL for reporting extension loading issues
This commit is contained in:
@@ -10,3 +10,4 @@
|
||||
from pathlib import Path
|
||||
|
||||
EXTENSION_ROOT = Path(__file__).resolve().parents[1].joinpath("extensions")
|
||||
GITHUB_ISSUES_URL = "https://github.com/dw-0/kiauh/issues"
|
||||
|
||||
@@ -19,7 +19,7 @@ from core.logger import Logger
|
||||
from core.menus import Option
|
||||
from core.menus.base_menu import BaseMenu
|
||||
from core.types.color import Color
|
||||
from extensions import EXTENSION_ROOT
|
||||
from extensions import EXTENSION_ROOT, GITHUB_ISSUES_URL
|
||||
from extensions.base_extension import BaseExtension
|
||||
|
||||
|
||||
@@ -56,6 +56,22 @@ class ExtensionsMenu(BaseMenu):
|
||||
with open(metadata_json, "r") as m:
|
||||
# read extension metadata from json
|
||||
metadata = json.load(m).get("metadata")
|
||||
index = str(metadata.get("index"))
|
||||
|
||||
# Prevent collisions where one extension silently overrides another.
|
||||
if index in ext_dict:
|
||||
existing_name = ext_dict[index].metadata.get("display_name")
|
||||
duplicate_name = metadata.get("display_name")
|
||||
Logger.print_warn(
|
||||
"Failed loading extension"
|
||||
f" {ext}: duplicate index '{index}'"
|
||||
f" already used by '{existing_name}'."
|
||||
f" Skipping '{duplicate_name}'."
|
||||
f" Please report this at {GITHUB_ISSUES_URL}."
|
||||
)
|
||||
continue
|
||||
|
||||
int(index)
|
||||
module_name = metadata.get("module")
|
||||
module_path = f"kiauh.extensions.{ext.name}.{module_name}"
|
||||
|
||||
@@ -73,10 +89,20 @@ class ExtensionsMenu(BaseMenu):
|
||||
|
||||
# instantiate the extension with its metadata and add to dict
|
||||
ext_instance: BaseExtension = ext_class(metadata)
|
||||
ext_dict[f"{metadata.get('index')}"] = ext_instance
|
||||
ext_dict[index] = ext_instance
|
||||
|
||||
except (IOError, json.JSONDecodeError, ImportError) as e:
|
||||
print(f"Failed loading extension {ext}: {e}")
|
||||
except (
|
||||
IOError,
|
||||
json.JSONDecodeError,
|
||||
ImportError,
|
||||
TypeError,
|
||||
ValueError,
|
||||
AttributeError,
|
||||
) as e:
|
||||
Logger.print_warn(
|
||||
f"Failed loading extension {ext}: {e}. "
|
||||
f"Please report this at {GITHUB_ISSUES_URL}."
|
||||
)
|
||||
|
||||
return dict(sorted(ext_dict.items(), key=lambda x: int(x[0])))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user