Fix #242: can now provide multiple paths at once when importing
This commit is contained in:
parent
b0430c3884
commit
3a2e6c3b6d
|
@ -13,7 +13,7 @@ class Command(BaseCommand):
|
||||||
help = "Import audio files mathinc given glob pattern"
|
help = "Import audio files mathinc given glob pattern"
|
||||||
|
|
||||||
def add_arguments(self, parser):
|
def add_arguments(self, parser):
|
||||||
parser.add_argument("path", type=str)
|
parser.add_argument("path", nargs="+", type=str)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--recursive",
|
"--recursive",
|
||||||
action="store_true",
|
action="store_true",
|
||||||
|
@ -65,10 +65,13 @@ class Command(BaseCommand):
|
||||||
|
|
||||||
def handle(self, *args, **options):
|
def handle(self, *args, **options):
|
||||||
glob_kwargs = {}
|
glob_kwargs = {}
|
||||||
|
matching = []
|
||||||
if options["recursive"]:
|
if options["recursive"]:
|
||||||
glob_kwargs["recursive"] = True
|
glob_kwargs["recursive"] = True
|
||||||
try:
|
try:
|
||||||
matching = sorted(glob.glob(options["path"], **glob_kwargs))
|
for import_path in options["path"]:
|
||||||
|
matching += glob.glob(import_path, **glob_kwargs)
|
||||||
|
matching = sorted(list(set(matching)))
|
||||||
except TypeError:
|
except TypeError:
|
||||||
raise Exception("You need Python 3.5 to use the --recursive flag")
|
raise Exception("You need Python 3.5 to use the --recursive flag")
|
||||||
|
|
||||||
|
@ -109,7 +112,7 @@ class Command(BaseCommand):
|
||||||
"No superuser available, please provide a --username"
|
"No superuser available, please provide a --username"
|
||||||
)
|
)
|
||||||
|
|
||||||
filtered = self.filter_matching(matching, options)
|
filtered = self.filter_matching(matching)
|
||||||
self.stdout.write("Import summary:")
|
self.stdout.write("Import summary:")
|
||||||
self.stdout.write(
|
self.stdout.write(
|
||||||
"- {} files found matching this pattern: {}".format(
|
"- {} files found matching this pattern: {}".format(
|
||||||
|
@ -153,7 +156,7 @@ class Command(BaseCommand):
|
||||||
"For details, please refer to import batch #{}".format(batch.pk)
|
"For details, please refer to import batch #{}".format(batch.pk)
|
||||||
)
|
)
|
||||||
|
|
||||||
def filter_matching(self, matching, options):
|
def filter_matching(self, matching):
|
||||||
sources = ["file://{}".format(p) for p in matching]
|
sources = ["file://{}".format(p) for p in matching]
|
||||||
# we skip reimport for path that are already found
|
# we skip reimport for path that are already found
|
||||||
# as a TrackFile.source
|
# as a TrackFile.source
|
||||||
|
|
|
@ -103,6 +103,18 @@ def test_in_place_import_only_from_music_dir(factories, settings):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_import_with_multiple_argument(factories, mocker):
|
||||||
|
factories["users.User"](username="me")
|
||||||
|
path1 = os.path.join(DATA_DIR, "dummy_file.ogg")
|
||||||
|
path2 = os.path.join(DATA_DIR, "utf8-éà◌.ogg")
|
||||||
|
mocked_filter = mocker.patch(
|
||||||
|
"funkwhale_api.providers.audiofile.management.commands.import_files.Command.filter_matching",
|
||||||
|
return_value=({"new": [], "skipped": []}),
|
||||||
|
)
|
||||||
|
call_command("import_files", path1, path2, username="me", interactive=False)
|
||||||
|
mocked_filter.assert_called_once_with([path1, path2])
|
||||||
|
|
||||||
|
|
||||||
def test_import_files_creates_a_batch_and_job(factories, mocker):
|
def test_import_files_creates_a_batch_and_job(factories, mocker):
|
||||||
m = mocker.patch("funkwhale_api.music.tasks.import_job_run")
|
m = mocker.patch("funkwhale_api.music.tasks.import_job_run")
|
||||||
user = factories["users.User"](username="me")
|
user = factories["users.User"](username="me")
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Command line import now accepts unlimited args (#242)
|
Loading…
Reference in New Issue