Fix #120: Better error handling during file import
This commit is contained in:
parent
f1cf250e29
commit
e99d757b57
|
@ -121,7 +121,13 @@ class Metadata(object):
|
||||||
|
|
||||||
def __init__(self, path):
|
def __init__(self, path):
|
||||||
self._file = mutagen.File(path)
|
self._file = mutagen.File(path)
|
||||||
self._conf = CONF[self.get_file_type(self._file)]
|
if self._file is None:
|
||||||
|
raise ValueError('Cannot parse metadata from {}'.format(path))
|
||||||
|
ft = self.get_file_type(self._file)
|
||||||
|
try:
|
||||||
|
self._conf = CONF[ft]
|
||||||
|
except KeyError:
|
||||||
|
raise ValueError('Unsupported format {}'.format(ft))
|
||||||
|
|
||||||
def get_file_type(self, f):
|
def get_file_type(self, f):
|
||||||
return f.__class__.__name__
|
return f.__class__.__name__
|
||||||
|
|
|
@ -34,6 +34,13 @@ class Command(BaseCommand):
|
||||||
default=False,
|
default=False,
|
||||||
help='Will launch celery tasks for each file to import instead of doing it synchronously and block the CLI',
|
help='Will launch celery tasks for each file to import instead of doing it synchronously and block the CLI',
|
||||||
)
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
'--exit', '-x',
|
||||||
|
action='store_true',
|
||||||
|
dest='exit_on_failure',
|
||||||
|
default=False,
|
||||||
|
help='use this flag to disable error catching',
|
||||||
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--no-acoustid',
|
'--no-acoustid',
|
||||||
action='store_true',
|
action='store_true',
|
||||||
|
@ -106,20 +113,27 @@ class Command(BaseCommand):
|
||||||
async = options['async']
|
async = options['async']
|
||||||
import_handler = tasks.import_job_run.delay if async else tasks.import_job_run
|
import_handler = tasks.import_job_run.delay if async else tasks.import_job_run
|
||||||
for path in matching:
|
for path in matching:
|
||||||
job = batch.jobs.create(
|
|
||||||
source='file://' + path,
|
|
||||||
)
|
|
||||||
name = os.path.basename(path)
|
|
||||||
with open(path, 'rb') as f:
|
|
||||||
job.audio_file.save(name, File(f))
|
|
||||||
|
|
||||||
job.save()
|
|
||||||
try:
|
try:
|
||||||
utils.on_commit(
|
self.stdout.write(message.format(path))
|
||||||
import_handler,
|
self.import_file(path, batch, import_handler, options)
|
||||||
import_job_id=job.pk,
|
|
||||||
use_acoustid=not options['no_acoustid'])
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.stdout.write('Error: {}'.format(e))
|
if options['exit_on_failure']:
|
||||||
|
raise
|
||||||
|
m = 'Error while importing {}: {} {}'.format(
|
||||||
|
path, e.__class__.__name__, e)
|
||||||
|
self.stderr.write(m)
|
||||||
return batch
|
return batch
|
||||||
|
|
||||||
|
def import_file(self, path, batch, import_handler, options):
|
||||||
|
job = batch.jobs.create(
|
||||||
|
source='file://' + path,
|
||||||
|
)
|
||||||
|
name = os.path.basename(path)
|
||||||
|
with open(path, 'rb') as f:
|
||||||
|
job.audio_file.save(name, File(f))
|
||||||
|
|
||||||
|
job.save()
|
||||||
|
utils.on_commit(
|
||||||
|
import_handler,
|
||||||
|
import_job_id=job.pk,
|
||||||
|
use_acoustid=not options['no_acoustid'])
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Better error handling during file import (#120)
|
Loading…
Reference in New Issue