From ccbca5c79d6cad2c8acf0dc090d5d444de56038f Mon Sep 17 00:00:00 2001 From: shagi Date: Tue, 3 Nov 2020 18:35:27 +0100 Subject: [PATCH] Add run and thumbnails commands --- negromate/web/builder.py | 2 +- negromate/web/commands.py | 11 --------- negromate/web/commands/__init__.py | 38 +++++++++++++++++++++++++++++ negromate/web/commands/build.py | 21 ++++++++++++++++ negromate/web/commands/run.py | 26 ++++++++++++++++++++ negromate/web/commands/thumbnail.py | 23 +++++++++++++++++ 6 files changed, 109 insertions(+), 12 deletions(-) delete mode 100644 negromate/web/commands.py create mode 100644 negromate/web/commands/__init__.py create mode 100644 negromate/web/commands/build.py create mode 100644 negromate/web/commands/run.py create mode 100644 negromate/web/commands/thumbnail.py diff --git a/negromate/web/builder.py b/negromate/web/builder.py index 3ad4e5f..6ae61d5 100644 --- a/negromate/web/builder.py +++ b/negromate/web/builder.py @@ -8,7 +8,7 @@ from jinja2 import Environment, PackageLoader, select_autoescape from jinja2.utils import Markup import srt -from negromate.songs import load_songs +from negromate.songs.loader import load_songs from .karaoke_templates import update_karaoke_songs diff --git a/negromate/web/commands.py b/negromate/web/commands.py deleted file mode 100644 index b962667..0000000 --- a/negromate/web/commands.py +++ /dev/null @@ -1,11 +0,0 @@ -from pathlib import Path - -from .builder import Builder - - -def build(): - builder = Builder( - Path(sys.argv[1]), - Path(sys.argv[2]), - ) - builder.build() diff --git a/negromate/web/commands/__init__.py b/negromate/web/commands/__init__.py new file mode 100644 index 0000000..6edaad3 --- /dev/null +++ b/negromate/web/commands/__init__.py @@ -0,0 +1,38 @@ +import argparse +import sys + +from . import run, build, thumbnail + + +def main(): + """ + Build parser for all the commands and launch appropiate command. + + Each command must be a module with at least the following members: + + * name: String with the command name. Will be used for + argparse subcommand. + * help_text: String with the help text. + * options: Function to build the parser of the command. Takes + one parametter, the argparser parser instance for this + subcommand. + * run: Function that runs the actual command. Takes one + parametter, the Namespace with the arguments. + """ + commands = [ + build, + run, + thumbnail, + ] + + parser = argparse.ArgumentParser() + subparsers = parser.add_subparsers() + for command in commands: + command_parser = subparsers.add_parser(command.name, help=command.help_text) + command_parser.set_defaults(command=command.name) + command.options(command_parser) + + args = parser.parse_args() + for command in commands: + if args.command == command.name: + command.run(args) diff --git a/negromate/web/commands/build.py b/negromate/web/commands/build.py new file mode 100644 index 0000000..eddaf88 --- /dev/null +++ b/negromate/web/commands/build.py @@ -0,0 +1,21 @@ +from pathlib import Path +from ..builder import Builder + + +name = 'build' +help_text = 'Generate static website' + + +def options(parser): + parser.add_argument( + 'song_folder', help="Folder with the song database.") + parser.add_argument( + 'lyrics_file', help="File with the lyrics of the songs.") + + +def run(args): + builder = Builder( + Path(args.song_folder), + Path(args.lyrics_file), + ) + builder.build() diff --git a/negromate/web/commands/run.py b/negromate/web/commands/run.py new file mode 100644 index 0000000..6bf76f5 --- /dev/null +++ b/negromate/web/commands/run.py @@ -0,0 +1,26 @@ +from http.server import test, SimpleHTTPRequestHandler +from functools import partial + + +name = 'run' +help_text = 'Start web server to test the website' + + +def options(parser): + parser.add_argument( + 'song_folder', help="Folder with the song database.") + parser.add_argument( + 'port', action='store', default=8000, type=int, nargs='?', + help='Specify alternate port [default: 8000]') + parser.add_argument( + '--bind', '-b', default='', metavar='ADDRESS', + help='Specify alternate bind address [default: all interfaces]') + + +def run(args): + Handler = partial( + SimpleHTTPRequestHandler, + directory=args.song_folder, + ) + + test(HandlerClass=Handler, port=args.port, bind=args.bind) diff --git a/negromate/web/commands/thumbnail.py b/negromate/web/commands/thumbnail.py new file mode 100644 index 0000000..10e381d --- /dev/null +++ b/negromate/web/commands/thumbnail.py @@ -0,0 +1,23 @@ +import subprocess +from pathlib import Path + +from negromate.songs.utils import generate_cover, generate_thumbnail + +name = 'thumbnail' +help_text = 'Generate cover and thumbnail for a video.' + + +def options(parser): + parser.add_argument( + 'video', help="Video of the song.", type=Path) + parser.add_argument( + 'second', type=int, + help='Take snapshot at this second.') + + +def run(args): + video = args.video + cover = video.parent / 'cover.jpg' + thumbnail = video.parent / 'thumb.jpg' + generate_cover(video, cover, args.second) + generate_thumbnail(cover, thumbnail)