local: Move --clear to it's own sub-command.

Split library setup out into a helper and move the clear option to a command.
This commit is contained in:
Thomas Adamcik 2013-12-30 21:47:02 +01:00
parent a462f132d3
commit f5430f4a7f
2 changed files with 38 additions and 19 deletions

View File

@ -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.

View File

@ -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()