From 00f33212306ed39638a1218e6ccbed244658d597 Mon Sep 17 00:00:00 2001 From: Gnarwhal Date: Mon, 2 Sep 2024 21:33:11 +0000 Subject: [PATCH] Extract Plugin class and plugin loading from main.py --- src/sshare/main.py | 35 +++++++++++-------------------- src/sshare/plugin.py | 35 +++++++++++++++++++++++++++++++ src/sshare/plugins/default/ssh.py | 2 +- 3 files changed, 48 insertions(+), 24 deletions(-) create mode 100644 src/sshare/plugin.py diff --git a/src/sshare/main.py b/src/sshare/main.py index 3df6f07..e40a269 100644 --- a/src/sshare/main.py +++ b/src/sshare/main.py @@ -14,17 +14,15 @@ import argparse import getpass -import importlib -import importlib.util import os import os.path -import pyclip import time import tomllib import subprocess import sys from pathlib import Path +from sshare.plugin import Plugin from sshare.plugins.config import Flag from sshare.plugins.config import NoArgument from sshare.plugins.config import NoDefault @@ -57,10 +55,6 @@ def fatalicize(logger): sys.exit(1) setattr(logger, "fatal", fatal) -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" @@ -71,17 +65,18 @@ def main(): # so that it can be used to report errors while loading and # configuring plugins # i.e. before other logging plugins have had a chance to be initialised - logger = importlib.import_module("sshare.plugins.default.command_line") + command_line = Plugin.internal("command_line") + logger = command_line.module fatalicize(logger) # Load inbuilt plugins plugins_flat = [ - Plugin("command_line", logger), - Plugin("file", importlib.import_module("sshare.plugins.default.file")), - Plugin("current_time", importlib.import_module("sshare.plugins.default.current_time")), - Plugin("append_type", importlib.import_module("sshare.plugins.default.append_type")), - Plugin("ssh", importlib.import_module("sshare.plugins.default.ssh")), - Plugin("log_result", importlib.import_module("sshare.plugins.default.log_result")), + command_line, + Plugin.internal("file"), + Plugin.internal("current_time"), + Plugin.internal("append_type"), + Plugin.internal("ssh"), + Plugin.internal("log_result"), ] plugins = {} for type in [ "logger", "source", "name", "upload", "result" ]: @@ -89,15 +84,9 @@ def main(): # Load external plugins sys.dont_write_bytecode = True - for plugin in (config_directory / "plugins").iterdir(): - if plugin.is_file() and plugin.suffix == ".py": - 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) - plugins_flat.append(Plugin(plugin.stem, module)) + for path in (config_directory / "plugins").iterdir(): + if path.is_file() and path.suffix == ".py": + plugins_flat.append(Plugin.external(path)) sys.dont_write_bytecode = False # Set plugin configurations from config file diff --git a/src/sshare/plugin.py b/src/sshare/plugin.py new file mode 100644 index 0000000..3fdd41f --- /dev/null +++ b/src/sshare/plugin.py @@ -0,0 +1,35 @@ +# This file is part of SSHare. +# +# SSHare is free software: you can redistribute it and/or modify it under the terms of +# the GNU General Public License as published by the Free Software Foundation, +# either version 3 of the License, or (at your option) any later version. +# +# SSHare is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY +# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +# more details. +# +# You should have received a copy of the GNU General Public License along with +# SSHare. If not, see . + +import importlib +import importlib.util + +class Plugin: + def __init__(self, name, module): + self.name = name + self.module = module + + @staticmethod + def internal(name): + return Plugin(name, importlib.import_module(f"sshare.plugins.default.{name}")) + + @staticmethod + def external(path): + module_spec = importlib.util.spec_from_file_location( + path.stem, + path.as_posix(), + ) + module = importlib.util.module_from_spec(module_spec) + module_spec.loader.exec_module(module) + return Plugin(path.stem, module) diff --git a/src/sshare/plugins/default/ssh.py b/src/sshare/plugins/default/ssh.py index b06325d..6aca493 100644 --- a/src/sshare/plugins/default/ssh.py +++ b/src/sshare/plugins/default/ssh.py @@ -52,7 +52,7 @@ def upload(name, source): f"-p {config.port}", f"{config.user}@{config.host}", "-T", - f"cat - > {config.path}/{name}" + f"cat > {config.path}/{name}" ] process = subprocess.run( command,