Added latest plugin in examples
- 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:
parent
987eddbf00
commit
2df010d55e
4 changed files with 75 additions and 24 deletions
57
examples/latest.py
Normal file
57
examples/latest.py
Normal file
|
@ -0,0 +1,57 @@
|
|||
# This file is part of SSHare.
|
||||
#
|
||||
# SSHare is free software: you can redistribute it and/or modify it under the terms of
|
||||
# the GNU General Public License as published by the Free Software Foundation,
|
||||
# either version 3 of the License, or (at your option) any later version.
|
||||
#
|
||||
# SSHare is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
# WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
# more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License along with
|
||||
# SSHare. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
from plugins.config import Argument
|
||||
from plugins.config import Flag
|
||||
from plugins.config import NoDefault
|
||||
from plugins.source import File
|
||||
|
||||
plugin_type = "source"
|
||||
|
||||
activate = [ "directory" ]
|
||||
config = {
|
||||
"directory": NoDefault
|
||||
}
|
||||
args = {
|
||||
"directory": Argument(
|
||||
short="l",
|
||||
long="latest",
|
||||
nargs="?",
|
||||
const=Flag,
|
||||
help="Upload the latest file from a directory",
|
||||
)
|
||||
}
|
||||
|
||||
def init():
|
||||
directory = Path(config.directory)
|
||||
if not directory.is_dir():
|
||||
logger.fatal(f"Error: 'plugins.latest.directory => {config.directory}' is not a directory")
|
||||
config.directory = directory
|
||||
|
||||
def source():
|
||||
key = os.path.getmtime
|
||||
files = config.directory.iterdir()
|
||||
selection = next(files)
|
||||
selection_key = key(selection)
|
||||
for file in files:
|
||||
file = file.as_posix()
|
||||
new_key = key(file)
|
||||
if new_key > selection_key:
|
||||
selection = file
|
||||
selection_key = new_key
|
||||
logger.info(f"Uploading file '{selection}'")
|
||||
return File(selection)
|
|
@ -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
|
||||
|
|
|
@ -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(
|
||||
|
|
|
@ -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}'")
|
||||
|
|
Loading…
Reference in a new issue