From 170cf996c695a39231e8569e06aa50af12faed47 Mon Sep 17 00:00:00 2001 From: vishichoudhary Date: Thu, 1 Feb 2018 23:12:23 +0530 Subject: [PATCH 1/3] Fix multi click wierd issues --- picard/plugin.py | 73 ++++++++++++++++++++++++++---------- picard/ui/options/plugins.py | 32 ++++++++++------ 2 files changed, 74 insertions(+), 31 deletions(-) diff --git a/picard/plugin.py b/picard/plugin.py index 493400ea3..3dd54f5f9 100644 --- a/picard/plugin.py +++ b/picard/plugin.py @@ -394,16 +394,10 @@ class PluginManager(QtCore.QObject): plugin_name = os.path.splitext(zip_plugin)[0] if plugin_name: try: - dirpath, filepaths = self._get_existing_paths(plugin_name) - update = dirpath or filepaths if plugin_data and plugin_name: # zipped module from download zip_plugin = plugin_name + '.zip' dst = os.path.join(USER_PLUGIN_DIR, zip_plugin) - if update: - dst += '.update' - if os.path.isfile(dst): - os.remove(dst) ziptmp = tempfile.NamedTemporaryFile(delete=False, dir=USER_PLUGIN_DIR).name try: @@ -421,24 +415,63 @@ class PluginManager(QtCore.QObject): raise elif os.path.isfile(path): dst = os.path.join(USER_PLUGIN_DIR, os.path.basename(path)) - if update: - dst += '.update' - if os.path.isfile(dst): - os.remove(dst) shutil.copy2(path, dst) elif os.path.isdir(path): dst = os.path.join(USER_PLUGIN_DIR, plugin_name) - if update: - dst += '.update' - if os.path.isdir(dst): - shutil.rmtree(dst) shutil.copytree(path, dst) - if not update: - installed_plugin = self.load_plugin(zip_plugin or plugin_name, USER_PLUGIN_DIR) - if installed_plugin is not None: - self.plugin_installed.emit(installed_plugin, False) - else: - self.plugin_updated.emit(plugin_name, False) + installed_plugin = self.load_plugin(zip_plugin or plugin_name, USER_PLUGIN_DIR) + if installed_plugin is not None: + self.plugin_installed.emit(installed_plugin, False) + except (OSError, IOError): + log.warning("Unable to copy %s to plugin folder %s" % (path, USER_PLUGIN_DIR)) + + def update_plugin(self, path, overwrite_confirm=None, plugin_name=None, + plugin_data=None): + + zip_plugin = False + if not plugin_name: + zip_plugin = is_zip(path) + if not zip_plugin: + plugin_name = _plugin_name_from_path(path) + else: + plugin_name = os.path.splitext(zip_plugin)[0] + if plugin_name: + try: + if plugin_data and plugin_name: + # zipped module from download + zip_plugin = plugin_name + '.zip' + dst = os.path.join(USER_PLUGIN_DIR, zip_plugin) + dst += '.update' + if os.path.isfile(dst): + os.remove(dst) + ziptmp = tempfile.NamedTemporaryFile(delete=False, + dir=USER_PLUGIN_DIR).name + try: + with open(ziptmp, "wb") as zipfile: + zipfile.write(plugin_data) + zipfile.flush() + os.fsync(zipfile.fileno()) + os.rename(ziptmp, dst) + log.debug("Plugin saved to %r", dst) + except: + try: + os.remove(ziptmp) + except (IOError, OSError): + pass + raise + elif os.path.isfile(path): + dst = os.path.join(USER_PLUGIN_DIR, os.path.basename(path)) + dst += '.update' + if os.path.isfile(dst): + os.remove(dst) + shutil.copy2(path, dst) + elif os.path.isdir(path): + dst = os.path.join(USER_PLUGIN_DIR, plugin_name) + dst += '.update' + if os.path.isdir(dst): + shutil.rmtree(dst) + shutil.copytree(path, dst) + self.plugin_updated.emit(plugin_name, False) except (OSError, IOError): log.warning("Unable to copy %s to plugin folder %s" % (path, USER_PLUGIN_DIR)) diff --git a/picard/ui/options/plugins.py b/picard/ui/options/plugins.py index 2c5fda16f..fe1d69b77 100644 --- a/picard/ui/options/plugins.py +++ b/picard/ui/options/plugins.py @@ -257,10 +257,13 @@ class PluginsOptionsPage(OptionsPage): button.setMaximumHeight(button.fontMetrics().boundingRect(label).height() + 7) self.ui.plugins.setItemWidget(item, 2, button) - def download_button_process(): + def download_button_process(action): self.ui.plugins.setCurrentItem(item) - self.download_plugin() - button.released.connect(download_button_process) + self.download_plugin(action) + if label == _("Update"): + button.released.connect(partial(download_button_process, "update")) + else: + button.released.connect(partial(download_button_process, "install")) else: # Note: setText() don't work after it was set to a button if plugin.marked_for_update: @@ -315,7 +318,7 @@ class PluginsOptionsPage(OptionsPage): for path in files: self.tagger.pluginmanager.install_plugin(path) - def download_plugin(self): + def download_plugin(self, action): selected = self.ui.plugins.selectedItems()[0] plugin = self.items[selected] @@ -323,14 +326,14 @@ class PluginsOptionsPage(OptionsPage): PLUGINS_API['host'], PLUGINS_API['port'], PLUGINS_API['endpoint']['download'], - partial(self.download_handler, plugin=plugin), + partial(self.download_handler, action, plugin=plugin), parse_response_type=None, priority=True, important=True, queryargs={"id": plugin.module_name} ) - def download_handler(self, response, reply, error, plugin): + def download_handler(self, action, response, reply, error, plugin): if error: msgbox = QtWidgets.QMessageBox(self) msgbox.setText(_("The plugin '%s' could not be downloaded.") % plugin.module_name) @@ -341,11 +344,18 @@ class PluginsOptionsPage(OptionsPage): log.error("Error occurred while trying to download the plugin: '%s'" % plugin.module_name) return - self.tagger.pluginmanager.install_plugin( - None, - plugin_name=plugin.module_name, - plugin_data=response - ) + if action == "install": + self.tagger.pluginmanager.install_plugin( + None, + plugin_name=plugin.module_name, + plugin_data=response + ) + else: + self.tagger.pluginmanager.update_plugin( + None, + plugin_name=plugin.module_name, + plugin_data=response + ) def open_plugin_dir(self): QtGui.QDesktopServices.openUrl(QtCore.QUrl(self.loader % USER_PLUGIN_DIR, QtCore.QUrl.TolerantMode)) From 774c239d3ff5eed5c9ba281121a493c55d003d3e Mon Sep 17 00:00:00 2001 From: vishichoudhary Date: Fri, 2 Feb 2018 19:05:03 +0530 Subject: [PATCH 2/3] Remove duplicate code --- picard/plugin.py | 74 ++++++++++-------------------------- picard/ui/options/plugins.py | 39 +++++++++---------- 2 files changed, 39 insertions(+), 74 deletions(-) diff --git a/picard/plugin.py b/picard/plugin.py index 3dd54f5f9..383176169 100644 --- a/picard/plugin.py +++ b/picard/plugin.py @@ -39,6 +39,7 @@ from picard import (config, from picard.const import USER_PLUGIN_DIR, PLUGINS_API from picard.util import load_json +PLUGIN_ACTION_UPDATE = 1 _suffixes = [s[0] for s in imp.get_suffixes()] _package_entries = ["__init__.py", "__init__.pyc", "__init__.pyo"] @@ -376,7 +377,7 @@ class PluginManager(QtCore.QObject): log.debug("Removing file %r", filepath) os.remove(filepath) - def install_plugin(self, path, overwrite_confirm=None, plugin_name=None, + def install_plugin(self, path, action, overwrite_confirm=None, plugin_name=None, plugin_data=None): """ path is either: @@ -398,6 +399,10 @@ class PluginManager(QtCore.QObject): # zipped module from download zip_plugin = plugin_name + '.zip' dst = os.path.join(USER_PLUGIN_DIR, zip_plugin) + if action == PLUGIN_ACTION_UPDATE: + dst += '.update' + if os.path.isfile(dst): + os.remove(dst) ziptmp = tempfile.NamedTemporaryFile(delete=False, dir=USER_PLUGIN_DIR).name try: @@ -415,63 +420,24 @@ class PluginManager(QtCore.QObject): raise elif os.path.isfile(path): dst = os.path.join(USER_PLUGIN_DIR, os.path.basename(path)) + if action == PLUGIN_ACTION_UPDATE: + dst += '.update' + if os.path.isfile(dst): + os.remove(dst) shutil.copy2(path, dst) elif os.path.isdir(path): dst = os.path.join(USER_PLUGIN_DIR, plugin_name) + if action == PLUGIN_ACTION_UPDATE: + dst += '.update' + if os.path.isdir(dst): + shutil.rmtree(dst) shutil.copytree(path, dst) - installed_plugin = self.load_plugin(zip_plugin or plugin_name, USER_PLUGIN_DIR) - if installed_plugin is not None: - self.plugin_installed.emit(installed_plugin, False) - except (OSError, IOError): - log.warning("Unable to copy %s to plugin folder %s" % (path, USER_PLUGIN_DIR)) - - def update_plugin(self, path, overwrite_confirm=None, plugin_name=None, - plugin_data=None): - - zip_plugin = False - if not plugin_name: - zip_plugin = is_zip(path) - if not zip_plugin: - plugin_name = _plugin_name_from_path(path) - else: - plugin_name = os.path.splitext(zip_plugin)[0] - if plugin_name: - try: - if plugin_data and plugin_name: - # zipped module from download - zip_plugin = plugin_name + '.zip' - dst = os.path.join(USER_PLUGIN_DIR, zip_plugin) - dst += '.update' - if os.path.isfile(dst): - os.remove(dst) - ziptmp = tempfile.NamedTemporaryFile(delete=False, - dir=USER_PLUGIN_DIR).name - try: - with open(ziptmp, "wb") as zipfile: - zipfile.write(plugin_data) - zipfile.flush() - os.fsync(zipfile.fileno()) - os.rename(ziptmp, dst) - log.debug("Plugin saved to %r", dst) - except: - try: - os.remove(ziptmp) - except (IOError, OSError): - pass - raise - elif os.path.isfile(path): - dst = os.path.join(USER_PLUGIN_DIR, os.path.basename(path)) - dst += '.update' - if os.path.isfile(dst): - os.remove(dst) - shutil.copy2(path, dst) - elif os.path.isdir(path): - dst = os.path.join(USER_PLUGIN_DIR, plugin_name) - dst += '.update' - if os.path.isdir(dst): - shutil.rmtree(dst) - shutil.copytree(path, dst) - self.plugin_updated.emit(plugin_name, False) + if action != PLUGIN_ACTION_UPDATE: + installed_plugin = self.load_plugin(zip_plugin or plugin_name, USER_PLUGIN_DIR) + if installed_plugin is not None: + self.plugin_installed.emit(installed_plugin, False) + else: + self.plugin_updated.emit(plugin_name, False) except (OSError, IOError): log.warning("Unable to copy %s to plugin folder %s" % (path, USER_PLUGIN_DIR)) diff --git a/picard/ui/options/plugins.py b/picard/ui/options/plugins.py index fe1d69b77..17a2525cc 100644 --- a/picard/ui/options/plugins.py +++ b/picard/ui/options/plugins.py @@ -34,6 +34,7 @@ from picard.ui import HashableTreeWidgetItem from picard.ui.options import OptionsPage, register_options_page from picard.ui.ui_options_plugins import Ui_PluginsOptionsPage +PLUGIN_ACTION_NONE, PLUGIN_ACTION_UPDATE, PLUGIN_ACTION_INSTALL = range(3) class PluginTreeWidgetItem(HashableTreeWidgetItem): @@ -245,14 +246,19 @@ class PluginsOptionsPage(OptionsPage): else: item.setText(1, plugin.version) - label = None + bt_action = PLUGIN_ACTION_NONE if plugin.can_be_updated: - label = _("Update") + bt_action = PLUGIN_ACTION_UPDATE elif plugin.can_be_downloaded: - label = _("Install") + bt_action = PLUGIN_ACTION_INSTALL item.setFlags(item.flags() ^ QtCore.Qt.ItemIsUserCheckable) - if label is not None: + if bt_action != PLUGIN_ACTION_NONE: + labels = { + PLUGIN_ACTION_UPDATE: N_("Update"), + PLUGIN_ACTION_INSTALL: N_("Install"), + } + label = _(labels[bt_action]) button = QtWidgets.QPushButton(label) button.setMaximumHeight(button.fontMetrics().boundingRect(label).height() + 7) self.ui.plugins.setItemWidget(item, 2, button) @@ -260,12 +266,11 @@ class PluginsOptionsPage(OptionsPage): def download_button_process(action): self.ui.plugins.setCurrentItem(item) self.download_plugin(action) - if label == _("Update"): - button.released.connect(partial(download_button_process, "update")) - else: - button.released.connect(partial(download_button_process, "install")) + + button.released.connect(partial(download_button_process, bt_action)) else: # Note: setText() don't work after it was set to a button + label = None if plugin.marked_for_update: label = _("Updated") else: @@ -344,18 +349,12 @@ class PluginsOptionsPage(OptionsPage): log.error("Error occurred while trying to download the plugin: '%s'" % plugin.module_name) return - if action == "install": - self.tagger.pluginmanager.install_plugin( - None, - plugin_name=plugin.module_name, - plugin_data=response - ) - else: - self.tagger.pluginmanager.update_plugin( - None, - plugin_name=plugin.module_name, - plugin_data=response - ) + self.tagger.pluginmanager.install_plugin( + None, + action, + plugin_name=plugin.module_name, + plugin_data=response, + ) def open_plugin_dir(self): QtGui.QDesktopServices.openUrl(QtCore.QUrl(self.loader % USER_PLUGIN_DIR, QtCore.QUrl.TolerantMode)) From 41e02c3c243a26a77d8f6fc18954925f85e93b67 Mon Sep 17 00:00:00 2001 From: vishichoudhary Date: Fri, 2 Feb 2018 19:39:53 +0530 Subject: [PATCH 3/3] move variable to const --- picard/const/__init__.py | 3 +++ picard/plugin.py | 4 +--- picard/ui/options/plugins.py | 5 +++-- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/picard/const/__init__.py b/picard/const/__init__.py index 8ed53f703..7389d94af 100644 --- a/picard/const/__init__.py +++ b/picard/const/__init__.py @@ -69,6 +69,9 @@ PICARD_URLS = { 'acoustid_track': "https://acoustid.org/track/", } +# Various option for plugin action +PLUGIN_ACTION_NONE, PLUGIN_ACTION_UPDATE, PLUGIN_ACTION_INSTALL = range(3) + # Various Artists MBID VARIOUS_ARTISTS_ID = '89ad4ac3-39f7-470e-963a-56509c546377' diff --git a/picard/plugin.py b/picard/plugin.py index 383176169..57f108ea7 100644 --- a/picard/plugin.py +++ b/picard/plugin.py @@ -36,11 +36,9 @@ from picard import (config, version_from_string, version_to_string, VersionError) -from picard.const import USER_PLUGIN_DIR, PLUGINS_API +from picard.const import USER_PLUGIN_DIR, PLUGINS_API, PLUGIN_ACTION_UPDATE from picard.util import load_json -PLUGIN_ACTION_UPDATE = 1 - _suffixes = [s[0] for s in imp.get_suffixes()] _package_entries = ["__init__.py", "__init__.pyc", "__init__.pyo"] _extension_points = [] diff --git a/picard/ui/options/plugins.py b/picard/ui/options/plugins.py index 17a2525cc..d38d59160 100644 --- a/picard/ui/options/plugins.py +++ b/picard/ui/options/plugins.py @@ -29,13 +29,14 @@ from picard import config, log from picard.const import ( USER_PLUGIN_DIR, PLUGINS_API, + PLUGIN_ACTION_NONE, + PLUGIN_ACTION_INSTALL, + PLUGIN_ACTION_UPDATE, ) from picard.ui import HashableTreeWidgetItem from picard.ui.options import OptionsPage, register_options_page from picard.ui.ui_options_plugins import Ui_PluginsOptionsPage -PLUGIN_ACTION_NONE, PLUGIN_ACTION_UPDATE, PLUGIN_ACTION_INSTALL = range(3) - class PluginTreeWidgetItem(HashableTreeWidgetItem): def __lt__(self, other):