diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..146440a --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "examples"] + path = examples + url = ./examples diff --git a/src/sshare/main.py b/src/sshare/main.py index 86e97b3..3458f2f 100644 --- a/src/sshare/main.py +++ b/src/sshare/main.py @@ -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 diff --git a/src/sshare/plugins/config.py b/src/sshare/plugins/config.py index 446bca2..a6e503a 100644 --- a/src/sshare/plugins/config.py +++ b/src/sshare/plugins/config.py @@ -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( diff --git a/src/sshare/plugins/default/file.py b/src/sshare/plugins/default/file.py index 9676bef..74d9b6a 100644 --- a/src/sshare/plugins/default/file.py +++ b/src/sshare/plugins/default/file.py @@ -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}'")