From 846a33a9412586fb9ad72a0b03cf47e5f4a9ea2c Mon Sep 17 00:00:00 2001 From: Gnarwhal Date: Mon, 9 Sep 2024 05:47:02 +0000 Subject: [PATCH] Flags are now configurable. Plugins no longer provide default short flags. --- examples | 2 +- src/sshare/main.py | 12 +++++-- src/sshare/plugin.py | 22 +++++++----- src/sshare/plugins/config.py | 39 +++++++++++++--------- src/sshare/plugins/default/current_time.py | 6 +--- src/sshare/plugins/default/file.py | 5 +-- 6 files changed, 50 insertions(+), 36 deletions(-) diff --git a/examples b/examples index 8ba852a..2916480 160000 --- a/examples +++ b/examples @@ -1 +1 @@ -Subproject commit 8ba852a8495b79361b4cc29dec12b6b2137f6bff +Subproject commit 29164805fbb4670befd826e4a44892f00f84688f diff --git a/src/sshare/main.py b/src/sshare/main.py index 1a13a1b..cb0812d 100644 --- a/src/sshare/main.py +++ b/src/sshare/main.py @@ -41,6 +41,8 @@ def main(): config_directory = Path(os.environ.get("XDG_CONFIG_DIR", f"{os.environ["HOME"]}/.config")) / "sshare" with open(arguments.config or (config_directory / "config.toml"), mode="rb") as file: config = tomllib.load(file) + config["config"] = config.get("config", {}) + config["flags" ] = config.get("flags", {}) arg_parser.add_argument( "-h", @@ -59,12 +61,18 @@ def main(): # Load command line early and set it as the active logger # so that it can be used to report errors while loading and # configuring other loggers - command_line = Plugin.internal(INTERNAL_PLUGIN_LOCATION, "command_line", config.get("plugins", dict())) + command_line = Plugin.internal( + INTERNAL_PLUGIN_LOCATION, + "command_line", + config["config"], + config["flags"], + ) logger = Logger(command_line=command_line) plugins = PluginManager( [ "logger", "source", "name", "upload", "location", "feedback" ], logger, - config.get("plugins", dict()), + config["config"], + config["flags"], arg_parser, ) plugins.add_from( diff --git a/src/sshare/plugin.py b/src/sshare/plugin.py index ea24721..41abb9c 100644 --- a/src/sshare/plugin.py +++ b/src/sshare/plugin.py @@ -19,10 +19,11 @@ from sshare.plugins.config import Flag from sshare.plugins.config import NoDefault class PluginManager: - def __init__(self, types, logger, config, arg_parser): + def __init__(self, types, logger, config, flags, arg_parser): self._uninitialized = [] self._logger = logger self._config = config + self._flags = flags self._arg_parser = arg_parser class PluginState: @@ -34,7 +35,7 @@ class PluginManager: def add_from(self, location, *args, **kwargs): for plugin in args: - plugin = location(plugin, self._config) + plugin = location(plugin, self._config, self._flags) plugin.set_logger(self._logger) plugin.add_args(self._arg_parser) self._uninitialized.append(plugin) @@ -52,7 +53,7 @@ class PluginManager: ).append(plugin) class Plugin: - def __init__(self, name, module, external_config): + def __init__(self, name, module, external_config, external_flags): self.__dict__ = module.__dict__ self.name = name @@ -83,9 +84,12 @@ class Plugin: if hasattr(self, "args"): for arg in self.args.items(): arg[1].bind(self, arg[0]) + flags = external_flags.get(arg[0], {}) + arg[1].set_flags(flags.get("short"), flags.get("long")) value = arg[1].default() if value != NoDefault: config[arg[0]] = value + else: self.args = dict() @@ -122,20 +126,20 @@ class Plugin: return activate @staticmethod - def internal(location, name=None, config=None): - def _load_internal(_name, _config): - return Plugin(_name, importlib.import_module(f"{location}.{_name}"), _config.get(_name, dict())) + def internal(location, name=None, config=None, flags=None): + def _load_internal(_name, _config, _flags): + return Plugin(_name, importlib.import_module(f"{location}.{_name}"), _config.get(_name, dict()), _flags.get(_name, dict())) if name == None: return _load_internal else: - return _load_internal(name, config) + return _load_internal(name, config, flags) @staticmethod - def external(path, config): + def external(path, config, flags): 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, config.get(path.stem, dict())) + return Plugin(path.stem, module, config.get(path.stem, dict()), flags.get(path.stem, dict())) diff --git a/src/sshare/plugins/config.py b/src/sshare/plugins/config.py index 5b8c857..31c18d8 100644 --- a/src/sshare/plugins/config.py +++ b/src/sshare/plugins/config.py @@ -14,10 +14,9 @@ class NoDefault: pass -def Flag(short=None, long=None, help=None): +def Flag(name=None, help=None): return Argument( - short, - long, + name, action="store_const", const=Flag, default=False, @@ -25,14 +24,14 @@ def Flag(short=None, long=None, help=None): ) class Argument: - def __init__(self, short=None, long=None, **kwargs): + def __init__(self, name=None, **kwargs): class _None: def __init__(self, default): self.default = default self._None = _None - self.short = short - self.long = long + self._short = None + self._long = name if not "default" in kwargs: kwargs["default"] = NoDefault @@ -40,8 +39,22 @@ class Argument: self._kwargs = kwargs def bind(self, plugin, argument): - self._plugin = plugin.name - self._argument = argument + self._plugin = plugin.name + if self._long == None: + self._long = argument + self._kwargs["metavar"] = argument + + def set_flags(self, short, long): + if short != None: + if short == False: + self._short = None + else: + self._short = short + if long != None: + if long == False: + self._long = None + else: + self._long = long def default(self): value = self._kwargs["default"] @@ -50,7 +63,7 @@ class Argument: return value def dest(self): - return f"{self._plugin}_{self._argument}" + return f"{self._plugin}_{self._kwargs["metavar"]}" def extract(self, arguments): value = getattr(arguments, self.dest()) @@ -62,13 +75,9 @@ class Argument: def add(self, arg_parser): flags = [] - if self.short: - flags.append(f"-{self.short}") - long = self.long or self._argument - if long: - flags.append(f"--{long}") + if self._short: flags.append(f"-{self._short}") + if self._long: flags.append(f"--{self._long}") kwargs = self._kwargs | { - "metavar": self._argument, "dest": self.dest() } arg_parser.add_argument( diff --git a/src/sshare/plugins/default/current_time.py b/src/sshare/plugins/default/current_time.py index b11dec5..03c1692 100644 --- a/src/sshare/plugins/default/current_time.py +++ b/src/sshare/plugins/default/current_time.py @@ -23,11 +23,7 @@ config = { "base": 62, } args = { - "base": Argument( - short="b", - long="base", - help="Set the numeric base to use for the current time" - ) + "base": Argument(help="Set the numeric base to use for the current time"), } def init(): diff --git a/src/sshare/plugins/default/file.py b/src/sshare/plugins/default/file.py index ccadbba..06ac354 100644 --- a/src/sshare/plugins/default/file.py +++ b/src/sshare/plugins/default/file.py @@ -20,10 +20,7 @@ plugin_type = "source" activate = { "file" } args = { - "file": Argument( - short="f", - help="Upload a file" - ) + "file": Argument(help="Upload a file") } def get_source():