Extract Plugin class and plugin loading from main.py

This commit is contained in:
Gnarwhal 2024-09-02 21:33:11 +00:00
parent 2a0b3dcb56
commit 00f3321230
Signed by: Gnarwhal
GPG key ID: 0989A73D8C421174
3 changed files with 48 additions and 24 deletions

View file

@ -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

35
src/sshare/plugin.py Normal file
View file

@ -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 <https://www.gnu.org/licenses/>.
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)

View file

@ -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,