1 # -*- coding: utf-8 -*-
3 # Copyright © 2018 Nick Bowler
5 # License GPLv3+: GNU General Public License version 3 or any later version.
6 # This is free software: you are free to change and redistribute it.
7 # There is NO WARRANTY, to the extent permitted by law.
9 PLUGIN_NAME = u"Tweak filename filter"
10 PLUGIN_AUTHOR = u"Nick Bowler"
11 PLUGIN_DESCRIPTION = u'''<p>Adds additional options to tweak file naming.</p>
12 <p>Currently, this overrides the default methods to substitute forward and
13 back-slashes, allowing alternate behaviours.</p>
17 <dt>tweak_file_replace_backslash (bool)</dt>
18 <dd>if true, backslashes will be replaced when renaming files. (default:
21 <dt>tweak_file_replacement_char (string)</dt>
22 <dd>matching characters will be replaced by this string (default: _)</dd>
26 PLUGIN_VERSION = "0.1"
27 PLUGIN_API_VERSIONS = ["1.0", "2.0"]
28 PLUGIN_LICENSE = "GPL-3.0-or-later"
30 from picard import (config, log)
35 return modulename.__module__[len("picard.plugins."):]
37 # Hook picard.util.sanitize_filename by replacing the underying re object.
38 class SanitizeHook(object):
40 if config.setting["tweak_file_replace_backslash"]:
41 self.re_match = re.compile(r'[\\/]', re.UNICODE)
43 self.re_match = re.compile(r'[/]', re.UNICODE)
45 def sub(self, repl, string):
46 ret = self.re_match.sub(
47 config.setting["tweak_file_replacement_char"],
52 def install_tweaker():
53 picard.util._re_slashes = SanitizeHook()
54 log.info("%s activated" % (modulename()))
56 config.TextOption("setting", "tweak_file_replacement_char", "_"),
57 config.BoolOption("setting", "tweak_file_replace_backslash", True),
59 if modulename() in config.setting["enabled_plugins"]:
62 log.debug("%s disabled in configuration" % (modulename()))