Question

As an exercise for learning, I am trying to write a plugin for MusicBrainz that matches the albumartistsort to albumartist and artistsort to artist, as opposed to the (apparently) default of Last Name, First Name format it is currently using.

I am just learning about Python and therefore I am trying to use another plugin as a guide, but some important changes need to be done and that's where I probably screwed up.

When I try installing the plug in, it doesn't appear in the plugin list although it is copied to the plugin folder; and the .pyo file is not generated. I am guessing this is due to a compilation error, but I haven't been able to include whatever I need so I can use the picard module (don't know where to find it nor import it) so I can test in my python interpreter.

This is the code I have:

PLUGIN_NAME = "Sort Artist and Album Artist"
PLUGIN_AUTHOR = "Kevin Hernandez"
PLUGIN_DESCRIPTION = "Sorts artist/album artist by name as in Artist/Album Artist field instead of Last, First"
PLUGIN_VERSION = "0.1"
PLUGIN_API_VERSIONS = ["0.9.0", "0.10", "0.15", "0.16"]

from picard.metadata import register_album_metadata_processor
import re

def copy_albumartist_to_albumartistsort(tagger, metadata, release):
  match = re.search($not($eq(metadata["albumartistsort"],metadata["albumartist"])))
    if match:
        metadata["albumartistsort"] = metadata["albumartist"]

def copy_artist_to_artistsort(tagger, metadata, release):
  match = re.search($not($eq(metadata["artistsort"],metadata["artist"])))
    if match:
        metadata["artistsort"] = metadata["artist"]

register_album_metadata_processor(copy_albumartist_to_albumartistsort)
register_album_metadata_processor(copy_artist_to_artistsort)

and I also tried defining the functions as:

def copy_albumartist_to_albumartistsort(tagger, metadata, release):
  metadata["albumartistsort"] = metadata["albumartist"]

def copy_artist_to_artistsort(tagger, metadata, release):
  metadata["artistsort"] = metadata["artist"]

I must point out that I don't fully understand when these are called. I believe the plugin documentation here, here and here is not enough to follow the plugins they have there (e.g. the search and match methods they use in different plugins with re are not explained in the documentation links I am referring to.

If there's a more thorough documentation for it, you can pinpoint what I am doing wrong in my code, or know how to include the picard module in an interpreter (where to find it AND how to include it), then your comments are very much appreciated and valid answers to this question.

Was it helpful?

Solution

I think your biggest problem is that you're mixing up the plugin API with the tagger scripting language.

Tagger scripts are written in a simple custom language; plugins are written in Python. You can't mix and match syntax between the two languages. In particular:

match = re.search($not($eq(metadata["albumartistsort"],metadata["albumartist"])))

That $not, $eq, etc. doesn't mean anything in Python. If you want to check whether things are equal, you use the == operator. If you want to use re.search, you use regular expression syntax. And so on.

Also, your code has to be valid Python, with valid indentation. Your code, at least as posted here.


But let's go through your questions one by one:

I am trying to write a plugin for MusicBrainz that matches the albumartistsort to albumartist and artistsort to artist, as opposed to the (apparently) default of Last Name, First Name format it is currently using.

There are very few automatic defaults in MusicBrainz. Each artist has a name and a sort name, in the database, entered by a human user and verified by other users. You can see this from the web interface. For example, go to David Bowie, and in the "Artist Information" panel on the right side, you'll see "Sort name: Bowie, David". If you're not used to using MusicBrainz's web interface, you should explore it before trying to expand Picard.

When I try installing the plug in, it doesn't appear in the plugin list although it is copied to the plugin folder; and the .pyo file is not generated. I am guessing this is due to a compilation error

Yep. If you run Picard from the command-line with the -d flag, it will show you the errors instead of just silently disabling your plugin, so you don't have to guess. This is documented under Troubleshooting. (If you're on a Mac, the path will be something like /Applications/MusicBrainz Picard.app/Contents/MacOS/MusicBrainz Picard; I think the docs don't explain that because it's standard OS X app bundle stuff.)

but I haven't been able to include whatever I need so I can use the picard module (don't know where to find it nor import it) so I can test in my python interpreter.

You really can't test it in your interpreter. Picard bundles its own custom-built Python interpreter, rather than using your system Python. In that custom interpreter, the picard package is on the sys.path, but in your system Python interpreter, it's not. And trying to import that package and use things out of it while not actually running the Picard GUI wouldn't be a good idea anyway.

If you really want to explore what's in the picard package, download the source and run a local build of the code. But you really shouldn't need to do that. You shouldn't need any functions beyond those documented in the API, and if you want to debug things, you want to debug them in the right context, which generally means adding print functions and/or using the logging module in your code.


I must point out that I don't fully understand when these are called.

At some point after each album is downloaded from the MusicBrainz server, all registered album processor functions get called with the album, and all registered track processor function get called with each track on the album.

Notice that an album processor isn't going to be able to change track-level fields like the track artist sort; you will need a track processor for that.


e.g. the search and match methods they use in different plugins with re are not explained in the documentation links I am referring to.

That's because they're part of the Python standard library, which is documented as part of the standard Python docs—in this case, see re.

You're expected to know the basics of Python before being able to write a Picard plugin.

Meanwhile, I'm not sure what you were trying to write here, but it looks like a really convoluted attempt to say "if these two fields aren't equal, make them equal". Which does the same thing as "unconditionally make them equal". So, why even bother with the regexp and the if condition?


So, your functions could be simplified to:

def copy_albumartist_to_albumartistsort(tagger, metadata, release):
    metadata["albumartistsort"] = metadata["albumartist"]

def copy_artist_to_artistsort(tagger, metadata, release, track):
    metadata["artistsort"] = metadata["artist"]

register_album_metadata_processor(copy_albumartist_to_albumartistsort)
register_track_metadata_processor(copy_artist_to_artistsort)

However, you really don't need a plugin here at all. You should be able to write this whole thing as a trivial tagger script:

$set(artistsort,%artist%)
$set(albumartistsort,%albumartist%)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top