You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

host_test_registry.py 3.4KB

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