Separate File In Plugin

Trying to create a genre’s plugin and need to store a large list of genre’s a separate file. A json file would be optimimal but when I add the json file to a zip with the plugin it tells the the plugin failed. Is this not supported? Something like this

import json

with open('accepted_genres.json') as f:
  fields = json.load(f)

No, since the file is inside the ZIP you would need to read the file from the zip file, e.g. by using the zipfile python module. To support both plugin as folder and as zip you need to detect whether the plugin is loaded from a zip or not.

Unfortunately there is no easy way to do all that. But see https://github.com/phw/picard-plugins/blob/opencc/plugins/opencc/opencc.py#L42 where I tried to solve a similar issue.

To better solve this Picard would either need to fully extract all the plugins or maybe it could offer a helper function to read a file from a plugin, abstracting away the details on whether the plugin is zipped or not.

1 Like

Can I at least add it in a function in a separate & treat it like a dictionary from a module?

Yes, of course. You don’t need to put it into a function, though. Just put the data you need in a dictionary in a separate file and import it. I don’t exactly know how your data looks like, but you can e.g. create a file genres.py:

GENRES = {
    # Your data goes here
}

And the you can import GENRES in your __init.py__

from picard.plugins.yourplugin.genres import GENRES
2 Likes