diff --git a/examples b/examples index 3f1ebb2..70b54a2 160000 --- a/examples +++ b/examples @@ -1 +1 @@ -Subproject commit 3f1ebb21262297d8b349b15c950ed51ee28f7fae +Subproject commit 70b54a2df4d23622fda418da55063da90817e313 diff --git a/src/sshare/main.py b/src/sshare/main.py index 3458f2f..e015c9d 100644 --- a/src/sshare/main.py +++ b/src/sshare/main.py @@ -80,15 +80,16 @@ def main(): Plugin("current_time", importlib.import_module("plugins.default.current_time")), Plugin("append_type", importlib.import_module("plugins.default.append_type")), Plugin("ssh", importlib.import_module("plugins.default.ssh")), + Plugin("log_result", importlib.import_module("plugins.default.log_result")), ] plugins = {} - for type in [ "logger", "source", "name", "upload" ]: + for type in [ "logger", "source", "name", "upload", "result" ]: plugins[type] = { "active": [], "inactive": [] } # Load external plugins sys.dont_write_bytecode = True 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( plugin.stem, plugin.as_posix(), @@ -104,7 +105,7 @@ def main(): argument_map = {} used_arguments = {} parser = argparse.ArgumentParser( - prog = "SSHare", + prog = "sshare", description = "Upload files to a server via ssh", ) parser.add_argument( @@ -171,6 +172,11 @@ def main(): if not active: break 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(): if len(plugins_of_type["active"]) == 0 and plugin_type != "logger": if len(plugins_of_type["inactive"]) == 0: @@ -188,19 +194,14 @@ def main(): if error: sys.exit(1) - # Flatten plugin configs + # Objectify configs error = False class PluginConfig: pass for plugin in plugins_flat: - if hasattr(plugin.module, "config"): - config = plugin.module.config - plugin.module.config = PluginConfig() - for config_entry in 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 - else: - setattr(plugin.module.config, config_entry[0], config_entry[1]) + config = plugin.module.config + plugin.module.config = PluginConfig() + for config_entry in config.items(): + setattr(plugin.module.config, config_entry[0], config_entry[1]) if error: sys.exit(1) @@ -225,43 +226,12 @@ def main(): name = plugin.module.name(name, source) sources[index] = name, source - for (name, source) in sources: + for name, source in sources: for plugin in plugins["upload"]["active"]: plugin.module.upload(name, source) + for name, _ in sources: + for plugin in plugins["result"]["active"]: + plugin.module.result(name) + 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 diff --git a/src/sshare/plugins/config.py b/src/sshare/plugins/config.py index a6e503a..7fb7c2a 100644 --- a/src/sshare/plugins/config.py +++ b/src/sshare/plugins/config.py @@ -21,13 +21,13 @@ class Argument: def __init__(self, short=None, long=None, - action='store', + action=None, nargs=None, const=None, default=NoArgument, - type=str, + type=None, choices=None, - required=False, + required=None, help=None): self.short = short self.long = long @@ -57,18 +57,26 @@ class Argument: self.dest = f"{plugin.name}_{argument}" 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( f"-{self.short}", f"--{self.long}", - action=self.action, - nargs=self.nargs, - const=self.const, - default=self.default, - type=self.type, - choices=self.choices, - help=self.help, - metavar=self.metavar, - dest=self.dest, + **kwargs ) if self.short: used_arguments["short"] = self.plugin diff --git a/src/sshare/plugins/default/log_result.py b/src/sshare/plugins/default/log_result.py new file mode 100644 index 0000000..9884385 --- /dev/null +++ b/src/sshare/plugins/default/log_result.py @@ -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 . + +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}'")