Mbed Host Tests
host_test_registry.py
Go to the documentation of this file.
1 """
2 mbed SDK
3 Copyright (c) 2011-2015 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 Author: 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.PLUGINSPLUGINS:
49  if plugin.setup(): # Setup plugin can be completed without errors
50  self.PLUGINSPLUGINS[plugin.name] = plugin
51  return True
52  else:
53  self.print_errorprint_error("%s setup failed"% plugin.name)
54  else:
55  self.print_errorprint_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.PLUGINSPLUGINS:
67  plugin = self.PLUGINSPLUGINS[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.PLUGINSPLUGINS:
79  plugin = self.PLUGINSPLUGINS[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.PLUGINSPLUGINS.keys()):
102  name = self.PLUGINSPLUGINS[plugin_name].name
103  type = self.PLUGINSPLUGINS[plugin_name].type
104  stable = self.PLUGINSPLUGINS[plugin_name].stable
105  capabilities = ', '.join(self.PLUGINSPLUGINS[plugin_name].capabilities)
106  is_os_supported = self.PLUGINSPLUGINS[plugin_name].is_os_supported()
107  required_parameters = ', '.join(self.PLUGINSPLUGINS[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.PLUGINSPLUGINS.keys()):
116  name = self.PLUGINSPLUGINS[plugin_name].name
117  type = self.PLUGINSPLUGINS[plugin_name].type
118  stable = self.PLUGINSPLUGINS[plugin_name].stable
119  capabilities = self.PLUGINSPLUGINS[plugin_name].capabilities
120  is_os_supported = self.PLUGINSPLUGINS[plugin_name].is_os_supported()
121  required_parameters = self.PLUGINSPLUGINS[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_stringget_string()
def call_plugin(self, type, capability, *args, **kwargs)
Execute plugin functionality respectively to its purpose.
def print_error(self, text)
Prints error directly on console.
def get_plugin_caps(self, type)
Returns list of all capabilities for plugin family with the same type.
def register_plugin(self, plugin)
Registers and stores plugin inside registry for further use.
def load_plugin(self, name)
Used to load module from system (by import)
def get_string(self)
User friendly printing method to show hooked plugins.