Modifications to arguments

- Arguments now use NoArgument instead of None to identify unset flags
- Arguments can now be optional and an activation flag using nargs="?" and const=Flag
This commit is contained in:
Gnarwhal 2024-08-31 18:26:35 +00:00
parent 987eddbf00
commit dd46dbc7b7
Signed by: Gnarwhal
GPG key ID: 0989A73D8C421174
4 changed files with 21 additions and 24 deletions

3
.gitmodules vendored Normal file
View file

@ -0,0 +1,3 @@
[submodule "examples"]
path = examples
url = ./examples

View file

@ -26,6 +26,8 @@ import sys
from pathlib import Path
from version import version
from plugins.config import Flag
from plugins.config import NoArgument
from plugins.config import NoDefault
class Congloggerate:
@ -124,7 +126,7 @@ def main():
if hasattr(plugin.module, "args"):
for arg_name, arg in plugin.module.args.items():
if arg.is_valid():
arg.set_for_plugin(plugin)
arg.bind(plugin, arg_name)
def check_flag(flag):
if flag in used_arguments:
logger.error(f"Error: Argument '{arg_name}' for plugin '{plugin.name}' has conflict. Flag '{flag}' is also used by plugin '{used_arguments[arg.short]}'")
@ -141,8 +143,10 @@ def main():
arguments = parser.parse_args()
for arg, (plugin, config) in list(argument_map.items()):
if getattr(arguments, arg):
plugin.module.config[config] = getattr(arguments, arg)
value = getattr(arguments, arg)
if value != NoArgument:
if value != Flag:
plugin.module.config[config] = value
del argument_map[arg]
# Sort plugins by type and check activation criteria
@ -210,7 +214,7 @@ def main():
sources = []
for plugin in plugins["source"]["active"]:
sources.append(plugin.module.get())
sources.append(plugin.module.source())
if len(sources) == 0:
logger.error("Error: No sources provided. Must activate at least one source plugin")
log_activations(logger, plugins["source"])
@ -261,15 +265,3 @@ def parse_arguments():
)
return arguments
def _latest(directory, key=os.path.getmtime):
files = map(lambda file: f"{directory}/{file}", os.listdir(directory))
selection = next(files)
selection_key = key(selection)
for file in files:
new_key = key(file)
if new_key > selection_key:
selection = file
selection_key = key
return selection

View file

@ -14,6 +14,9 @@
class NoDefault: pass
class NoArgument: pass
class Flag: pass
class Argument:
def __init__(self,
short=None,
@ -21,12 +24,11 @@ class Argument:
action='store',
nargs=None,
const=None,
default=None,
default=NoArgument,
type=str,
choices=None,
required=False,
help=None,
metavar=None):
help=None):
self.short = short
self.long = long
self.action = action
@ -36,7 +38,6 @@ class Argument:
self.type = type
self.choices = choices
self.help = help
self.metavar = metavar or self.long or self.short
def is_valid(self):
return (self.short != None and self.short != "") or (self.long != None and self.long != "")
@ -50,9 +51,10 @@ class Argument:
pretty = f"-{self.short}"
return pretty + f" {self.help}"
def set_for_plugin(self, plugin):
self.plugin = plugin
self.dest = f"{plugin.name}_{self.metavar}"
def bind(self, plugin, argument):
self.plugin = plugin
self.metavar = argument
self.dest = f"{plugin.name}_{argument}"
def add(self, parser, used_arguments):
parser.add_argument(

View file

@ -27,7 +27,7 @@ args = {
)
}
def get():
def source():
file = File(config.file)
if file.path.is_dir():
logger.info(f"Uploading directory '{config.file}'")