Plugin Development help. Simple test plugin not working

Hello all,

I am completely new at writing plugins and have started with the example one provided on the Picard site. No matter what I do, I can’t seem to get anything working nor see any errors in the log window.

Obviously there is something I am missing. If someone could take a look at this, I would really appreciate it. I have a plugin idea that I want to get off the ground but I can’t even get the following working.

PLUGIN_NAME = "Clayton's Tutorial Plugin"
PLUGIN_AUTHOR = "Clayton Mattatall"
PLUGIN_DESCRIPTION = "This plugin is an tutorial"
PLUGIN_VERSION = '0.1'
PLUGIN_API_VERSIONS = ['2.2']
PLUGIN_LICENSE = "GPL-2.0-or-later"
PLUGIN_LICENSE_URL = "https://www.gnu.org/licenses/gpl-2.0.html"

import json
import os

from picard import config, log
from picard.metadata import (register_album_metadata_processor,
                             register_track_metadata_processor)
from picard.plugin import PluginPriority

file_to_write = os.path.join(config.setting["move_files_to"], "data_dump.txt")


def write_line(line_type, object_to_write, dump_json=False, append=True):
    file_mode = 'a' if append else 'w'
    try:
        with open(file_to_write, file_mode, encoding="UTF-8") as f:
            if dump_json:
                f.write('{0} JSON dump follows:\n'.format(line_type, ))
                f.write('{0}\n\n'.format(json.dumps(object_to_write, indent=4)))
            else:
                f.write("{0:s}: {1:s}\n".format(line_type, str(object_to_write), ))
    except Exception as ex:
        log.error("{0}: Error: {1}".format(PLUGIN_NAME, ex, ))


def dump_release_info(album, metadata, release):
    write_line('Release Argument 1 (album)', album)
    write_line('Release Argument 3 (release)', release)


def dump_track_info(album, metadata, track, release):
    write_line('Track Argument 1 (album)', album, dump_json=True)
    write_line('Track Argument 3 (track)', track, dump_json=True)
    write_line('Track Argument 4 (release)', release, dump_json=True)


# Register the plugin to run at a HIGH priority so that other plugins will
# not have an opportunity to modify the contents of the metadata provided.
register_album_metadata_processor(dump_release_info, priority=PluginPriority.HIGH)
register_track_metadata_processor(dump_track_info, priority=PluginPriority.HIGH)

I just can’t figure out why no output file is created and anything written to it

Thanks,
Clayton

1 Like

Are you retrieving an album from MusicBrainz? I believe that’s what triggers the metadata processor plugins.

Also, are your lines following the if statements properly indented? They don’t show that way in your message. If there’s an error loading the plugin, the message should appear in the log during the startup processing (near the start of the log).

Another dumb question, but you are enabling the plugin from the Settings plugin page, right?

As a hint, when entering code in a message here on the community forum try putting a line with three backticks before the first line of code, and another line with three backticks after the last line of code. This will format it as a code block, and will retain all leading spaces and such without wrapping. That makes it a lot easier to read (and see what’s going on). For example, entering:

```
This is a line of code.
{four leading spaces}This line is indented.
```

will display as:

This is a line of code.
    This line is indented.

You can also specify the programming language immediately following the backticks before the code, and the system will try to do proper syntax highlighting.

2 Likes