From 6ff26d2bbfc7ade30ee0c3e38682ef689b73bda6 Mon Sep 17 00:00:00 2001 From: Wolfgang Müller Date: Tue, 22 Feb 2022 11:35:39 +0100 Subject: Initial commit --- beetsplug/extras.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 beetsplug/extras.py (limited to 'beetsplug/extras.py') diff --git a/beetsplug/extras.py b/beetsplug/extras.py new file mode 100644 index 0000000..32fe3f6 --- /dev/null +++ b/beetsplug/extras.py @@ -0,0 +1,44 @@ +import os +import shutil +import beets.plugins + +class ExtrasPlugin(beets.plugins.BeetsPlugin): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + self.register_listener('item_moved', self.on_item_moved) + self.register_listener('item_copied', self.on_item_copied) + + def on_item_moved(self, _, source, destination): + for (sourcepath, destpath) in self.gather(source, destination): + os.rename(sourcepath, destpath) + + def on_item_copied(self, _, source, destination): + for (sourcepath, destpath) in self.gather(source, destination): + if os.path.isdir(sourcepath): + shutil.copytree(sourcepath, destpath) + else: + shutil.copy(sourcepath, destpath) + + def gather(self, source, destination): + candidates = [] + sourcedir = os.path.dirname(source) + destdir = os.path.dirname(destination) + + if sourcedir == destdir: + return [] + + paths = [beets.util.bytestring_path(p) for p in self.config['paths'].get()] + + for path in paths: + sourcepath = os.path.join(sourcedir, path) + if not os.path.exists(sourcepath): + continue + + destpath = os.path.join(destdir, path) + if os.path.exists(destpath): + continue + + candidates.append((sourcepath, destpath)) + + return candidates -- cgit v1.2.3-2-gb3c3