Added latest plugin
This commit is contained in:
parent
7e934743c4
commit
3f1ebb2126
2 changed files with 63 additions and 0 deletions
|
@ -2,3 +2,9 @@
|
|||
|
||||
A collection of the plugins I have written for SSHare that I didn't
|
||||
think deserved to be included by default
|
||||
|
||||
## Plugins
|
||||
|
||||
### latest
|
||||
|
||||
Upload the most recently touched file from a directory
|
||||
|
|
57
latest.py
Normal file
57
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)
|
Loading…
Reference in a new issue