NeuroneLauncher.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. class NeuroneNotFoundError(Exception):
  2. pass
  3. def _run_plugin(plugin, parameters=None):
  4. """
  5. Dynamic loading of a module
  6. :param plugin: Module name to load
  7. :param parameters: Parameter of the module
  8. :return:
  9. """
  10. print "Run plugin %s with parameter %s" % (plugin, parameters)
  11. mod = __import__('neurons', fromlist=[plugin])
  12. try:
  13. klass = getattr(mod, plugin)
  14. except AttributeError:
  15. print "Error: No module named %s " % plugin
  16. raise NeuroneNotFoundError
  17. if klass is not None:
  18. # run the plugin
  19. if not parameters:
  20. klass()
  21. elif isinstance(parameters, dict):
  22. klass(**parameters)
  23. else:
  24. klass(parameters)
  25. class NeuroneLauncher:
  26. def __init__(self):
  27. pass
  28. @staticmethod
  29. def start_neurone(neuron):
  30. """
  31. Neuron dict to start. {'neurone_name': {'args1': 'value1', 'args2': 'value2'}}
  32. :param neuron: Dict with neuron declaration
  33. :type neuron: Dict
  34. :return:
  35. """
  36. if isinstance(neuron, dict):
  37. for plugin, parameters in neuron.items():
  38. # capitalizes the first letter (because classes have first letter upper case)
  39. plugin = plugin.capitalize()
  40. _run_plugin(plugin, parameters)
  41. else:
  42. plugin = neuron
  43. # capitalizes the first letter (because classes have first letter upper case)
  44. plugin = plugin.capitalize()
  45. _run_plugin(plugin)