]> git.draconx.ca Git - picard-plugins.git/commitdiff
tweak-filename-filter: Update for picard 2.3.1 master
authorNick Bowler <nbowler@draconx.ca>
Sun, 14 Jun 2020 19:49:28 +0000 (15:49 -0400)
committerNick Bowler <nbowler@draconx.ca>
Sun, 14 Jun 2020 19:49:28 +0000 (15:49 -0400)
Recent versions of Picard have changed how slashes are substituted
internally, so we must update the monkey patching in this plugin
accordingly.

tweak-filename-filter.py

index 2d251b2ec29fe29accd09ec51bc3a56d060d5e20..f1ad2dfd221b3aeb0e14562ec056e047fc47b0ea 100644 (file)
@@ -1,6 +1,6 @@
 # -*- coding: utf-8 -*-
 #
-# Copyright © 2018-2019 Nick Bowler
+# Copyright © 2018-2020 Nick Bowler
 #
 # License GPLv3+: GNU General Public License version 3 or any later version.
 # This is free software: you are free to change and redistribute it.
@@ -23,7 +23,7 @@ true)</dd>
 
 </dl>
 '''
-PLUGIN_VERSION = "1"
+PLUGIN_VERSION = "2"
 PLUGIN_API_VERSIONS = ["2.0"]
 PLUGIN_LICENSE = "GPL-3.0-or-later"
 
@@ -120,7 +120,7 @@ class TweakFilenameOptionsPage(OptionsPage):
             self.ui.replace_backslashes.isChecked()
         config.setting["tweak_file_replacement_char"] = \
             self.ui.sanitize_replacement.text()
-        picard.util._re_slashes.__init__()
+        install_tweaker()
 
 # Hook picard.util.sanitize_filename by replacing the underying re object.
 class SanitizeHook(object):
@@ -136,7 +136,20 @@ class SanitizeHook(object):
         return ret
 
 def install_tweaker():
-    picard.util._re_slashes = SanitizeHook()
+    re = SanitizeHook()
+    if not hasattr(picard.util, "_re_slashes"):
+        # Harder to globally monkey patch this picard.util function on newer
+        # Picard, so instead we patch it in the two modules which import it.
+        orig_sanitize_filename = picard.util.sanitize_filename
+        def sanitize_filename_hook(string, **kwargs):
+            return orig_sanitize_filename(re.sub(None, string), **kwargs)
+
+        picard.util.scripttofilename.sanitize_filename = sanitize_filename_hook
+        picard.util.textencoding.sanitize_filename = sanitize_filename_hook
+    else:
+        # On older picard we can monkey patch the underlying re object.
+        picard.util._re_slashes = re
+
     log.info("%s activated" % (modulename()))
 
 if modulename() in config.setting["enabled_plugins"]: