Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers host_test_registry.py Source File

host_test_registry.py

00001 """
00002 mbed SDK
00003 Copyright (c) 2011-2013 ARM Limited
00004 
00005 Licensed under the Apache License, Version 2.0 (the "License");
00006 you may not use this file except in compliance with the License.
00007 You may obtain a copy of the License at
00008 
00009     http://www.apache.org/licenses/LICENSE-2.0
00010 
00011 Unless required by applicable law or agreed to in writing, software
00012 distributed under the License is distributed on an "AS IS" BASIS,
00013 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014 See the License for the specific language governing permissions and
00015 limitations under the License.
00016 """
00017 from __future__ import print_function
00018 
00019 class HostTestRegistry :
00020     """ Simple class used to register and store
00021         host test plugins for further usage
00022     """
00023     # Here we actually store all the plugins
00024     PLUGINS = {}    # 'Plugin Name' : Plugin Object
00025 
00026     def print_error(self, text):
00027         print("Plugin load failed. Reason: %s" % text)
00028 
00029     def register_plugin (self, plugin):
00030         """ Registers and stores plugin inside registry for further use.
00031             Method also calls plugin's setup() function to configure plugin if needed.
00032 
00033             Note: Different groups of plugins may demand different extra parameter. Plugins
00034             should be at least for one type of plugin configured with the same parameters
00035             because we do not know which of them will actually use particular parameter.
00036         """
00037         # TODO:
00038         # - check for unique caps for specified type
00039         if plugin.name not in self.PLUGINS :
00040             if plugin.setup(): # Setup plugin can be completed without errors
00041                 self.PLUGINS [plugin.name] = plugin
00042                 return True
00043             else:
00044                 self.print_error ("%s setup failed"% plugin.name)
00045         else:
00046             self.print_error ("%s already loaded"% plugin.name)
00047         return False
00048 
00049     def call_plugin (self, type, capability, *args, **kwargs):
00050         """ Execute plugin functionality respectively to its purpose
00051         """
00052         for plugin_name in self.PLUGINS :
00053             plugin = self.PLUGINS [plugin_name]
00054             if plugin.type == type and capability in plugin.capabilities:
00055                 return plugin.execute(capability, *args, **kwargs)
00056         return False
00057 
00058     def get_plugin_caps (self, type):
00059         """ Returns list of all capabilities for plugin family with the same type.
00060             If there are no capabilities empty list is returned
00061         """
00062         result = []
00063         for plugin_name in self.PLUGINS :
00064             plugin = self.PLUGINS [plugin_name]
00065             if plugin.type == type:
00066                 result.extend(plugin.capabilities)
00067         return sorted(result)
00068 
00069     def load_plugin (self, name):
00070         """ Used to load module from
00071         """
00072         mod = __import__("module_%s"% name)
00073         return mod
00074 
00075     def __str__ (self):
00076         """ User friendly printing method to show hooked plugins
00077         """
00078         from prettytable import PrettyTable
00079         column_names = ['name', 'type', 'capabilities', 'stable']
00080         pt = PrettyTable(column_names)
00081         for column in column_names:
00082             pt.align[column] =  'l'
00083         for plugin_name in sorted(self.PLUGINS .keys()):
00084             name  = self.PLUGINS [plugin_name].name
00085             type  = self.PLUGINS [plugin_name].type
00086             stable = self.PLUGINS [plugin_name].stable
00087             capabilities  = ', '.join(self.PLUGINS [plugin_name].capabilities)
00088             row = [name, type, capabilities, stable]
00089             pt.add_row(row)
00090         return pt.get_string()