Mbed Host Tests
host_test_registry.py
Go to the documentation of this file.
1"""
2mbed SDK
3Copyright (c) 2011-2015 ARM Limited
4
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16
17Author: Przemyslaw Wirkus <Przemyslaw.Wirkus@arm.com>
18"""
19
21 """ Simple class used to register and store
22 host test plugins for further usage
23 """
24 # Here we actually store all the plugins
25 PLUGINS = {} # 'Plugin Name' : Plugin Object
26
27 def print_error(self, text):
28 """! Prints error directly on console
29
30 @param text Error message text message
31 """
32 print("Plugin load failed. Reason: %s"% text)
33
34 def register_plugin(self, plugin):
35 """! Registers and stores plugin inside registry for further use.
36
37 @param plugin Plugin name
38
39 @return True if plugin setup was successful and plugin can be registered, else False
40
41 @details Method also calls plugin's setup() function to configure plugin if needed.
42 Note: Different groups of plugins may demand different extra parameter. Plugins
43 should be at least for one type of plugin configured with the same parameters
44 because we do not know which of them will actually use particular parameter.
45 """
46 # TODO:
47 # - check for unique caps for specified type
48 if plugin.name not in self.PLUGINS:
49 if plugin.setup(): # Setup plugin can be completed without errors
50 self.PLUGINS[plugin.name] = plugin
51 return True
52 else:
53 self.print_error("%s setup failed"% plugin.name)
54 else:
55 self.print_error("%s already loaded"% plugin.name)
56 return False
57
58 def call_plugin(self, type, capability, *args, **kwargs):
59 """! Execute plugin functionality respectively to its purpose
60 @param type Plugin type
61 @param capability Plugin capability name
62 @param args Additional plugin parameters
63 @param kwargs Additional plugin parameters
64 @return Returns result from plugin's execute() method
65 """
66 for plugin_name in self.PLUGINS:
67 plugin = self.PLUGINS[plugin_name]
68 if plugin.type == type and capability in plugin.capabilities:
69 return plugin.execute(capability, *args, **kwargs)
70 return False
71
72 def get_plugin_caps(self, type):
73 """! Returns list of all capabilities for plugin family with the same type
74 @param type Plugin type
75 @return Returns list of capabilities for plugin. If there are no capabilities empty list is returned
76 """
77 result = []
78 for plugin_name in self.PLUGINS:
79 plugin = self.PLUGINS[plugin_name]
80 if plugin.type == type:
81 result.extend(plugin.capabilities)
82 return sorted(result)
83
84 def load_plugin(self, name):
85 """! Used to load module from system (by import)
86 @param name name of the module to import
87 @return Returns result of __import__ operation
88 """
89 mod = __import__("module_%s"% name)
90 return mod
91
92 def get_string(self):
93 """! User friendly printing method to show hooked plugins
94 @return Returns string formatted with PrettyTable
95 """
96 from prettytable import PrettyTable, HEADER
97 column_names = ['name', 'type', 'capabilities', 'stable', 'os_support', 'required_parameters']
98 pt = PrettyTable(column_names, junction_char="|", hrules=HEADER)
99 for column in column_names:
100 pt.align[column] = 'l'
101 for plugin_name in sorted(self.PLUGINS.keys()):
102 name = self.PLUGINS[plugin_name].name
103 type = self.PLUGINS[plugin_name].type
104 stable = self.PLUGINS[plugin_name].stable
105 capabilities = ', '.join(self.PLUGINS[plugin_name].capabilities)
106 is_os_supported = self.PLUGINS[plugin_name].is_os_supported()
107 required_parameters = ', '.join(self.PLUGINS[plugin_name].required_parameters)
108 row = [name, type, capabilities, stable, is_os_supported, required_parameters]
109 pt.add_row(row)
110 return pt.get_string()
111
112 def get_dict(self):
113 column_names = ['name', 'type', 'capabilities', 'stable']
114 result = {}
115 for plugin_name in sorted(self.PLUGINS.keys()):
116 name = self.PLUGINS[plugin_name].name
117 type = self.PLUGINS[plugin_name].type
118 stable = self.PLUGINS[plugin_name].stable
119 capabilities = self.PLUGINS[plugin_name].capabilities
120 is_os_supported = self.PLUGINS[plugin_name].is_os_supported()
121 required_parameters = self.PLUGINS[plugin_name].required_parameters
122 result[plugin_name] = {
123 "name" : name,
124 "type" : type,
125 "stable" : stable,
126 "capabilities" : capabilities,
127 "os_support" : is_os_supported,
128 "required_parameters" : required_parameters
129 }
130 return result
131
132 def __str__(self):
133 return self.get_string()
call_plugin(self, type, capability, *args, **kwargs)
Execute plugin functionality respectively to its purpose.
load_plugin(self, name)
Used to load module from system (by import)
get_string(self)
User friendly printing method to show hooked plugins.
get_plugin_caps(self, type)
Returns list of all capabilities for plugin family with the same type.
register_plugin(self, plugin)
Registers and stores plugin inside registry for further use.