Alias plugin

I just threw together a quick little plugin, pretty trivial to implement, but useful to me, and figured it might be of interest to some.

Alias Plugin

This adds support for beets command aliases, not unlike git.

By default, it also checks $PATH for beet-* commands/scripts and makes those available as well.

Example configuration:

alias:
  from_path: True # Default
  aliases:
    singletons: ls singleton:true
    external-cmd-test: '!echo'
    sh-c-test: '!sh -c "echo foo bar arg1:$1, arg2:$2" sh-c-test'

Example of the command listing the available aliases on my system:

$ beet alias
albums-for: !beet-albums-for
external-cmd-test: !echo
items-for: !beet-items-for
merge-albums: !beet-merge-albums
open: !beet-open
sh-c-test: !sh -c "echo foo bar arg1:$1, arg2:$2" sh-c-test
singletons: ls singleton:true

Using a couple aliases:

$ beet singletons | tail -n 1
[15394] The Tiberian Sons - Frank Klepacki & The Tiberian Sons LIVE: OFFICIAL Multi-cam Full Show
$ beet sh-c-test something something-else
foo bar arg1:something, arg2:something-else
$ beet external-cmd-test foo
foo

A less contrived example from my own configuration:

simple-dupe-check: "dup -Fa -k albumartist -k album -k disctotal -k tracktotal -f '[$id] $albumartist - $album - $existing/$albumtotal via $source'"

$ beet simple-dupe-check
[158] Dirty Vegas - Electric Love - 1/8 via Amazon
[624] Dirty Vegas - Electric Love - 1/10 via Google
[213] Langhorne Slim - Be Set Free - 1/1 via Amazon
[1659] Langhorne Slim - Be Set Free - 1/13 via iTunes

And my current entire config:

alias:
  aliases:
    reimport: import -maL
    reimport-mb: reimport mb_albumid::'^$'
    dup-albums: dup -aF
    dup-a: dup-albums
    singletons: ls singleton:true

    # get-config library; get-config alias.aliases.reimport
    get-config: '!sh -c "beet config | yq -r \".$1\"" -'

    # Show incomplete albums, skipping any albums where I only have one track
    incomplete-albums: 'ls -a -f "[$id] $albumartist - $album, $existing/$albumtotal, missing $missing tracks" missing:2.. single_track:false , missing:1 \\^genre:game \\^genre:videogame \\^genre:vgm single_track:false'

    # Very basic dupe check for albums lacking musicbrainz data, primarily
    simple-dupe-check: 'dup -Fa -k albumartist -k album -k disctotal -k tracktotal -f "[$id] $albumartist - $album - $existing/$albumtotal via $source"'

    # Red flags
    empty-artist: ls artist::'^$'
    empty-album: ls album::'^$' singleton:false
    book-not-audiobook: ls genre:book '^albumtype:audiobook'
    unknown-source: source:Unknown

    # Potential concerns
    various-not-comp: ls -a is_various_not_comp:true
    unknown-soundtracks: ls -f '$genre $path' albumtype:soundtrack \^genre:soundtrack \^genre:game

    # Informative
    non-mb-albums: ls -a mb_albumid::^$
    possible-singles: ls -a tracktotal:1 'albumtype::^$'
    # These are often not an issue, just albums which feature multiple
    # artists, but aren't compilations. Cases where the album artist is the
    # producer, cases where there are featuring artist, etc.
    multiartist-not-comp: ls -a comp:false multiartist:true
4 Likes

This looks like a very useful plugin, particularly the way it allows searches to be saved. Would you be interested in contributing it as a core beets plugin? The alternative route I could think of would be some way of configuring named queries using a query syntax extension that could then be used for commands other than beet ls.

I’d certainly be willing to submit it if there’s interest, and it seems there is. I’ll see about cleaning up the code a bit, flesh one or two things out a little more, and submit a pull req. Thanks for the input. Named queries does sound useful as well. I rather like the simplicity and flexibility of the alias-based approach, though, even if it has a bit more overhead via forking off new processes. I’ll see if a plugin can directly execute another Subcommand rather than spawning the extra beet process as an optimization.

1 Like

Turns out aliased queries is fairly handy too, for path_format queries, where aliased commands don’t help, i.e.:

alias_query:
  item_queries:
    is_christmas: 'genre:christmas genre:holiday'
    is_soundtrack: 'genre:soundtrack albumtype:soundtrack'
alternatives:
  dap:
    paths:
      alias:is_christmas: 'Christmas Music/$albumartistdir/$albumdir/$comp_filename'
1 Like