Created
March 31, 2021 20:54
-
-
Save atj/2333e8279bfa194b9ab624c9b637df65 to your computer and use it in GitHub Desktop.
Add a $replacemulti to Picard
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
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 😄
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
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.