]> git.draconx.ca Git - picard-plugins.git/blob - tweak-filename-filter.py
5777bb7757bee59fd73d920fe8455e5d62196133
[picard-plugins.git] / tweak-filename-filter.py
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright © 2018 Nick Bowler
4 #
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.
8
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>
14
15 <dl>
16
17 <dt>tweak_file_replace_backslash (bool)</dt>
18 <dd>if true, backslashes will be replaced when renaming files. (default:
19 true)</dd>
20
21 <dt>tweak_file_replacement_char (string)</dt>
22 <dd>matching characters will be replaced by this string (default: _)</dd>
23
24 </dl>
25 '''
26 PLUGIN_VERSION = "0.1"
27 PLUGIN_API_VERSIONS = ["1.0", "2.0"]
28 PLUGIN_LICENSE = "GPL-3.0-or-later"
29
30 from picard import (config, log)
31 import picard.util
32 import re
33
34 def modulename():
35     return modulename.__module__[len("picard.plugins."):]
36
37 # Hook picard.util.sanitize_filename by replacing the underying re object.
38 class SanitizeHook(object):
39     def __init__(self):
40         if config.setting["tweak_file_replace_backslash"]:
41             self.re_match = re.compile(r'[\\/]', re.UNICODE)
42         else:
43             self.re_match = re.compile(r'[/]', re.UNICODE)
44
45     def sub(self, repl, string):
46         ret = self.re_match.sub(
47                  config.setting["tweak_file_replacement_char"],
48                  string)
49         return ret
50
51
52 def install_tweaker():
53     picard.util._re_slashes = SanitizeHook()
54     log.info("%s activated" % (modulename()))
55
56 config.TextOption("setting", "tweak_file_replacement_char", "_"),
57 config.BoolOption("setting", "tweak_file_replace_backslash", True),
58
59 if modulename() in config.setting["enabled_plugins"]:
60     install_tweaker()
61 else:
62     log.debug("%s disabled in configuration" % (modulename()))