a2ee5c
# -*- coding: utf-8 -*-
a2ee5c
#
a2ee5c
# Picard plugin swapprefix
a2ee5c
# Adds the swapprefix tagger script function.
a2ee5c
# This function offers the same functionality as the one in Foobar2000.
a2ee5c
# See http://wiki.hydrogenaudio.org/index.php?title=Foobar2000:Title_Formatting_Reference
a2ee5c
#
a2ee5c
# Copyright (C) 2010 Philipp Wolfer
a2ee5c
#
a2ee5c
# This program is free software; you can redistribute it and/or
a2ee5c
# modify it under the terms of the GNU General Public License
a2ee5c
# as published by the Free Software Foundation; either version 2
a2ee5c
# of the License, or (at your option) any later version.
a2ee5c
#
a2ee5c
# This program is distributed in the hope that it will be useful,
a2ee5c
# but WITHOUT ANY WARRANTY; without even the implied warranty of
a2ee5c
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
a2ee5c
# GNU General Public License for more details.
a2ee5c
#
a2ee5c
# You should have received a copy of the GNU General Public License
a2ee5c
# along with this program; if not, write to the Free Software
a2ee5c
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
a2ee5c

a2ee5c
PLUGIN_NAME = 'swapprefix function'
a2ee5c
PLUGIN_AUTHOR = 'Philipp Wolfer'
a2ee5c
PLUGIN_DESCRIPTION = 'Moves the specified prefixes to the end of a string.'
a2ee5c
PLUGIN_VERSION = "0.1"
a2ee5c
PLUGIN_API_VERSIONS = ["0.9.0", "0.10", "0.11", "0.12", "0.15"]
a2ee5c

a2ee5c
from picard.script import register_script_function
a2ee5c
import re
a2ee5c

a2ee5c
def swapprefix(parser, text, *prefixes):
a2ee5c
    """
a2ee5c
    Moves the specified prefixes to the end of text.
a2ee5c
    If no prefix is specified 'A' and 'The' are taken
a2ee5c
    as default.
a2ee5c
    """
a2ee5c
    if not prefixes:
a2ee5c
        prefixes = ('A', 'The')
a2ee5c
    for prefix in prefixes:
a2ee5c
        pattern = re.compile('^' + re.escape(prefix) + '\s')
a2ee5c
        match = pattern.match(text)
a2ee5c
        if match:
a2ee5c
            rest = pattern.split(text)[1].strip()
a2ee5c
            if rest:
a2ee5c
                return ", ".join((rest, match.group(0).rstrip()))
a2ee5c
    return text
a2ee5c

a2ee5c
register_script_function(swapprefix)
a2ee5c

a2ee5c