diff --git a/openlp/core/common/registry.py b/openlp/core/common/registry.py index 85fca6912..2393425ee 100644 --- a/openlp/core/common/registry.py +++ b/openlp/core/common/registry.py @@ -24,6 +24,7 @@ Provide Registry Services """ import logging import sys +import re from openlp.core.common import trace_error_handler @@ -56,6 +57,7 @@ class Registry(object): registry.service_list = {} registry.functions_list = {} registry.working_flags = {} + registry.remote_apis = {} # Allow the tests to remove Registry entries but not the live system registry.running_under_test = 'nose' in sys.argv[0] registry.initialising = True @@ -176,3 +178,29 @@ class Registry(object): """ if key in self.working_flags: del self.working_flags[key] + + def remote_api(self, path, function, secure=False): + """ + Sets a working_flag based on the key passed in. + + :param path: The working_flag to be created this is usually a major class like "renderer" or "main_window" . + :param function: The data to be saved. + :param secure: The data to be saved. + """ + self.remote_apis[path] = {'function': function, 'secure': secure} + + def remote_execute(self, url_path): + """ + Execute all the handlers associated with the event and return an array of results. + + :param url_path: The url path to be found + """ + for url, funcs in self.remote_apis.items(): + a = url + match = re.match(url, url_path) + if match: + log.debug('Route "{route}" matched "{path}"'.format(route=url_path, path=url_path)) + args = [] + for param in match.groups(): + args.append(param) + return funcs, args diff --git a/openlp/plugins/remotes/lib/httpserver.py b/openlp/plugins/remotes/lib/httpserver.py index 4b96c7118..9c36375ae 100644 --- a/openlp/plugins/remotes/lib/httpserver.py +++ b/openlp/plugins/remotes/lib/httpserver.py @@ -25,8 +25,6 @@ The :mod:`http` module contains the API web server. This is a lightweight web se with OpenLP. It uses JSON to communicate with the remotes. """ -import ssl -import socket import logging import time diff --git a/tests/functional/openlp_core_common/test_registry.py b/tests/functional/openlp_core_common/test_registry.py index 0642ff93c..a0a3a9844 100644 --- a/tests/functional/openlp_core_common/test_registry.py +++ b/tests/functional/openlp_core_common/test_registry.py @@ -149,3 +149,24 @@ class TestRegistry(TestCase): def dummy_function_2(self): return "function_2" + + def test_registry_remote_apis(self): + """ + Test the registry working flags creation and its usage + """ + # GIVEN: A new registry + Registry.create() + + # WHEN: I register a API to function + func = MagicMock() + func1 = MagicMock() + Registry().remote_api(r'^/api/poll$', func) + Registry().remote_api(r'^/main/poll$', func1, True) + + # THEN: When I look to a function + call_structure, args = Registry().remote_execute('/api/poll') + self.assertEqual(call_structure['function'], func, 'Calling function should match') + self.assertEqual(call_structure['secure'], False, 'Calling function should not require security') + call_structure, args = Registry().remote_execute('/main/poll') + self.assertEqual(call_structure['function'], func1, 'Calling function should match') + self.assertEqual(call_structure['secure'], True, 'Calling function should not require security')