Flags are now configurable. Plugins no longer provide default short flags.

This commit is contained in:
Gnarwhal 2024-09-09 05:47:02 +00:00
parent d95038a650
commit 846a33a941
Signed by: Gnarwhal
GPG key ID: 0989A73D8C421174
6 changed files with 50 additions and 36 deletions

@ -1 +1 @@
Subproject commit 8ba852a8495b79361b4cc29dec12b6b2137f6bff
Subproject commit 29164805fbb4670befd826e4a44892f00f84688f

View file

@ -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(

View file

@ -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()))

View file

@ -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(

View file

@ -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():

View file

@ -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():