Import custom tag from file into beets, Mediafile plugin

Hi can someone kindly help me get this plugin running?
https://beets.readthedocs.io/en/stable/dev/plugins.html#extend-mediafile
I’m trying to figure out how I can import into beets a custom tag “source” that’s already written to .flac and .mp3 files, to eventually use it in the path as part of the album naming.

I’ve sevral times but I can’t seem to get anything to work, I have no experience with python and just getting beets to run it (by putting it in /usr/lib/python3.11/site-packages/beetsplug/ as the plugin specified in the config seems to be ignored) has taken me ages.
sourceplugin.py:

    def __init__(self):
        field = mediafile.MediaField(
            mediafile.MP3DescStorageStyle(u'source'),
            mediafile.StorageStyle(u'source')
        )
        self.add_media_field('source', field)

This other section I don;t understand at all, why would I specify an .mp3 file and why would I want to write ham coded into the plugin?

item = Item.from_path('/path/to/foo/tag.mp3')
assert item['foo'] == 'spam'

item['foo'] == 'ham'
item.write()
# The "foo" tag of the file is now "ham"

Never mind I figured it out after finding some other similar topics.

But I am still having some troubles.
Import seemed to go well and the field source is read correctly, but when I try to update some files I get:

Sending event: item_moved
Traceback (most recent call last):
  File "/home/user/.local/bin/beet", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/beets/ui/__init__.py", line 1865, in main
    _raw_main(args)
  File "/home/user/.local/lib/python3.11/site-packages/beets/ui/__init__.py", line 1852, in _raw_main
    subcommand.func(lib, suboptions, subargs)
  File "/home/user/.local/lib/python3.11/site-packages/beets/ui/commands.py", line 1734, in update_func
    update_items(
  File "/home/user/.local/lib/python3.11/site-packages/beets/ui/commands.py", line 1686, in update_items
    item.store(fields=item_fields)
  File "/home/user/.local/lib/python3.11/site-packages/beets/library.py", line 395, in store
    super().store(fields)
  File "/home/user/.local/lib/python3.11/site-packages/beets/dbcore/db.py", line 585, in store
    tx.mutate(query, subvars)
  File "/home/user/.local/lib/python3.11/site-packages/beets/dbcore/db.py", line 963, in mutate
    cursor = self.db._connection().execute(statement, subvals)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
sqlite3.OperationalError: no such column: source'''

The plugin:

from beets.plugins import BeetsPlugin
import mediafile

class MyPlugin(BeetsPlugin):
    """Plugin object."""
    def __init__(self):
        """Self definition."""
        super().__init__()
        source = ""
        _source = mediafile.MediaField(
            mediafile.MP3DescStorageStyle('source'),
            mediafile.StorageStyle('source'),
            out_type=str
        )
        self.add_media_field('source', _source)

Another problem I can’t seem to figure out is how to use this field in the path.
This field should be consistent across an album so I tried to include it after the album name: using directly $source seems to work when the value is defined, but returns “$source” when empty, so I tried to define it in the plugin with no result.
Using an %ifdef statement returns always nothing.
I tried to create an album_field for the inline plugin but I can’t seem to define it.

album_fields:  
  sourcepath: source 

Returns error:

Traceback (most recent call last):
  File "/home/user/.local/lib/python3.11/site-packages/beets/util/functemplate.py", line 577, in substitute
    res = self.compiled(values, functions)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/beets/util/functemplate.py", line 601, in wrapper_func
    args[VARIABLE_PREFIX + varname] = values[varname]
                                      ~~~~~~^^^^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/beets/library.py", line 483, in __getitem__
    value = self._get(key)
            ^^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/beets/library.py", line 475, in _get
    raise KeyError(key)
KeyError: 'source'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/user/.local/lib/python3.11/site-packages/beetsplug/inline.py", line 111, in _expr_func
    return eval(code, values)
           ^^^^^^^^^^^^^^^^^^
  File "inline", line 1, in <module>
NameError: name 'asdew' is not defined. Did you mean: 'added'?

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/user/.local/bin/beet", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/beets/ui/__init__.py", line 1865, in main
    _raw_main(args)
  File "/home/user/.local/lib/python3.11/site-packages/beets/ui/__init__.py", line 1852, in _raw_main
    subcommand.func(lib, suboptions, subargs)
  File "/home/user/.local/lib/python3.11/site-packages/beets/ui/commands.py", line 2211, in move_func
    move_items(
  File "/home/user/.local/lib/python3.11/site-packages/beets/ui/commands.py", line 2141, in move_items
    objs = [o for o in objs if (isalbummoved if album else isitemmoved)(o)]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/beets/ui/commands.py", line 2141, in <listcomp>
    objs = [o for o in objs if (isalbummoved if album else isitemmoved)(o)]
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/beets/ui/commands.py", line 2136, in isitemmoved
    return item.path != item.destination(basedir=dest)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/beets/library.py", line 1088, in destination
    subpath = self.evaluate_template(subpath_tmpl, True)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/beets/dbcore/db.py", line 686, in evaluate_template
    return t.substitute(
           ^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/beets/util/functemplate.py", line 579, in substitute
    res = self.interpret(values, functions)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/beets/util/functemplate.py", line 572, in interpret
    return self.expr.evaluate(Environment(values, functions))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/beets/util/functemplate.py", line 260, in evaluate
    out.append(part.evaluate(env))
               ^^^^^^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/beets/util/functemplate.py", line 165, in evaluate
    if self.ident in env.values:
       ^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen _collections_abc>", line 780, in __contains__
  File "/home/user/.local/lib/python3.11/site-packages/beets/library.py", line 483, in __getitem__
    value = self._get(key)
            ^^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/beets/library.py", line 469, in _get
    return self._get_formatted(self.album, key)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/beets/dbcore/db.py", line 134, in _get_formatted
    value = model._type(key).format(model.get(key))
                                    ^^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/beets/dbcore/db.py", line 433, in _get
    return getters[key](self)
           ^^^^^^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/beetsplug/inline.py", line 113, in _expr_func
    raise InlineError(python_code, exc)
beetsplug.inline.InlineError: error in inline path field code:
asdew
NameError: name 'asdew' is not defined
[user@ant library]$ beet mv hypodrome -p
Traceback (most recent call last):
  File "/home/user/.local/lib/python3.11/site-packages/beets/util/functemplate.py", line 577, in substitute
    res = self.compiled(values, functions)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/beets/util/functemplate.py", line 601, in wrapper_func
    args[VARIABLE_PREFIX + varname] = values[varname]
                                      ~~~~~~^^^^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/beets/library.py", line 483, in __getitem__
    value = self._get(key)
            ^^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/beets/library.py", line 475, in _get
    raise KeyError(key)
KeyError: 'source'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/user/.local/lib/python3.11/site-packages/beetsplug/inline.py", line 111, in _expr_func
    return eval(code, values)
           ^^^^^^^^^^^^^^^^^^
  File "inline", line 1, in <module>
NameError: name 'source' is not defined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/user/.local/bin/beet", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/beets/ui/__init__.py", line 1865, in main
    _raw_main(args)
  File "/home/user/.local/lib/python3.11/site-packages/beets/ui/__init__.py", line 1852, in _raw_main
    subcommand.func(lib, suboptions, subargs)
  File "/home/user/.local/lib/python3.11/site-packages/beets/ui/commands.py", line 2211, in move_func
    move_items(
  File "/home/user/.local/lib/python3.11/site-packages/beets/ui/commands.py", line 2141, in move_items
    objs = [o for o in objs if (isalbummoved if album else isitemmoved)(o)]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/beets/ui/commands.py", line 2141, in <listcomp>
    objs = [o for o in objs if (isalbummoved if album else isitemmoved)(o)]
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/beets/ui/commands.py", line 2136, in isitemmoved
    return item.path != item.destination(basedir=dest)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/beets/library.py", line 1088, in destination
    subpath = self.evaluate_template(subpath_tmpl, True)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/beets/dbcore/db.py", line 686, in evaluate_template
    return t.substitute(
           ^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/beets/util/functemplate.py", line 579, in substitute
    res = self.interpret(values, functions)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/beets/util/functemplate.py", line 572, in interpret
    return self.expr.evaluate(Environment(values, functions))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/beets/util/functemplate.py", line 260, in evaluate
    out.append(part.evaluate(env))
               ^^^^^^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/beets/util/functemplate.py", line 165, in evaluate
    if self.ident in env.values:
       ^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen _collections_abc>", line 780, in __contains__
  File "/home/user/.local/lib/python3.11/site-packages/beets/library.py", line 483, in __getitem__
    value = self._get(key)
            ^^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/beets/library.py", line 469, in _get
    return self._get_formatted(self.album, key)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/beets/dbcore/db.py", line 134, in _get_formatted
    value = model._type(key).format(model.get(key))
                                    ^^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/beets/dbcore/db.py", line 433, in _get
    return getters[key](self)
           ^^^^^^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/beetsplug/inline.py", line 113, in _expr_func
    raise InlineError(python_code, exc)
beetsplug.inline.InlineError: error in inline path field code:
source
NameError: name 'source' is not defined

The path:

    albumtype:live: $genre/$albumartist/live/$year-$month-$day - $album %ifdef{$source,($source)} %aunique{albumartist album year, albumdisambig}%if{$albumdisambig,($albumdisambig $year) } %if{$is_1644,,%if{$is_flac,[$format $album_bitdepth-$album_samplerate],[$format $av_bitrate]}}/$disc_and_track. $title