Skip to content

Commit

Permalink
Add update --all flag to update all collections add once
Browse files Browse the repository at this point in the history
Closes #77
  • Loading branch information
geigerzaehler committed Mar 8, 2024
1 parent 0830fe1 commit 6348345
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 9 deletions.
3 changes: 1 addition & 2 deletions .flake8
@@ -1,3 +1,2 @@
[flake8]
max-line-length = 88
extend-ignore = E203
extend-ignore = E203, E501
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,6 +1,10 @@
Change Log
==========

## Upcoming
* Add `--all` flag to update command which will update all configured
collections.

## v0.11.0 - 2023-06-06
* Use the convert’s plugin [`thread` configuration][convert-config] when
transcoding files. ([@johnyerhot](https://github.com/johnyerhot))
Expand Down
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -205,7 +205,13 @@ The command accepts the following option.
command will ask you to confirm the creation of the external
collection. These options specify the answer as a cli option.

```plain
beet alt update [--create|--no-create] --all
```

Update all external collections defined in `alternatives` configuration.

```plain
beet alt list-tracks [--format=FORMAT] NAME
```

Expand Down
34 changes: 27 additions & 7 deletions beetsplug/alternatives.py
Expand Up @@ -53,12 +53,21 @@ def __init__(self):
def commands(self):
return [AlternativesCommand(self)]

def update(self, lib: Library, options):
try:
alt = self.alternative(options.name, lib)
except KeyError as e:
raise UserError("Alternative collection '{0}' not found.".format(e.args[0]))
alt.update(create=options.create)
def update(self, lib: Library, options: argparse.Namespace):
if options.name is None:
if not options.all:
raise UserError("Please specify a collection name or the --all flag")

for name in self.config.keys():
self.alternative(name, lib).update(create=options.create)
else:
try:
alt = self.alternative(options.name, lib)
except KeyError as e:
raise UserError(
"Alternative collection '{0}' not found.".format(e.args[0])
)
alt.update(create=options.create)

def list_tracks(self, lib, options):
if options.format is not None:
Expand Down Expand Up @@ -100,11 +109,22 @@ def __init__(self, plugin):

update = subparsers.add_parser("update")
update.set_defaults(func=plugin.update)
update.add_argument("name", metavar="NAME")
update.add_argument(
"name",
metavar="NAME",
nargs="?",
help="Name of the collection. Must be provided unless --all is given",
)
update.add_argument("--create", action="store_const", dest="create", const=True)
update.add_argument(
"--no-create", action="store_const", dest="create", const=False
)
update.add_argument(
"--all",
action="store_true",
default=False,
help="Update all alternative collections that are defined in the configuration",
)

list_tracks = subparsers.add_parser(
"list-tracks",
Expand Down
29 changes: 29 additions & 0 deletions test/cli_test.py
Expand Up @@ -415,6 +415,35 @@ def touch_art(item, image_path):
item = album.items().get()
self.assertHasEmbeddedArtwork(self.get_path(item), self.IMAGE_FIXTURE2)

def test_update_all(self):
dir_a = self.mkdtemp()
dir_b = self.mkdtemp()
self.config["alternatives"] = {
"a": {
"directory": dir_a,
"query": "myexternal:true",
},
"b": {
"directory": dir_b,
"query": "myexternal:true",
},
}

with self.assertRaises(UserError) as c:
self.runcli("alt", "update")
assert str(c.exception) == "Please specify a collection name or the --all flag"

item = self.add_track(title="a", myexternal="true")
self.runcli("alt", "update", "--all")
item.load()
path_a = self.get_path(item, path_key="alt.a")
assert path_a and dir_a in path_a.decode()
self.assertIsFile(path_a)

path_b = self.get_path(item, path_key="alt.b")
assert path_b and dir_b in path_b.decode()
self.assertIsFile(path_b)


class ExternalConvertTest(TestHelper):
"""Test alternatives with non-empty ``format`` option, i.e. transcoding
Expand Down

0 comments on commit 6348345

Please sign in to comment.