From f5430f4a7f9a23cfb34686f8ab1cb5c04496c6f4 Mon Sep 17 00:00:00 2001 From: Thomas Adamcik Date: Mon, 30 Dec 2013 21:47:02 +0100 Subject: [PATCH] local: Move --clear to it's own sub-command. Split library setup out into a helper and move the clear option to a command. --- docs/commands/mopidy.rst | 4 +++ mopidy/backends/local/commands.py | 53 ++++++++++++++++++++----------- 2 files changed, 38 insertions(+), 19 deletions(-) diff --git a/docs/commands/mopidy.rst b/docs/commands/mopidy.rst index 44e961e6..49c7b5b9 100644 --- a/docs/commands/mopidy.rst +++ b/docs/commands/mopidy.rst @@ -83,6 +83,10 @@ Additionally, extensions can provide extra commands. Run `mopidy --help` for a list of what is available on your system and command-specific help. Commands for disabled extensions will be listed, but can not be run. +.. cmdoption:: local clear + + Clear local media files from the local library. + .. cmdoption:: local scan Scan local media files present in your library. diff --git a/mopidy/backends/local/commands.py b/mopidy/backends/local/commands.py index 8eaf9d0d..e4075fc5 100644 --- a/mopidy/backends/local/commands.py +++ b/mopidy/backends/local/commands.py @@ -13,19 +13,49 @@ from . import translator logger = logging.getLogger('mopidy.backends.local.commands') +def _get_library(args, config): + libraries = dict((l.name, l) for l in args.registry['local:library']) + library_name = config['local']['library'] + + if library_name not in libraries: + logger.warning('Local library %s not found', library_name) + return 1 + + logger.debug('Using %s as the local library', library_name) + return libraries[library_name](config) + + class LocalCommand(commands.Command): def __init__(self): super(LocalCommand, self).__init__() self.add_child('scan', ScanCommand()) + self.add_child('clear', ClearCommand()) + + +class ClearCommand(commands.Command): + help = 'Clear local media files from the local library.' + + def run(self, args, config): + library = _get_library(args, config) + prompt = 'Are you sure you want to clear the library? [y/N] ' + + if raw_input(prompt).lower() != 'y': + logging.info('Clearing library aborted.') + return 0 + + if library.clear(): + logging.info('Library succesfully cleared.') + return 0 + + logging.warning('Unable to clear library.') + return 1 class ScanCommand(commands.Command): - help = "Scan local media files and populate the local library." + help = 'Scan local media files and populate the local library.' def __init__(self): super(ScanCommand, self).__init__() - self.add_argument('--clear', action='store_true', dest='clear', - help='Clear out library storage') self.add_argument('--limit', action='store', type=int, dest='limit', default=0, help='Maxmimum number of tracks to scan') @@ -37,22 +67,7 @@ class ScanCommand(commands.Command): excluded_file_extensions = set( file_ext.lower() for file_ext in excluded_file_extensions) - libraries = dict((l.name, l) for l in args.registry['local:library']) - library_name = config['local']['library'] - - if library_name not in libraries: - logger.warning('Local library %s not found', library_name) - return 1 - - library = libraries[library_name](config) - logger.debug('Using %s as the local library', library_name) - - if args.clear: - if library.clear(): - logging.info('Library succesfully cleared.') - return 0 - logging.warning('Unable to clear library.') - return 1 + library = _get_library(args, config) uri_path_mapping = {} uris_in_library = set()