diff --git a/examples b/examples index 3273aa4..99f2e9c 160000 --- a/examples +++ b/examples @@ -1 +1 @@ -Subproject commit 3273aa4e4c1898c484e989340a8525edb343cebe +Subproject commit 99f2e9c3d2fbab02b0582dc8dcf9ed05df7789a6 diff --git a/src/sshare/main.py b/src/sshare/main.py index da4369b..f0744ec 100644 --- a/src/sshare/main.py +++ b/src/sshare/main.py @@ -40,6 +40,31 @@ def main(): arguments, _ = arg_parser.parse_known_args() with open(arguments.config or config_directory.default_config(), mode="rb") as file: config = tomllib.load(file) + + config["spec"] = config.get("spec", {}) + for spec_name, spec in config["spec"].items(): + if spec_name != "default": + flags = [] + if spec.get("flag", {}).get("short") != None: + flags = flags + [ f"-{spec["flag"]["short"]}" ] + if spec.get("flag", {}).get("long") != None: + flags = flags + [ f"--{spec["flag"]["long"]}" ] + arg_parser.add_argument( + *flags, + action="store_const", + const=True, + default=False, + help=spec.get(help, f"Use {spec_name} spec"), + dest=spec_name, + ) + + use_spec = config["spec"].get("default", {}) + arguments, _ = arg_parser.parse_known_args() + for spec_name, spec in config["spec"].items(): + if spec_name != "default": + if getattr(arguments, spec_name): + use_spec = spec + config["config"] = config.get("config", {}) config["flags" ] = config.get("flags", {}) @@ -69,8 +94,9 @@ def main(): logger.add(command_line) plugins = PluginManager( logger, + use_spec, config["config"], - config["flags"], + config["flags" ], arg_parser, ) plugins.activate("logger") diff --git a/src/sshare/plugin/plugin.py b/src/sshare/plugin/plugin.py index 558f2c2..213db6d 100644 --- a/src/sshare/plugin/plugin.py +++ b/src/sshare/plugin/plugin.py @@ -35,8 +35,9 @@ class PluginLoader: in ([ "command_line" ] if command_line else []) + [ "file", "stdin", - "current_time", - "append_type", + "preserve", + "time", + "extension", "ssh", "uri", "print_location", @@ -60,7 +61,7 @@ class PluginLoader: ] class PluginManager: - def __init__(self, logger, config, flags, arg_parser): + def __init__(self, logger, spec, config, flags, arg_parser): self._logger = logger self._arg_parser = arg_parser @@ -71,14 +72,22 @@ class PluginManager: for type in Plugin.types(): setattr(self, type, PluginState()) - self._uninitialized = PluginLoader.all( + self._uninitialized = [] + uninitialized = PluginLoader.all( command_line=False, logger=logger, config=config, flags=flags, ) - for plugin in self._uninitialized: + for plugin in uninitialized: plugin.add_args(arg_parser) + if not "name" in plugin.plugin_type: + self._uninitialized.append(plugin) + + for name in spec.get("name", []): + for plugin in uninitialized: + if plugin.name == name: + self._uninitialized.append(plugin) def activate(self, activate_type=None): args = self._arg_parser.parse_args() diff --git a/src/sshare/plugin/source.py b/src/sshare/plugin/source.py index fcfc6a6..b2fede5 100644 --- a/src/sshare/plugin/source.py +++ b/src/sshare/plugin/source.py @@ -15,7 +15,8 @@ from pathlib import Path class Raw: - def __init__(self, type, data): + def __init__(self, name, type, data): + self.name = name self.type = type self.data = data diff --git a/src/sshare/plugins/append_type.py b/src/sshare/plugins/extension.py similarity index 100% rename from src/sshare/plugins/append_type.py rename to src/sshare/plugins/extension.py diff --git a/src/sshare/plugins/preserve.py b/src/sshare/plugins/preserve.py new file mode 100644 index 0000000..d010cde --- /dev/null +++ b/src/sshare/plugins/preserve.py @@ -0,0 +1,32 @@ +# 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 sshare.plugin.source import File +from sshare.plugin.source import Raw + +plugin_type = "name" + +def get_name(name, source): + if name != "": + name = name + "_" + else: + name = "" + if isinstance(source, File): + components = source.path.name.split(".") + if components[0] == "": + return name + "." + components[1] + else: + return name + components[0] + else: + return name + source.name diff --git a/src/sshare/plugins/stdin.py b/src/sshare/plugins/stdin.py index 7b219b6..7edccbe 100644 --- a/src/sshare/plugins/stdin.py +++ b/src/sshare/plugins/stdin.py @@ -28,4 +28,4 @@ args = { } def get_source(): - return Raw(config.suffix, sys.stdin.buffer.read()) + return Raw("stdin", config.suffix, sys.stdin.buffer.read()) diff --git a/src/sshare/plugins/current_time.py b/src/sshare/plugins/time.py similarity index 80% rename from src/sshare/plugins/current_time.py rename to src/sshare/plugins/time.py index 16cdd44..20f65d0 100644 --- a/src/sshare/plugins/current_time.py +++ b/src/sshare/plugins/time.py @@ -20,18 +20,12 @@ from sshare.plugin.source import File plugin_type = "name" config = { - "base": 62, + "format": 62, } - -def init(): - if not isinstance(config.base, int): - logger.fatal("Error: 'base' must be an integer") - elif config.base < 2: - logger.fatal("Error: 'base' cannot be less than 2") - elif config.base > 62: - logger.fatal("Error: 'base' cannot be greater than 62") - + def get_name(name, source): + if name != "": + name = name + "_" return name + _rebase(config.base, time.time_ns()) def _rebase(base, number):