]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/workspace_tools/host_tests/host_tests_plugins/host_test_registry.py
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / workspace_tools / host_tests / host_tests_plugins / host_test_registry.py
1 """
2 mbed SDK
3 Copyright (c) 2011-2013 ARM Limited
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9     http://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 """
17
18 class HostTestRegistry:
19     """ Simple class used to register and store
20         host test plugins for further usage
21     """
22     # Here we actually store all the plugins
23     PLUGINS = {}    # 'Plugin Name' : Plugin Object
24
25     def print_error(self, text):
26         print "Plugin load failed. Reason: %s"% text
27
28     def register_plugin(self, plugin):
29         """ Registers and stores plugin inside registry for further use.
30             Method also calls plugin's setup() function to configure plugin if needed.
31
32             Note: Different groups of plugins may demand different extra parameter. Plugins
33             should be at least for one type of plugin configured with the same parameters
34             because we do not know which of them will actually use particular parameter.
35         """
36         # TODO:
37         # - check for unique caps for specified type
38         if plugin.name not in self.PLUGINS:
39             if plugin.setup(): # Setup plugin can be completed without errors
40                 self.PLUGINS[plugin.name] = plugin
41                 return True
42             else:
43                 self.print_error("%s setup failed"% plugin.name)
44         else:
45             self.print_error("%s already loaded"% plugin.name)
46         return False
47
48     def call_plugin(self, type, capability, *args, **kwargs):
49         """ Execute plugin functionality respectively to its purpose
50         """
51         for plugin_name in self.PLUGINS:
52             plugin = self.PLUGINS[plugin_name]
53             if plugin.type == type and capability in plugin.capabilities:
54                 return plugin.execute(capability, *args, **kwargs)
55         return False
56
57     def get_plugin_caps(self, type):
58         """ Returns list of all capabilities for plugin family with the same type.
59             If there are no capabilities empty list is returned
60         """
61         result = []
62         for plugin_name in self.PLUGINS:
63             plugin = self.PLUGINS[plugin_name]
64             if plugin.type == type:
65                 result.extend(plugin.capabilities)
66         return sorted(result)
67
68     def load_plugin(self, name):
69         """ Used to load module from
70         """
71         mod = __import__("module_%s"% name)
72         return mod
73
74     def __str__(self):
75         """ User friendly printing method to show hooked plugins
76         """
77         from prettytable import PrettyTable
78         column_names = ['name', 'type', 'capabilities', 'stable']
79         pt = PrettyTable(column_names)
80         for column in column_names:
81             pt.align[column] =  'l'
82         for plugin_name in sorted(self.PLUGINS.keys()):
83             name  = self.PLUGINS[plugin_name].name
84             type  = self.PLUGINS[plugin_name].type
85             stable = self.PLUGINS[plugin_name].stable
86             capabilities  = ', '.join(self.PLUGINS[plugin_name].capabilities)
87             row = [name, type, capabilities, stable]
88             pt.add_row(row)
89         return pt.get_string()