OrderAnalyser.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import re
  2. from core.NeuroneLauncher import NeuroneLauncher
  3. class OrderAnalyser:
  4. def __init__(self, order, main_controller=None):
  5. """
  6. Class used to load
  7. :param order: spelt order
  8. :param main_controller
  9. """
  10. self.main_controller = main_controller
  11. self.order = order
  12. self.brain = main_controller.conf.brainLoader.get_config()
  13. print "Receiver order: %s" % self.order
  14. def start(self):
  15. print self.brain
  16. for el in self.brain:
  17. # print el
  18. # print el["when"]
  19. whens = el["when"]
  20. for when in whens:
  21. brain_order = when["order"]
  22. print "order to test: %s" % brain_order
  23. if self._spelt_order_match_brain_order(brain_order):
  24. print "Order found! Run neurons: %s" % el["neurons"]
  25. neurons = el["neurons"]
  26. for neuron in neurons:
  27. NeuroneLauncher().start_neurone(neuron)
  28. # once we ran all plugin, we can start back jarvis trigger
  29. if self.main_controller is not None:
  30. self.main_controller.unpause_jarvis_trigger()
  31. def _spelt_order_match_brain_order(self, order_to_test):
  32. """
  33. test if the current order match the order spelt by the user
  34. :param order_to_test:
  35. :return:
  36. """
  37. my_regex = r"\b(?=\w)" + re.escape(order_to_test) + r"\b(?!\w)"
  38. if re.search(my_regex, self.order, re.IGNORECASE):
  39. return True
  40. return False