Skip to content

Instantly share code, notes, and snippets.

@atj
Created March 31, 2021 20:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atj/2333e8279bfa194b9ab624c9b637df65 to your computer and use it in GitHub Desktop.
Save atj/2333e8279bfa194b9ab624c9b637df65 to your computer and use it in GitHub Desktop.
Add a $replacemulti to Picard
#!/usr/bin/python
# -*- coding: utf-8 -*-
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
PLUGIN_NAME = "Define a replacemulti function"
PLUGIN_AUTHOR = "atj"
PLUGIN_DESCRIPTION = """Adds a $replacemulti script function."""
PLUGIN_VERSION = "1.0"
PLUGIN_API_VERSIONS = ["2.0"]
PLUGIN_LICENSE = "GPL-2.0"
PLUGIN_LICENSE_URL = "https://www.gnu.org/licenses/gpl-2.0.html"
from picard.metadata import MULTI_VALUED_JOINER
from picard.script import register_script_function
from picard.script.parser import (
MultiValue,
normalize_tagname,
)
def replacemulti(parser, name, item, replacement, separator=MULTI_VALUED_JOINER):
if not name or not replacement or not separator:
return ""
name = normalize_tagname(name)
multi = parser.context.get(name)
if not multi:
return ""
multi = multi.split(separator)
changed = False
for n, value in enumerate(multi):
if value == item:
multi[n] = replacement
changed = True
if changed:
parser.context[name] = separator.join(multi)
return ""
register_script_function(replacemulti)
@Sophist-UK
Copy link

Nice plugin - please submit it to the Plugins repo as a PR.

P.S. It is normal in replacement functions to allow an empty string as the replacement. Yes - I know that there is probably a $delmulti which would do it.

@Extarys
Copy link

Extarys commented Mar 31, 2021

There is no $delmulti function right now.
I didn't know it was "easy" like that to create new function so I might create more to manage multi variables 😄

@Sophist-UK
Copy link

Since these seems to be suitable as core functionality, you might want to consider making it a PR to Picard itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment