Several changes

- Plugin configurations are only validated if they are active
- Fixed issue with how Argument was passing it's paramateres to add_argument
- Added new plugin type 'result'
- Added inbuilt log_result plugin
- Added clipboard plugin
- Fix issue with misidentifying non-plugins in plugins directory as plugins
This commit is contained in:
Gnarwhal 2024-08-31 19:56:48 +00:00
parent e6a42988c6
commit 7e3e713b0d
Signed by: Gnarwhal
GPG key ID: 0989A73D8C421174
4 changed files with 71 additions and 62 deletions

@ -1 +1 @@
Subproject commit 3f1ebb21262297d8b349b15c950ed51ee28f7fae Subproject commit 70b54a2df4d23622fda418da55063da90817e313

View file

@ -80,15 +80,16 @@ def main():
Plugin("current_time", importlib.import_module("plugins.default.current_time")), Plugin("current_time", importlib.import_module("plugins.default.current_time")),
Plugin("append_type", importlib.import_module("plugins.default.append_type")), Plugin("append_type", importlib.import_module("plugins.default.append_type")),
Plugin("ssh", importlib.import_module("plugins.default.ssh")), Plugin("ssh", importlib.import_module("plugins.default.ssh")),
Plugin("log_result", importlib.import_module("plugins.default.log_result")),
] ]
plugins = {} plugins = {}
for type in [ "logger", "source", "name", "upload" ]: for type in [ "logger", "source", "name", "upload", "result" ]:
plugins[type] = { "active": [], "inactive": [] } plugins[type] = { "active": [], "inactive": [] }
# Load external plugins # Load external plugins
sys.dont_write_bytecode = True sys.dont_write_bytecode = True
for plugin in (config_directory / "plugins").iterdir(): for plugin in (config_directory / "plugins").iterdir():
if plugin.is_file(): if plugin.is_file() and plugin.suffix == ".py":
module_spec = importlib.util.spec_from_file_location( module_spec = importlib.util.spec_from_file_location(
plugin.stem, plugin.stem,
plugin.as_posix(), plugin.as_posix(),
@ -104,7 +105,7 @@ def main():
argument_map = {} argument_map = {}
used_arguments = {} used_arguments = {}
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
prog = "SSHare", prog = "sshare",
description = "Upload files to a server via ssh", description = "Upload files to a server via ssh",
) )
parser.add_argument( parser.add_argument(
@ -171,6 +172,11 @@ def main():
if not active: if not active:
break break
plugins_of_type["active" if active else "inactive"].append(plugin) plugins_of_type["active" if active else "inactive"].append(plugin)
if active:
for config_entry in plugin.module.config.items():
if config_entry[1] == NoDefault:
logger.error(f"Error: Value 'plugins.{plugin.name}.{config_entry[0]}' has no default value and must be specified explicitly")
error = True
for plugin_type, plugins_of_type in plugins.items(): for plugin_type, plugins_of_type in plugins.items():
if len(plugins_of_type["active"]) == 0 and plugin_type != "logger": if len(plugins_of_type["active"]) == 0 and plugin_type != "logger":
if len(plugins_of_type["inactive"]) == 0: if len(plugins_of_type["inactive"]) == 0:
@ -188,19 +194,14 @@ def main():
if error: if error:
sys.exit(1) sys.exit(1)
# Flatten plugin configs # Objectify configs
error = False error = False
class PluginConfig: pass class PluginConfig: pass
for plugin in plugins_flat: for plugin in plugins_flat:
if hasattr(plugin.module, "config"): config = plugin.module.config
config = plugin.module.config plugin.module.config = PluginConfig()
plugin.module.config = PluginConfig() for config_entry in config.items():
for config_entry in config.items(): setattr(plugin.module.config, config_entry[0], config_entry[1])
if config_entry[1] == NoDefault:
logger.error(f"Error: Value 'plugins.{plugin.name}.{config_entry[0]}' has no default value and must be specified explicitly")
error = True
else:
setattr(plugin.module.config, config_entry[0], config_entry[1])
if error: if error:
sys.exit(1) sys.exit(1)
@ -225,43 +226,12 @@ def main():
name = plugin.module.name(name, source) name = plugin.module.name(name, source)
sources[index] = name, source sources[index] = name, source
for (name, source) in sources: for name, source in sources:
for plugin in plugins["upload"]["active"]: for plugin in plugins["upload"]["active"]:
plugin.module.upload(name, source) plugin.module.upload(name, source)
for name, _ in sources:
for plugin in plugins["result"]["active"]:
plugin.module.result(name)
sys.exit(0) sys.exit(0)
def parse_arguments():
parser = argparse.ArgumentParser(
prog = "SSHare",
description = "Upload files to a server via ssh",
)
parser.add_argument(
"-l",
"--latest",
action="store_const",
const=True,
help="Upload the latest image from the source directory",
)
parser.add_argument(
"-p",
"--paste",
action="store_const",
const=True,
help="Upload the contents of the clipboard as a .txt file",
)
parser.add_argument(
"-f",
"--file",
help="Upload a file",
)
parser.add_argument(
"-c",
"--copy",
action="store_const",
const=True,
help="Copy the resultant URL to the clipboard",
)
return arguments

View file

@ -21,13 +21,13 @@ class Argument:
def __init__(self, def __init__(self,
short=None, short=None,
long=None, long=None,
action='store', action=None,
nargs=None, nargs=None,
const=None, const=None,
default=NoArgument, default=NoArgument,
type=str, type=None,
choices=None, choices=None,
required=False, required=None,
help=None): help=None):
self.short = short self.short = short
self.long = long self.long = long
@ -57,18 +57,26 @@ class Argument:
self.dest = f"{plugin.name}_{argument}" self.dest = f"{plugin.name}_{argument}"
def add(self, parser, used_arguments): def add(self, parser, used_arguments):
keywords = [
"action",
"nargs",
"const",
"default",
"type",
"choices",
"help",
"metavar",
"dest"
]
kwargs = {}
for keyword in keywords:
value = getattr(self, keyword)
if value != None:
kwargs[keyword] = value
parser.add_argument( parser.add_argument(
f"-{self.short}", f"-{self.short}",
f"--{self.long}", f"--{self.long}",
action=self.action, **kwargs
nargs=self.nargs,
const=self.const,
default=self.default,
type=self.type,
choices=self.choices,
help=self.help,
metavar=self.metavar,
dest=self.dest,
) )
if self.short: if self.short:
used_arguments["short"] = self.plugin used_arguments["short"] = self.plugin

View file

@ -0,0 +1,31 @@
# 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/>.
from ..config import NoDefault
plugin_type = "result"
config = {
"protocol": "https",
"host": NoDefault,
"port": None,
"path": "",
}
def result(name):
if config.port:
config.port = f":{config.port}"
else:
config.port = ""
logger.info(f"Uploaded to '{config.protocol}://{config.host}{config.port}{config.path}/{name}'")