Comments Field Template - dev questions

I’m working on a plugin that allows for a template to be used to define how the comments field will be filled. For example a template such as [$rating] %upper{%left{$context, 3}} $tags ::: $comments would lead to a comments field like [4] CHI #acid,#vocal ::: blah blah blah. The reason is outside of beets in software for DJing I obviously can’t see these flexible attributes and I think the comments field made the most sense to add these other details. If there is a better way do let me know.

It’s my first time digging through beets so I’m having a bit of trouble following all the logic but I’ve learned a few things too. I’ve also realized my initial configuration concept is not going to work or would be more complex than necessary. I have two main questions at this point that could help me.

First, I want to re-use some of the classes in beets for path configuration or for the format_item configuration for my purpose to construct a comments field. I have already found beets.utils.functemplate.Template which has the following odd behaviour I would like to understand.

tmpl('$rating %upper{$title}')
s1 = template.substitute(item)
s2 = template.substitute(item, item._template_funcs())
assert s1 == '3 %upper{My Title}'
assert s2 == '3 MY TITLE'

What is the deal with _template_funcs and why does it need to be called for functions to be substituted in the template when _template_funcs is not an implemented function as seen in beets.dbcore.db.py:264?

Second, is there not a hook in beets to see what fields are being modified prior to the actual modification of the item class? So I could examine the changes to the item and if the item makes a change to the comments or a field I defined in my comments template I would have the comments field rewritten to follow the template instead of rewriting the comments field every time an item is modified.

Thank you.

Woohoo; looks line an awesome plugin!

The idea is that the templating library itself has no built-in fields or functions. So it contains the logic for invoking functions but doesn’t know anything about %upper per se, nor any other function. _template_funcs is implemented here:

And provides the list of template functions defined here:

But template functions can also come from plugins.

There’s no event exactly like that (intercept all field updates on a model object), but there is a write object that lets you change an Item before it gets written to disk.