From 67ca5d640947f603706d706aa284d2cc7752036b Mon Sep 17 00:00:00 2001 From: Laurent Monin Date: Wed, 5 Jan 2022 15:23:43 +0100 Subject: [PATCH 1/5] Reduce code redundancy --- picard/tagger.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/picard/tagger.py b/picard/tagger.py index 62328a922..acd0b7ea0 100644 --- a/picard/tagger.py +++ b/picard/tagger.py @@ -266,10 +266,12 @@ class Tagger(QtWidgets.QApplication): self.pluginmanager = PluginManager() if not self._no_plugins: if IS_FROZEN: - self.pluginmanager.load_plugins_from_directory(os.path.join(os.path.dirname(sys.argv[0]), "plugins")) + plugin_dir = os.path.join(os.path.dirname(sys.argv[0]), "plugins") else: mydir = os.path.dirname(os.path.abspath(__file__)) - self.pluginmanager.load_plugins_from_directory(os.path.join(mydir, "plugins")) + plugin_dir = os.path.join(mydir, "plugins") + + self.pluginmanager.load_plugins_from_directory(plugin_dir) if not os.path.exists(USER_PLUGIN_DIR): os.makedirs(USER_PLUGIN_DIR) From 29160804c0ae60d6a7b780543fffae2d0936da98 Mon Sep 17 00:00:00 2001 From: Laurent Monin Date: Wed, 5 Jan 2022 15:25:47 +0100 Subject: [PATCH 2/5] Reduce code redundancy, introducing topdir --- picard/tagger.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/picard/tagger.py b/picard/tagger.py index acd0b7ea0..7d170361d 100644 --- a/picard/tagger.py +++ b/picard/tagger.py @@ -266,11 +266,11 @@ class Tagger(QtWidgets.QApplication): self.pluginmanager = PluginManager() if not self._no_plugins: if IS_FROZEN: - plugin_dir = os.path.join(os.path.dirname(sys.argv[0]), "plugins") + topdir = os.path.dirname(sys.argv[0]) else: - mydir = os.path.dirname(os.path.abspath(__file__)) - plugin_dir = os.path.join(mydir, "plugins") + topdir = os.path.dirname(os.path.abspath(__file__)) + plugin_dir = os.path.join(topdir, "plugins") self.pluginmanager.load_plugins_from_directory(plugin_dir) if not os.path.exists(USER_PLUGIN_DIR): From 845c1abd2697703a9f4202693dea6084fcb50402 Mon Sep 17 00:00:00 2001 From: Laurent Monin Date: Wed, 5 Jan 2022 15:27:03 +0100 Subject: [PATCH 3/5] Reduce code redundancy, introducing toppath --- picard/tagger.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/picard/tagger.py b/picard/tagger.py index 7d170361d..ec6605186 100644 --- a/picard/tagger.py +++ b/picard/tagger.py @@ -266,10 +266,11 @@ class Tagger(QtWidgets.QApplication): self.pluginmanager = PluginManager() if not self._no_plugins: if IS_FROZEN: - topdir = os.path.dirname(sys.argv[0]) + toppath = sys.argv[0] else: - topdir = os.path.dirname(os.path.abspath(__file__)) + toppath = os.path.abspath(__file__) + topdir = os.path.dirname(toppath) plugin_dir = os.path.join(topdir, "plugins") self.pluginmanager.load_plugins_from_directory(plugin_dir) From 9dca67e267050197e23fbd40a0ff1ecd040aafb2 Mon Sep 17 00:00:00 2001 From: Laurent Monin Date: Wed, 5 Jan 2022 15:28:57 +0100 Subject: [PATCH 4/5] Reduce code redudancy, introducing plugin_dirs list and a loop --- picard/tagger.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/picard/tagger.py b/picard/tagger.py index ec6605186..29c124bce 100644 --- a/picard/tagger.py +++ b/picard/tagger.py @@ -265,6 +265,7 @@ class Tagger(QtWidgets.QApplication): # Load plugins self.pluginmanager = PluginManager() if not self._no_plugins: + plugin_dirs = [] if IS_FROZEN: toppath = sys.argv[0] else: @@ -272,11 +273,14 @@ class Tagger(QtWidgets.QApplication): topdir = os.path.dirname(toppath) plugin_dir = os.path.join(topdir, "plugins") - self.pluginmanager.load_plugins_from_directory(plugin_dir) + plugin_dirs.append(plugin_dir) if not os.path.exists(USER_PLUGIN_DIR): os.makedirs(USER_PLUGIN_DIR) - self.pluginmanager.load_plugins_from_directory(USER_PLUGIN_DIR) + plugin_dirs.append(USER_PLUGIN_DIR) + + for plugin_dir in plugin_dirs: + self.pluginmanager.load_plugins_from_directory(plugin_dir) self.browser_integration = BrowserIntegration() self.browser_integration.listen_port_changed.connect(self.listen_port_changed) From 3148dc65f497f8d08bbba9225f7c5e7d85ad7458 Mon Sep 17 00:00:00 2001 From: Laurent Monin Date: Wed, 5 Jan 2022 15:30:35 +0100 Subject: [PATCH 5/5] Move code to new generator plugin_dirs() --- picard/tagger.py | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/picard/tagger.py b/picard/tagger.py index 29c124bce..95e85f79f 100644 --- a/picard/tagger.py +++ b/picard/tagger.py @@ -156,6 +156,21 @@ _orig_shutil_copystat = shutil.copystat shutil.copystat = _patched_shutil_copystat +def plugin_dirs(): + if IS_FROZEN: + toppath = sys.argv[0] + else: + toppath = os.path.abspath(__file__) + + topdir = os.path.dirname(toppath) + plugin_dir = os.path.join(topdir, "plugins") + yield plugin_dir + + if not os.path.exists(USER_PLUGIN_DIR): + os.makedirs(USER_PLUGIN_DIR) + yield USER_PLUGIN_DIR + + class Tagger(QtWidgets.QApplication): tagger_stats_changed = QtCore.pyqtSignal() @@ -265,21 +280,7 @@ class Tagger(QtWidgets.QApplication): # Load plugins self.pluginmanager = PluginManager() if not self._no_plugins: - plugin_dirs = [] - if IS_FROZEN: - toppath = sys.argv[0] - else: - toppath = os.path.abspath(__file__) - - topdir = os.path.dirname(toppath) - plugin_dir = os.path.join(topdir, "plugins") - plugin_dirs.append(plugin_dir) - - if not os.path.exists(USER_PLUGIN_DIR): - os.makedirs(USER_PLUGIN_DIR) - plugin_dirs.append(USER_PLUGIN_DIR) - - for plugin_dir in plugin_dirs: + for plugin_dir in plugin_dirs(): self.pluginmanager.load_plugins_from_directory(plugin_dir) self.browser_integration = BrowserIntegration()