Evaluate_template's "path" mode


I’m wondering what your thoughts are on giving plugins a way to control the ‘path’ field passed to evaluate_template in destination(). I was experimenting with a plugin and found myself wanting to have a field return not just one element of a path format, but an entire section of a path, and that’s not possible to due path mode’s replacement of / with _. I can see that being the default behavior for the stock fields, but it’d be nice for a plugin to be able to control it on a per-field basis.


Could you perhaps elaborate a bit more on what you’re trying to do that would need a change like that?

1 Like


diff --git a/Beets/config.yaml b/Beets/config.yaml
index ac222fb..8f45cb2 100644
--- a/Beets/config.yaml
+++ b/Beets/config.yaml
@@ -56,6 +56,7 @@ set_fields:
   source: 'Unknown'
+  - format_fields
   - alternatives
   - badfiles
   - chroma
@@ -173,33 +174,43 @@ item_fields:
       return title
+# Aliases for format strings / path formats
+  item_fields:
+    # cli usage
+    if_singletrack_path: '%if{$single_track,$singletrack_path}'
+    # path formats
+    singletrack_path: 'Single Tracks/$artist - $title'
+    alternatives_default_path: '%if{$single_track,$singletrack_path,%if{$missing,Incomplete ,}Albums/$artist_album_track}'
+    nonmusic_path: 'Non-Music/$genre/$artist_album_track'
+    artist_album_track: '$comp_albumartist/$album_track'
+    album_track: '$album%aunique{}%if{$multidisc, Disc $disc}%if{$disctitle,: $disctitle}/$track_pre%if{$comp,$artist - $title,$title}'
     directory: ../DAP
-      default: '%if{$single_track,Single Tracks/$artist - $title,%if{$missing,Incomplete ,}Albums/$comp_albumartist/$album%aunique{}%if{$multidisc, Disc $disc}%if{$disctitle,: $disctitle}/$track_pre%if{$comp,$artist - $title,$title}}'
+      default: '$alternatives_default_path'
+      genre:Speech: '$nonmusic_path'
+      genre:Meditation: '$nonmusic_path'
+      genre:Dharma: '$nonmusic_path'
+      genre:Book: '$nonmusic_path'
+      genre:Spoken: '$nonmusic_path'
-      genre:Speech: 'Non-Music/$genre/$comp_albumartist/$album%aunique{}%if{$multidisc, Disc $disc}%if{$disctitle,: $disctitle}/$track_pre%if{$comp,$artist - $title,$title}'
-      genre:Meditation: 'Non-Music/$genre/$comp_albumartist/$album%aunique{}%if{$multidisc, Disc $disc}%if{$disctitle,: $disctitle}/$track_pre%if{$comp,$artist - $title,$title}'
-      genre:Dharma: 'Non-Music/$genre/$comp_albumartist/$album%aunique{}%if{$multidisc, Disc $disc}%if{$disctitle,: $disctitle}/$track_pre%if{$comp,$artist - $title,$title}'
-      genre:Book: 'Non-Music/$genre/$comp_albumartist/$album%aunique{}%if{$multidisc, Disc $disc}%if{$disctitle,: $disctitle}/$track_pre%if{$comp,$artist - $title,$title}'
-      genre:Spoken: 'Non-Music/$genre/$comp_albumartist/$album%aunique{}%if{$multidisc, Disc $disc}%if{$disctitle,: $disctitle}/$track_pre%if{$comp,$artist - $title,$title}'

     query: ''
     formats: link

Obviously this exact example isn’t a great one, since those genres can be consolidated with a regex query, but I have a long list of others that aren’t in this snippet. I’m sure other use cases exist, this is just the one I happened to try to do to make my config.yaml a bit more readable.

How about using “or” queries instead to use your “non-music” path for all of those genres?

Its true, I’ll likely use that, it’s good idea, thanks. There’s still quite a lot of common components that could be shared, but which have slight differences, though. Different toplevel directory, but the rest is identical, for example.

Interesting! You could consider defining a special field just for that top-level directory that inserts “Non-Album,” for example, depending on the genre.

It’s a fair point, I can likely even leverage this format_fields plugin still to ease use of format strings (since it’s not particularly easy to use them from the inline plugin right now) and just use them on a per-path-component basis. I’ll give that a go and see how it goes. Thanks!

Of course, I could also shift the responsibility for some of this logic out of the path formats by explicitly setting fields based on those queries and then formatting based on the new field. I.e. explicitly set albumtype to spokenword for some of the non-musicbrainz-matched content. Maintaining that will get easier when the issue relating to automatically setting tags is resolved.

I’ll set this approach aside for now. Thanks again. Beets is awesome, by the way. I was able to replace an ungodly amount of shell scripts for maintaining my library for my digital audio player with beets + beets-alternatives.

1 Like