diff --git a/pyproject.toml b/pyproject.toml index 948ad06..c937898 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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" diff --git a/src/sshare/__main__.py b/src/sshare/__main__.py index 6545e2b..2ae67cd 100644 --- a/src/sshare/__main__.py +++ b/src/sshare/__main__.py @@ -12,6 +12,6 @@ # You should have received a copy of the GNU General Public License along with # SSHare. If not, see . -from cli import main +from main import main main() diff --git a/src/sshare/plugins/logger_cli.py b/src/sshare/command_line_logger.py similarity index 95% rename from src/sshare/plugins/logger_cli.py rename to src/sshare/command_line_logger.py index 42ba723..6b86cce 100644 --- a/src/sshare/plugins/logger_cli.py +++ b/src/sshare/command_line_logger.py @@ -12,10 +12,12 @@ # You should have received a copy of the GNU General Public License along with # SSHare. If not, see . +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): diff --git a/src/sshare/cli.py b/src/sshare/main.py similarity index 74% rename from src/sshare/cli.py rename to src/sshare/main.py index ddf7794..30c27e7 100644 --- a/src/sshare/cli.py +++ b/src/sshare/main.py @@ -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) + + 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") - module.log("test?") - module.warn("test?") - module.error("test?") - arguments = parse_arguments() - config = Config() + config = Config(config_directory) contents = b'' target_file_extension = ""