Fix up Registry function processing

This commit is contained in:
Tim Bentley 2013-02-04 19:25:12 +00:00
parent ca5e46a2b1
commit b7b19a4180
9 changed files with 70 additions and 32 deletions

View File

@ -98,32 +98,36 @@ class Registry(object):
if key in self.service_list: if key in self.service_list:
del self.service_list[key] del self.service_list[key]
def register_function(self, occasion, function): def register_function(self, event, function):
""" """
Register a function and a handler to be called later Register a function and a handler to be called later
""" """
if self.functions_list.has_key(occasion) == 0: if not self.functions_list.has_key(event):
self.functions_list[occasion] = [function] self.functions_list[event] = [function]
else: else:
self.functions_list[occasion].append(function) self.functions_list[event].append(function)
def remove_function(self, occasion, function): def remove_function(self, event, function):
""" """
Register a function and a handler to be called later Register a function and a handler to be called later
""" """
if self.running_under_test is False: if self.running_under_test is False:
log.error(u'Invalid Method call for key %s' % occasion) log.error(u'Invalid Method call for key %s' % event)
raise KeyError(u'Invalid Method call for key %s' % occasion) raise KeyError(u'Invalid Method call for key %s' % event)
return return
if self.functions_list.has_key(occasion): if event in self.functions_list:
self.functions_list[occasion].remove(function) self.functions_list[event].remove(function)
def execute(self, occasion, data): def execute(self, event, *args, **kwargs):
""" """
Execute all the handlers registered passing the data to the handler Execute all the handlers registered passing the data to the handler and returning results
""" """
print self.functions_list, self.functions_list.has_key(occasion) results = []
if event in self.functions_list:
for function in self.functions_list[event]:
print event, function,
result = function(*args, **kwargs)
if result:
results.append(result)
return results
if self.functions_list.has_key(occasion):
for function in self.functions_list[occasion]:
function(data)

View File

@ -80,7 +80,7 @@ class Renderer(object):
self.display.setup() self.display.setup()
self._theme_dimensions = {} self._theme_dimensions = {}
self._calculate_default() self._calculate_default()
QtCore.QObject.connect(Receiver.get_receiver(), QtCore.SIGNAL(u'theme_update_global'), self.set_global_theme) Registry().register_function(u'theme_update_global', self.set_global_theme)
self.web = QtWebKit.QWebView() self.web = QtWebKit.QWebView()
self.web.setVisible(False) self.web.setVisible(False)
self.web_frame = self.web.page().mainFrame() self.web_frame = self.web.page().mainFrame()

View File

@ -528,7 +528,7 @@ class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
QtCore.QObject.connect(self.modeDefaultItem, QtCore.SIGNAL(u'triggered()'), self.onModeDefaultItemClicked) QtCore.QObject.connect(self.modeDefaultItem, QtCore.SIGNAL(u'triggered()'), self.onModeDefaultItemClicked)
QtCore.QObject.connect(self.modeSetupItem, QtCore.SIGNAL(u'triggered()'), self.onModeSetupItemClicked) QtCore.QObject.connect(self.modeSetupItem, QtCore.SIGNAL(u'triggered()'), self.onModeSetupItemClicked)
QtCore.QObject.connect(self.modeLiveItem, QtCore.SIGNAL(u'triggered()'), self.onModeLiveItemClicked) QtCore.QObject.connect(self.modeLiveItem, QtCore.SIGNAL(u'triggered()'), self.onModeLiveItemClicked)
QtCore.QObject.connect(Receiver.get_receiver(), QtCore.SIGNAL(u'theme_update_global'), self.defaultThemeChanged) Registry().register_function(u'theme_update_global', self.defaultThemeChanged)
QtCore.QObject.connect(Receiver.get_receiver(), QtCore.SIGNAL(u'openlp_version_check'), self.versionNotice) QtCore.QObject.connect(Receiver.get_receiver(), QtCore.SIGNAL(u'openlp_version_check'), self.versionNotice)
QtCore.QObject.connect(Receiver.get_receiver(), QtCore.SIGNAL(u'live_display_blank_check'), self.blankCheck) QtCore.QObject.connect(Receiver.get_receiver(), QtCore.SIGNAL(u'live_display_blank_check'), self.blankCheck)
QtCore.QObject.connect(Receiver.get_receiver(), QtCore.SIGNAL(u'config_screen_changed'), self.screenChanged) QtCore.QObject.connect(Receiver.get_receiver(), QtCore.SIGNAL(u'config_screen_changed'), self.screenChanged)
@ -699,7 +699,8 @@ class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
self.activePlugin.toggleStatus(PluginStatus.Inactive) self.activePlugin.toggleStatus(PluginStatus.Inactive)
self.themeManagerContents.configUpdated() self.themeManagerContents.configUpdated()
self.themeManagerContents.load_themes(True) self.themeManagerContents.load_themes(True)
Receiver.send_message(u'theme_update_global', self.themeManagerContents.global_theme) #Receiver.send_message(u'theme_update_global', self.themeManagerContents.global_theme)
Registry().execute(u'theme_update_global', self.themeManagerContents.global_theme)
# Check if any Bibles downloaded. If there are, they will be # Check if any Bibles downloaded. If there are, they will be
# processed. # processed.
Receiver.send_message(u'bibles_load_list', True) Receiver.send_message(u'bibles_load_list', True)

View File

@ -214,7 +214,7 @@ class ServiceManagerDialog(object):
QtCore.QObject.connect(Receiver.get_receiver(), QtCore.SIGNAL(u'config_updated'), self.config_updated) QtCore.QObject.connect(Receiver.get_receiver(), QtCore.SIGNAL(u'config_updated'), self.config_updated)
QtCore.QObject.connect(Receiver.get_receiver(), QtCore.SIGNAL(u'config_screen_changed'), QtCore.QObject.connect(Receiver.get_receiver(), QtCore.SIGNAL(u'config_screen_changed'),
self.regenerate_service_Items) self.regenerate_service_Items)
QtCore.QObject.connect(Receiver.get_receiver(), QtCore.SIGNAL(u'theme_update_global'), self.theme_change) Registry().register_function(u'theme_update_global', self.theme_change)
# Last little bits of setting up # Last little bits of setting up
self.service_theme = Settings().value(self.main_window.serviceManagerSettingsSection + u'/service theme') self.service_theme = Settings().value(self.main_window.serviceManagerSettingsSection + u'/service theme')
self.servicePath = AppLocation.get_section_data_path(u'servicemanager') self.servicePath = AppLocation.get_section_data_path(u'servicemanager')
@ -1243,7 +1243,7 @@ class ServiceManager(QtGui.QWidget, ServiceManagerDialog):
Settings().setValue(self.main_window.serviceManagerSettingsSection + u'/service theme', self.service_theme) Settings().setValue(self.main_window.serviceManagerSettingsSection + u'/service theme', self.service_theme)
self.regenerate_service_Items(True) self.regenerate_service_Items(True)
def theme_change(self): def theme_change(self, args, **Kwargs):
""" """
The theme may have changed in the settings dialog so make The theme may have changed in the settings dialog so make
sure the theme combo box is in the correct state. sure the theme combo box is in the correct state.

View File

@ -134,8 +134,7 @@ class ThemeManager(QtGui.QWidget):
QtCore.SIGNAL(u'doubleClicked(QModelIndex)'), self.changeGlobalFromScreen) QtCore.SIGNAL(u'doubleClicked(QModelIndex)'), self.changeGlobalFromScreen)
QtCore.QObject.connect(self.theme_list_widget, QtCore.QObject.connect(self.theme_list_widget,
QtCore.SIGNAL(u'currentItemChanged(QListWidgetItem *, QListWidgetItem *)'), self.check_list_state) QtCore.SIGNAL(u'currentItemChanged(QListWidgetItem *, QListWidgetItem *)'), self.check_list_state)
QtCore.QObject.connect(Receiver.get_receiver(), Registry().register_function(u'theme_update_global', self.change_global_from_tab)
QtCore.SIGNAL(u'theme_update_global'), self.change_global_from_tab)
QtCore.QObject.connect(Receiver.get_receiver(), QtCore.SIGNAL(u'config_updated'), self.config_updated) QtCore.QObject.connect(Receiver.get_receiver(), QtCore.SIGNAL(u'config_updated'), self.config_updated)
# Variables # Variables
self.theme_list = [] self.theme_list = []
@ -237,7 +236,6 @@ class ThemeManager(QtGui.QWidget):
name = translate('OpenLP.ThemeManager', '%s (default)') % self.global_theme name = translate('OpenLP.ThemeManager', '%s (default)') % self.global_theme
self.theme_list_widget.item(count).setText(name) self.theme_list_widget.item(count).setText(name)
Settings().setValue(self.settingsSection + u'/global theme', self.global_theme) Settings().setValue(self.settingsSection + u'/global theme', self.global_theme)
Receiver.send_message(u'theme_update_global', self.global_theme)
Registry().execute(u'theme_update_global', self.global_theme) Registry().execute(u'theme_update_global', self.global_theme)
self._push_themes() self._push_themes()

View File

@ -31,7 +31,7 @@ The Themes configuration tab
""" """
from PyQt4 import QtCore, QtGui from PyQt4 import QtCore, QtGui
from openlp.core.lib import Receiver, Settings, SettingsTab, translate, UiStrings from openlp.core.lib import Receiver, Registry, Settings, SettingsTab, translate, UiStrings
from openlp.core.lib.theme import ThemeLevel from openlp.core.lib.theme import ThemeLevel
from openlp.core.lib.ui import find_and_set_in_combo_box from openlp.core.lib.ui import find_and_set_in_combo_box
@ -104,7 +104,7 @@ class ThemesTab(SettingsTab):
QtCore.QObject.connect(self.GlobalLevelRadioButton, QtCore.SIGNAL(u'clicked()'), QtCore.QObject.connect(self.GlobalLevelRadioButton, QtCore.SIGNAL(u'clicked()'),
self.onGlobalLevelButtonClicked) self.onGlobalLevelButtonClicked)
QtCore.QObject.connect(self.DefaultComboBox, QtCore.SIGNAL(u'activated(int)'), self.onDefaultComboBoxChanged) QtCore.QObject.connect(self.DefaultComboBox, QtCore.SIGNAL(u'activated(int)'), self.onDefaultComboBoxChanged)
QtCore.QObject.connect(Receiver.get_receiver(), QtCore.SIGNAL(u'theme_update_list'), self.updateThemeList) Registry().register_function(u'theme_update_list', self.updateThemeList)
def retranslateUi(self): def retranslateUi(self):
""" """
@ -153,13 +153,15 @@ class ThemesTab(SettingsTab):
settings.endGroup() settings.endGroup()
self.renderer.set_global_theme(self.global_theme) self.renderer.set_global_theme(self.global_theme)
self.renderer.set_theme_level(self.theme_level) self.renderer.set_theme_level(self.theme_level)
Receiver.send_message(u'theme_update_global', self.global_theme) #Receiver.send_message(u'theme_update_global', self.global_theme)
Registry().execute(u'theme_update_global', self.global_theme)
def postSetUp(self): def postSetUp(self):
""" """
After setting things up... After setting things up...
""" """
Receiver.send_message(u'theme_update_global', self.global_theme) #Receiver.send_message(u'theme_update_global', self.global_theme)
Registry().execute(u'theme_update_global', self.global_theme)
def onSongLevelButtonClicked(self): def onSongLevelButtonClicked(self):
""" """
@ -197,6 +199,7 @@ class ThemesTab(SettingsTab):
[u'Bible Theme', u'Song Theme'] [u'Bible Theme', u'Song Theme']
""" """
# Reload as may have been triggered by the ThemeManager. # Reload as may have been triggered by the ThemeManager.
print theme_list
self.global_theme = Settings().value(self.settingsSection + u'/global theme') self.global_theme = Settings().value(self.settingsSection + u'/global theme')
self.DefaultComboBox.clear() self.DefaultComboBox.clear()
self.DefaultComboBox.addItems(theme_list) self.DefaultComboBox.addItems(theme_list)

View File

@ -64,7 +64,6 @@ class EditCustomForm(QtGui.QDialog, Ui_CustomEditDialog):
self.editAllButton.clicked.connect(self.on_edit_all_button_clicked) self.editAllButton.clicked.connect(self.on_edit_all_button_clicked)
self.slideListView.currentRowChanged.connect(self.on_current_row_changed) self.slideListView.currentRowChanged.connect(self.on_current_row_changed)
self.slideListView.doubleClicked.connect(self.on_edit_button_clicked) self.slideListView.doubleClicked.connect(self.on_edit_button_clicked)
QtCore.QObject.connect(Receiver.get_receiver(), QtCore.SIGNAL(u'theme_update_list'), self.loadThemes)
Registry().register_function(u'theme_update_list', self.loadThemes) Registry().register_function(u'theme_update_list', self.loadThemes)
def loadThemes(self, theme_list): def loadThemes(self, theme_list):

View File

@ -39,8 +39,8 @@ import shutil
from PyQt4 import QtCore, QtGui from PyQt4 import QtCore, QtGui
from openlp.core.lib import PluginStatus, Receiver, MediaType, translate, create_separated_list, \ from openlp.core.lib import PluginStatus, Receiver, MediaType, translate, create_separated_list, \
check_directory_exists, Registry, UiStrings, Registry check_directory_exists, Registry, UiStrings
from openlp.core.lib.ui import UiStrings, set_case_insensitive_completer, critical_error_message_box, \ from openlp.core.lib.ui import set_case_insensitive_completer, critical_error_message_box, \
find_and_set_in_combo_box find_and_set_in_combo_box
from openlp.core.utils import AppLocation from openlp.core.utils import AppLocation
from openlp.plugins.songs.forms import EditVerseForm, MediaFilesForm from openlp.plugins.songs.forms import EditVerseForm, MediaFilesForm
@ -97,7 +97,6 @@ class EditSongForm(QtGui.QDialog, Ui_EditSongDialog):
QtCore.QObject.connect(self.audioRemoveButton, QtCore.SIGNAL(u'clicked()'), self.onAudioRemoveButtonClicked) QtCore.QObject.connect(self.audioRemoveButton, QtCore.SIGNAL(u'clicked()'), self.onAudioRemoveButtonClicked)
QtCore.QObject.connect(self.audioRemoveAllButton, QtCore.SIGNAL(u'clicked()'), QtCore.QObject.connect(self.audioRemoveAllButton, QtCore.SIGNAL(u'clicked()'),
self.onAudioRemoveAllButtonClicked) self.onAudioRemoveAllButtonClicked)
QtCore.QObject.connect(Receiver.get_receiver(), QtCore.SIGNAL(u'theme_update_list'), self.loadThemes)
Registry().register_function(u'theme_update_list', self.loadThemes) Registry().register_function(u'theme_update_list', self.loadThemes)
self.previewButton = QtGui.QPushButton() self.previewButton = QtGui.QPushButton()
self.previewButton.setObjectName(u'previewButton') self.previewButton.setObjectName(u'previewButton')

View File

@ -11,7 +11,7 @@ TESTPATH = os.path.abspath(os.path.join(os.path.dirname(__file__), u'..', u'..',
class TestRegistry(TestCase): class TestRegistry(TestCase):
def registry_basic_test(self): def registry_service_test(self):
""" """
Test the registry creation and its usage Test the registry creation and its usage
""" """
@ -46,3 +46,37 @@ class TestRegistry(TestCase):
temp = Registry().get(u'test1') temp = Registry().get(u'test1')
self.assertEqual(context.exception[0], u'Service test1 not found in list', self.assertEqual(context.exception[0], u'Service test1 not found in list',
u'KeyError exception should have been thrown for deleted service') u'KeyError exception should have been thrown for deleted service')
def registry_function_test(self):
"""
Test the registry creation and its usage
"""
# GIVEN: An existing registry register a function
Registry.create()
Registry().register_function(u'test1', self.dummy_function_1)
# WHEN: I execute the function
return_value = Registry().execute(u'test1')
# THEN: I expect then function to have been called and a return given
self.assertEqual(return_value[0], u'function_1', u'A return value is provided and matches')
# WHEN: I execute the a function with the same reference and execute the function
Registry().register_function(u'test1', self.dummy_function_1)
return_value = Registry().execute(u'test1')
# THEN: I expect then function to have been called and a return given
self.assertEqual(return_value, [u'function_1', u'function_1'], u'A return value list is provided and matches')
# WHEN: I execute the a 2nd function with the different reference and execute the function
Registry().register_function(u'test2', self.dummy_function_2)
return_value = Registry().execute(u'test2')
# THEN: I expect then function to have been called and a return given
self.assertEqual(return_value[0], u'function_2', u'A return value is provided and matches')
def dummy_function_1(self):
return "function_1"
def dummy_function_2(self):
return "function_2"