]>
git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/workspace_tools/host_tests/host_tests_plugins/host_test_registry.py
3 Copyright (c) 2011-2013 ARM Limited
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
9 http://www.apache.org/licenses/LICENSE-2.0
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.
18 class HostTestRegistry
:
19 """ Simple class used to register and store
20 host test plugins for further usage
22 # Here we actually store all the plugins
23 PLUGINS
= {} # 'Plugin Name' : Plugin Object
25 def print_error(self
, text
):
26 print "Plugin load failed. Reason: %s"% text
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.
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.
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
43 self
.print_error("%s setup failed"% plugin
.name
)
45 self
.print_error("%s already loaded"% plugin
.name
)
48 def call_plugin(self
, type, capability
, *args
, **kwargs
):
49 """ Execute plugin functionality respectively to its purpose
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
)
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
62 for plugin_name
in self
.PLUGINS
:
63 plugin
= self
.PLUGINS
[plugin_name
]
64 if plugin
.type == type:
65 result
.extend(plugin
.capabilities
)
68 def load_plugin(self
, name
):
69 """ Used to load module from
71 mod
= __import__("module_%s"% name
)
75 """ User friendly printing method to show hooked plugins
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
]
89 return pt
.get_string()