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" 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: with open(arguments.config or (config_directory / "config.toml"), mode="rb") as file:
config = tomllib.load(file) config = tomllib.load(file)
config["config"] = config.get("config", {})
config["flags" ] = config.get("flags", {})
arg_parser.add_argument( arg_parser.add_argument(
"-h", "-h",
@ -59,12 +61,18 @@ def main():
# Load command line early and set it as the active logger # Load command line early and set it as the active logger
# so that it can be used to report errors while loading and # so that it can be used to report errors while loading and
# configuring other loggers # 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) logger = Logger(command_line=command_line)
plugins = PluginManager( plugins = PluginManager(
[ "logger", "source", "name", "upload", "location", "feedback" ], [ "logger", "source", "name", "upload", "location", "feedback" ],
logger, logger,
config.get("plugins", dict()), config["config"],
config["flags"],
arg_parser, arg_parser,
) )
plugins.add_from( plugins.add_from(

View file

@ -19,10 +19,11 @@ from sshare.plugins.config import Flag
from sshare.plugins.config import NoDefault from sshare.plugins.config import NoDefault
class PluginManager: class PluginManager:
def __init__(self, types, logger, config, arg_parser): def __init__(self, types, logger, config, flags, arg_parser):
self._uninitialized = [] self._uninitialized = []
self._logger = logger self._logger = logger
self._config = config self._config = config
self._flags = flags
self._arg_parser = arg_parser self._arg_parser = arg_parser
class PluginState: class PluginState:
@ -34,7 +35,7 @@ class PluginManager:
def add_from(self, location, *args, **kwargs): def add_from(self, location, *args, **kwargs):
for plugin in args: for plugin in args:
plugin = location(plugin, self._config) plugin = location(plugin, self._config, self._flags)
plugin.set_logger(self._logger) plugin.set_logger(self._logger)
plugin.add_args(self._arg_parser) plugin.add_args(self._arg_parser)
self._uninitialized.append(plugin) self._uninitialized.append(plugin)
@ -52,7 +53,7 @@ class PluginManager:
).append(plugin) ).append(plugin)
class Plugin: class Plugin:
def __init__(self, name, module, external_config): def __init__(self, name, module, external_config, external_flags):
self.__dict__ = module.__dict__ self.__dict__ = module.__dict__
self.name = name self.name = name
@ -83,9 +84,12 @@ class Plugin:
if hasattr(self, "args"): if hasattr(self, "args"):
for arg in self.args.items(): for arg in self.args.items():
arg[1].bind(self, arg[0]) 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() value = arg[1].default()
if value != NoDefault: if value != NoDefault:
config[arg[0]] = value config[arg[0]] = value
else: else:
self.args = dict() self.args = dict()
@ -122,20 +126,20 @@ class Plugin:
return activate return activate
@staticmethod @staticmethod
def internal(location, name=None, config=None): def internal(location, name=None, config=None, flags=None):
def _load_internal(_name, _config): def _load_internal(_name, _config, _flags):
return Plugin(_name, importlib.import_module(f"{location}.{_name}"), _config.get(_name, dict())) return Plugin(_name, importlib.import_module(f"{location}.{_name}"), _config.get(_name, dict()), _flags.get(_name, dict()))
if name == None: if name == None:
return _load_internal return _load_internal
else: else:
return _load_internal(name, config) return _load_internal(name, config, flags)
@staticmethod @staticmethod
def external(path, config): def external(path, config, flags):
module_spec = importlib.util.spec_from_file_location( module_spec = importlib.util.spec_from_file_location(
path.stem, path.stem,
path.as_posix(), path.as_posix(),
) )
module = importlib.util.module_from_spec(module_spec) module = importlib.util.module_from_spec(module_spec)
module_spec.loader.exec_module(module) 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 class NoDefault: pass
def Flag(short=None, long=None, help=None): def Flag(name=None, help=None):
return Argument( return Argument(
short, name,
long,
action="store_const", action="store_const",
const=Flag, const=Flag,
default=False, default=False,
@ -25,14 +24,14 @@ def Flag(short=None, long=None, help=None):
) )
class Argument: class Argument:
def __init__(self, short=None, long=None, **kwargs): def __init__(self, name=None, **kwargs):
class _None: class _None:
def __init__(self, default): def __init__(self, default):
self.default = default self.default = default
self._None = _None self._None = _None
self.short = short self._short = None
self.long = long self._long = name
if not "default" in kwargs: if not "default" in kwargs:
kwargs["default"] = NoDefault kwargs["default"] = NoDefault
@ -40,8 +39,22 @@ class Argument:
self._kwargs = kwargs self._kwargs = kwargs
def bind(self, plugin, argument): def bind(self, plugin, argument):
self._plugin = plugin.name self._plugin = plugin.name
self._argument = argument 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): def default(self):
value = self._kwargs["default"] value = self._kwargs["default"]
@ -50,7 +63,7 @@ class Argument:
return value return value
def dest(self): def dest(self):
return f"{self._plugin}_{self._argument}" return f"{self._plugin}_{self._kwargs["metavar"]}"
def extract(self, arguments): def extract(self, arguments):
value = getattr(arguments, self.dest()) value = getattr(arguments, self.dest())
@ -62,13 +75,9 @@ class Argument:
def add(self, arg_parser): def add(self, arg_parser):
flags = [] flags = []
if self.short: if self._short: flags.append(f"-{self._short}")
flags.append(f"-{self.short}") if self._long: flags.append(f"--{self._long}")
long = self.long or self._argument
if long:
flags.append(f"--{long}")
kwargs = self._kwargs | { kwargs = self._kwargs | {
"metavar": self._argument,
"dest": self.dest() "dest": self.dest()
} }
arg_parser.add_argument( arg_parser.add_argument(

View file

@ -23,11 +23,7 @@ config = {
"base": 62, "base": 62,
} }
args = { args = {
"base": Argument( "base": Argument(help="Set the numeric base to use for the current time"),
short="b",
long="base",
help="Set the numeric base to use for the current time"
)
} }
def init(): def init():

View file

@ -20,10 +20,7 @@ plugin_type = "source"
activate = { "file" } activate = { "file" }
args = { args = {
"file": Argument( "file": Argument(help="Upload a file")
short="f",
help="Upload a file"
)
} }
def get_source(): def get_source():