Move reconnect() helper function to picard.util

This commit is contained in:
Laurent Monin
2018-09-27 14:41:56 +02:00
parent 40a0e3b1a4
commit e71cd1d5f4
2 changed files with 22 additions and 12 deletions

View File

@@ -42,6 +42,8 @@ from picard.const import (
USER_PLUGIN_DIR,
)
from picard.util import reconnect
from picard.ui import HashableTreeWidgetItem
from picard.ui.options import (
OptionsPage,
@@ -427,18 +429,6 @@ class PluginsOptionsPage(OptionsPage):
log.debug("Plugin %r enabled: %r", item.plugin.name, item.is_enabled)
update_text()
def reconnect(signal, newhandler=None, oldhandler=None):
while True:
try:
if oldhandler is not None:
signal.disconnect(oldhandler)
else:
signal.disconnect()
except TypeError:
break
if newhandler is not None:
signal.connect(newhandler)
reconnect(item.button_enable.clicked, toggle_enable)
install_enabled = not item.is_installed or bool(item.new_version)

View File

@@ -546,3 +546,23 @@ def compare_version_tuples(version1, version2):
if test1[x] != test2[x]:
return 1 if test1[x] < test2[x] else -1
return 0
def reconnect(signal, newhandler=None, oldhandler=None):
"""
Reconnect an handler to a signal
It disconnects all previous handlers before connecting new one
Credits: https://stackoverflow.com/a/21589403
"""
while True:
try:
if oldhandler is not None:
signal.disconnect(oldhandler)
else:
signal.disconnect()
except TypeError:
break
if newhandler is not None:
signal.connect(newhandler)