Extract Plugin class and plugin loading from main.py
This commit is contained in:
parent
2a0b3dcb56
commit
00f3321230
3 changed files with 48 additions and 24 deletions
|
@ -14,17 +14,15 @@
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import getpass
|
import getpass
|
||||||
import importlib
|
|
||||||
import importlib.util
|
|
||||||
import os
|
import os
|
||||||
import os.path
|
import os.path
|
||||||
import pyclip
|
|
||||||
import time
|
import time
|
||||||
import tomllib
|
import tomllib
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
from sshare.plugin import Plugin
|
||||||
from sshare.plugins.config import Flag
|
from sshare.plugins.config import Flag
|
||||||
from sshare.plugins.config import NoArgument
|
from sshare.plugins.config import NoArgument
|
||||||
from sshare.plugins.config import NoDefault
|
from sshare.plugins.config import NoDefault
|
||||||
|
@ -57,10 +55,6 @@ def fatalicize(logger):
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
setattr(logger, "fatal", fatal)
|
setattr(logger, "fatal", fatal)
|
||||||
|
|
||||||
class Plugin:
|
|
||||||
def __init__(self, name, module):
|
|
||||||
self.name = name
|
|
||||||
self.module = module
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
config_directory = Path(os.environ.get("XDG_CONFIG_DIR") or f"{os.environ["HOME"]}/.config") / "sshare"
|
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
|
# so that it can be used to report errors while loading and
|
||||||
# configuring plugins
|
# configuring plugins
|
||||||
# i.e. before other logging plugins have had a chance to be initialised
|
# 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)
|
fatalicize(logger)
|
||||||
|
|
||||||
# Load inbuilt plugins
|
# Load inbuilt plugins
|
||||||
plugins_flat = [
|
plugins_flat = [
|
||||||
Plugin("command_line", logger),
|
command_line,
|
||||||
Plugin("file", importlib.import_module("sshare.plugins.default.file")),
|
Plugin.internal("file"),
|
||||||
Plugin("current_time", importlib.import_module("sshare.plugins.default.current_time")),
|
Plugin.internal("current_time"),
|
||||||
Plugin("append_type", importlib.import_module("sshare.plugins.default.append_type")),
|
Plugin.internal("append_type"),
|
||||||
Plugin("ssh", importlib.import_module("sshare.plugins.default.ssh")),
|
Plugin.internal("ssh"),
|
||||||
Plugin("log_result", importlib.import_module("sshare.plugins.default.log_result")),
|
Plugin.internal("log_result"),
|
||||||
]
|
]
|
||||||
plugins = {}
|
plugins = {}
|
||||||
for type in [ "logger", "source", "name", "upload", "result" ]:
|
for type in [ "logger", "source", "name", "upload", "result" ]:
|
||||||
|
@ -89,15 +84,9 @@ def main():
|
||||||
|
|
||||||
# Load external plugins
|
# Load external plugins
|
||||||
sys.dont_write_bytecode = True
|
sys.dont_write_bytecode = True
|
||||||
for plugin in (config_directory / "plugins").iterdir():
|
for path in (config_directory / "plugins").iterdir():
|
||||||
if plugin.is_file() and plugin.suffix == ".py":
|
if path.is_file() and path.suffix == ".py":
|
||||||
module_spec = importlib.util.spec_from_file_location(
|
plugins_flat.append(Plugin.external(path))
|
||||||
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))
|
|
||||||
sys.dont_write_bytecode = False
|
sys.dont_write_bytecode = False
|
||||||
|
|
||||||
# Set plugin configurations from config file
|
# Set plugin configurations from config file
|
||||||
|
|
35
src/sshare/plugin.py
Normal file
35
src/sshare/plugin.py
Normal 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)
|
|
@ -52,7 +52,7 @@ def upload(name, source):
|
||||||
f"-p {config.port}",
|
f"-p {config.port}",
|
||||||
f"{config.user}@{config.host}",
|
f"{config.user}@{config.host}",
|
||||||
"-T",
|
"-T",
|
||||||
f"cat - > {config.path}/{name}"
|
f"cat > {config.path}/{name}"
|
||||||
]
|
]
|
||||||
process = subprocess.run(
|
process = subprocess.run(
|
||||||
command,
|
command,
|
||||||
|
|
Loading…
Reference in a new issue