local: Update scanner to use new library interface.

This commit is contained in:
Thomas Adamcik 2013-12-24 00:57:57 +01:00
parent ff57439995
commit ba642aa680

View File

@ -29,25 +29,24 @@ class ScanCommand(commands.Command):
excluded_file_extensions = set( excluded_file_extensions = set(
file_ext.lower() for file_ext in excluded_file_extensions) file_ext.lower() for file_ext in excluded_file_extensions)
# TODO: select updater / library to use by name libraries = dict((l.name, l) for l in args.registry['local:library'])
updaters = args.registry['local:library'] library_name = config['local']['library']
if not updaters:
logger.error('No usable library updaters found.') if library_name not in libraries:
logger.warning('Local library %s not found', library_name)
return 1 return 1
elif len(updaters) > 1:
logger.error('More than one library updater found. ' library = libraries[library_name](config)
'Provided by: %s', ', '.join(updaters)) logger.debug('Using %s as the local library', library_name)
return 1
local_updater = updaters[0](config)
uri_path_mapping = {} uri_path_mapping = {}
uris_in_library = set() uris_in_library = set()
uris_to_update = set() uris_to_update = set()
uris_to_remove = set() uris_to_remove = set()
tracks = local_updater.load() num_tracks = library.load()
logger.info('Checking %d tracks from library.', len(tracks)) logger.info('Checking %d tracks from library.', num_tracks)
for track in tracks: for track in library.tracks():
uri_path_mapping[track.uri] = translator.local_track_uri_to_path( uri_path_mapping[track.uri] = translator.local_track_uri_to_path(
track.uri, media_dir) track.uri, media_dir)
try: try:
@ -61,7 +60,7 @@ class ScanCommand(commands.Command):
logger.info('Removing %d missing tracks.', len(uris_to_remove)) logger.info('Removing %d missing tracks.', len(uris_to_remove))
for uri in uris_to_remove: for uri in uris_to_remove:
local_updater.remove(uri) library.remove(uri)
logger.info('Checking %s for unknown tracks.', media_dir) logger.info('Checking %s for unknown tracks.', media_dir)
for relpath in path.find_files(media_dir): for relpath in path.find_files(media_dir):
@ -85,7 +84,7 @@ class ScanCommand(commands.Command):
try: try:
data = scanner.scan(path.path_to_uri(uri_path_mapping[uri])) data = scanner.scan(path.path_to_uri(uri_path_mapping[uri]))
track = scan.audio_data_to_track(data).copy(uri=uri) track = scan.audio_data_to_track(data).copy(uri=uri)
local_updater.add(track) library.add(track)
logger.debug('Added %s', track.uri) logger.debug('Added %s', track.uri)
except exceptions.ScannerError as error: except exceptions.ScannerError as error:
logger.warning('Failed %s: %s', uri, error) logger.warning('Failed %s: %s', uri, error)
@ -93,7 +92,7 @@ class ScanCommand(commands.Command):
progress.increment() progress.increment()
logger.info('Commiting changes.') logger.info('Commiting changes.')
local_updater.commit() library.commit()
return 0 return 0