Plugins are loading
This commit is contained in:
parent
72627995a7
commit
98c490dab9
4 changed files with 64 additions and 21 deletions
|
@ -27,4 +27,4 @@ Repository = "https://forge.monodon.me/Gnarwhal/sshare"
|
|||
Issues = "https://forge.monodon.me/Gnarwhal/sshare/issues"
|
||||
|
||||
[project.scripts]
|
||||
sshare = "sshare.cli:main"
|
||||
sshare = "sshare.main:main"
|
||||
|
|
|
@ -12,6 +12,6 @@
|
|||
# You should have received a copy of the GNU General Public License along with
|
||||
# SSHare. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
from cli import main
|
||||
from main import main
|
||||
|
||||
main()
|
||||
|
|
|
@ -12,10 +12,12 @@
|
|||
# You should have received a copy of the GNU General Public License along with
|
||||
# SSHare. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
plugin_type = "logger"
|
||||
|
||||
def _print_with_color(color, message):
|
||||
print(f"\033[{color}m{message}\033[0m")
|
||||
|
||||
def log(message):
|
||||
def info(message):
|
||||
_print_with_color(0, message)
|
||||
|
||||
def warn(message):
|
|
@ -23,14 +23,12 @@ import time
|
|||
import tomllib
|
||||
import subprocess
|
||||
import sys
|
||||
from sshare.version import version
|
||||
from pathlib import Path
|
||||
from version import version
|
||||
|
||||
class Config:
|
||||
def __init__(self):
|
||||
config_directory = os.environ.get("XDG_CONFIG_DIR")
|
||||
if config_directory == None:
|
||||
config_directory = f"{os.environ["HOME"]}/.config"
|
||||
_config = self._load_from_file(f"{config_directory}/sshare/config.toml")
|
||||
def __init__(self, config_directory):
|
||||
_config = self._load_from_file(config_directory / "config.toml")
|
||||
|
||||
self.source_directory = _config.get("source_directory")
|
||||
|
||||
|
@ -90,21 +88,64 @@ def rebase(number):
|
|||
return rebased
|
||||
|
||||
|
||||
def main():
|
||||
module_spec = importlib.util.spec_from_file_location(
|
||||
"logger_cli",
|
||||
"/home/gnarwhal/meine/projects/active/sshare/src/sshare/plugins/logger_cli.py"
|
||||
)
|
||||
module = importlib.util.module_from_spec(module_spec)
|
||||
module_spec.loader.exec_module(module)
|
||||
class MetaLogger:
|
||||
def __init__(self, loggers):
|
||||
def info(message):
|
||||
for logger in loggers:
|
||||
logger.info(message)
|
||||
|
||||
module.log("test?")
|
||||
module.warn("test?")
|
||||
module.error("test?")
|
||||
def warn(message):
|
||||
for logger in loggers:
|
||||
logger.warn(message)
|
||||
|
||||
def error(message):
|
||||
for logger in loggers:
|
||||
logger.error(message)
|
||||
|
||||
self.info = info
|
||||
self.warn = warn
|
||||
self.error = error
|
||||
|
||||
class Plugin:
|
||||
def __init__(self, name, module):
|
||||
self.name = name
|
||||
self.module = module
|
||||
|
||||
def main():
|
||||
config_directory = Path(os.environ.get("XDG_CONFIG_DIR") or f"{os.environ["HOME"]}/.config") / "sshare"
|
||||
logger = importlib.import_module("command_line_logger")
|
||||
modules = {
|
||||
"logger": [ Plugin("command_line_logger", logger) ],
|
||||
"data": [],
|
||||
}
|
||||
error = False
|
||||
sys.dont_write_bytecode = True
|
||||
for plugin in (config_directory / "plugins").iterdir():
|
||||
if plugin.is_file():
|
||||
module_spec = importlib.util.spec_from_file_location(
|
||||
plugin.stem,
|
||||
plugin.as_posix(),
|
||||
)
|
||||
module = importlib.util.module_from_spec(module_spec)
|
||||
module_spec.loader.exec_module(module)
|
||||
|
||||
modules_of_type = modules.get(module.plugin_type)
|
||||
if modules_of_type == None:
|
||||
logger.error(f"Error: Plugin '{plugin.stem}' has an invalid plugin type '{module.plugin_type}'")
|
||||
error = True
|
||||
else:
|
||||
modules_of_type.append(Plugin(plugin.stem, module))
|
||||
if hasattr(module, "init"):
|
||||
module.init()
|
||||
if error:
|
||||
sys.exit(1)
|
||||
sys.dont_write_bytecode = False
|
||||
logger = MetaLogger([ logger.module for logger in modules["logger"] ])
|
||||
logger.info("Successfully loaded plugins")
|
||||
|
||||
arguments = parse_arguments()
|
||||
|
||||
config = Config()
|
||||
config = Config(config_directory)
|
||||
|
||||
contents = b''
|
||||
target_file_extension = ""
|
Loading…
Reference in a new issue