Start with generic negromate command with entry points for other modules
This commit is contained in:
parent
373b12bacd
commit
68d20da4cc
|
@ -0,0 +1,71 @@
|
|||
import argparse
|
||||
import sys
|
||||
try:
|
||||
from importlib import metadata
|
||||
except ImportError: # Python <3.8
|
||||
import importlib_metadata as metadata
|
||||
|
||||
|
||||
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.
|
||||
|
||||
Minimal module example:
|
||||
|
||||
# hello_world.py
|
||||
name = 'hello'
|
||||
help_text = 'Sample command'
|
||||
|
||||
def options(parser):
|
||||
parser.add_argument(
|
||||
'-w', '--who', default='World'
|
||||
help="Who to say hello, defaults to 'World'"
|
||||
)
|
||||
|
||||
def run(args):
|
||||
print("Hello {}".format(args.who))
|
||||
|
||||
To add more commands to negromate register 'negromate.commands'
|
||||
entry point in setup.cfg. For example:
|
||||
|
||||
[options.entry_points]
|
||||
negromate.commands =
|
||||
hello = negromate.web.commands.hello_world
|
||||
|
||||
"""
|
||||
commands = []
|
||||
# clean sys.argv for any module imported here
|
||||
# Yes Kivy, I'm looking at you
|
||||
args = sys.argv.copy()
|
||||
sys.argv = args[:1]
|
||||
|
||||
entry_points = metadata.entry_points().get('negromate.commands', [])
|
||||
for entry_point in entry_points:
|
||||
commands.append(entry_point.load())
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.set_defaults(command=None)
|
||||
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(args[1:])
|
||||
if args.command is None:
|
||||
parser.print_usage()
|
||||
else:
|
||||
for command in commands:
|
||||
if args.command == command.name:
|
||||
command.run(args)
|
|
@ -1,11 +1,20 @@
|
|||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
from .loader import load_songs
|
||||
from ..loader import load_songs
|
||||
|
||||
name = 'songs'
|
||||
help_text = 'Update song database'
|
||||
|
||||
|
||||
def main():
|
||||
songs, pending_songs = load_songs(Path(sys.argv[1]))
|
||||
def options(parser):
|
||||
parser.add_argument(
|
||||
'song_folder', type=Path,
|
||||
help="Folder with the song database.")
|
||||
|
||||
|
||||
def run(args):
|
||||
songs, pending_songs = load_songs(args.song_folder)
|
||||
print(
|
||||
"#######\n"
|
||||
" Songs\n"
|
|
@ -0,0 +1,22 @@
|
|||
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)
|
|
@ -22,13 +22,17 @@ packages = find:
|
|||
zip_safe = true
|
||||
python_requires = >= 3.4
|
||||
install_requires =
|
||||
importlib_metadata
|
||||
webvtt-py
|
||||
asstosrt ==0.1.6
|
||||
srt ==1.6.0
|
||||
|
||||
[options.entry_points]
|
||||
console_scripts =
|
||||
negromate-songs = negromate.songs.command:main
|
||||
negromate = negromate.songs.commands:main
|
||||
negromate.commands =
|
||||
songs = negromate.songs.commands.songs
|
||||
thumbnail = negromate.songs.commands.thumbnail
|
||||
|
||||
[bdist_wheel]
|
||||
# This flag says to generate wheels that support both Python 2 and Python
|
||||
|
|
Loading…
Reference in New Issue