-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add plugin download system in
backend
- Loading branch information
Showing
7 changed files
with
147 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from typing import Optional | ||
|
||
import importlib | ||
import logging | ||
|
||
from plugins.base_plugin import Plugin | ||
from plugins import priority | ||
|
||
|
||
def load_all() -> None: | ||
for plugin_name, prio in priority.fetch_plugins(): | ||
logging.info(f'Loading plugin "{plugin_name}" with priority {prio}') | ||
plugin = load(plugin_name) | ||
if plugin is not None: | ||
plugin.load() | ||
|
||
|
||
def load(name: str) -> Optional[Plugin]: | ||
try: | ||
source = f'plugins.plugins.{name}.backend.main' | ||
plugin: Plugin = importlib.import_module(source).init() | ||
return plugin | ||
except TypeError: | ||
# Abstract class (Plugin) does not implement methods like load & unload | ||
logging.error(f'Plugin "{name}" does not implement abstract methods') | ||
except AttributeError: | ||
# No init() function | ||
logging.error(f'Plugin "{name}" does not provide an init() function') | ||
except ModuleNotFoundError: | ||
# No such file | ||
logging.error(f'Plugin "{name}" does not exist') | ||
|
||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from typing import Generator, List, Tuple | ||
|
||
from utils.const import PLUGINS_DIR | ||
|
||
_PRIORITIES: List[Tuple[str, int]] = [] | ||
|
||
|
||
def fetch_plugins() -> Generator[Tuple[str, int], None, None]: | ||
with open(f'{PLUGINS_DIR}/priorities.csv', 'r', encoding='utf-8') as f: | ||
name, priority = f.readline().strip().split(',') | ||
_PRIORITIES.append((name, int(priority))) | ||
yield (name, int(priority)) | ||
f.close() | ||
|
||
|
||
def add_new_plugin(name: str, priority: int) -> None: | ||
for pp_pair in _PRIORITIES: | ||
if pp_pair[0] == name: | ||
return | ||
_PRIORITIES.append((name, priority)) | ||
_sort_priorities() | ||
_save_priorities() | ||
|
||
|
||
def change_plugin_priority(name: str, priority: int) -> None: | ||
for i, pp_pair in enumerate(_PRIORITIES): | ||
if pp_pair[0] == name: | ||
_PRIORITIES[i] = (name, priority) | ||
|
||
|
||
def _save_priorities() -> None: | ||
with open(f'{PLUGINS_DIR}/priorities.csv', 'w', encoding='utf-8') as f: | ||
for prio in _PRIORITIES: | ||
f.write(f'{prio[0]},{prio[1]}') | ||
f.close() | ||
|
||
|
||
def _sort_priorities() -> None: | ||
_PRIORITIES.sort(key=lambda o: o[1]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# TODO: Validate plugins TOML | ||
def validate_toml(toml: dict) -> bool: | ||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters