浏览代码

jarvis can now launch a synapse directly from CLI

nico 8 年之前
父节点
当前提交
460c05fb51
共有 5 个文件被更改,包括 72 次插入12 次删除
  1. 0 1
      core/ConfigurationManager/ConfigurationChecker.py
  2. 3 2
      core/MainController.py
  3. 49 0
      core/SynapseLauncher.py
  4. 15 4
      jarvis.py
  5. 5 5
      test.py

+ 0 - 1
core/ConfigurationManager/ConfigurationChecker.py

@@ -65,7 +65,6 @@ class ConfigurationChecker:
 
     @staticmethod
     def check_event_dict(event_dict):
-        print event_dict
         if event_dict is None:
             raise NoEventPeriod("Event must contain a period: %s" % event_dict)
 

+ 3 - 2
core/MainController.py

@@ -7,7 +7,8 @@ from neurons import Say
 
 
 class MainController:
-    def __init__(self):
+    def __init__(self, brain_file=None):
+        self.brain_file = brain_file
         # Manage Global Configuration
         self.conf = ConfigurationManager().get_settings()
 
@@ -50,7 +51,7 @@ class MainController:
         Receive an order, try to retreive it in the brain.yml to launch to attached plugins
         :return:
         """
-        order_analyser = OrderAnalyser(order, main_controller=self)
+        order_analyser = OrderAnalyser(order, main_controller=self, brain_file=self.brain_file)
         order_analyser.start()
 
     def get_analyse_order_callback(self):

+ 49 - 0
core/SynapseLauncher.py

@@ -0,0 +1,49 @@
+from core.ConfigurationManager.BrainLoader import BrainLoader
+from core.NeuroneLauncher import NeuroneLauncher
+
+
+class SynapseNameNotFound(Exception):
+    pass
+
+
+class SynapseLauncher(object):
+
+    def __init__(self):
+        pass
+
+    @classmethod
+    def start_synapse(cls, name, brain_file=None):
+        """
+        Start a synapse by it's name
+        :param name: Name (Unique ID) of the synapse to launch
+        :param brain_file: Brain file path to load instead of the default one
+        """
+        synapse_name_launch = name
+        # get the brain
+        if brain_file is None:
+            brain = BrainLoader().get_brain()
+        else:
+            brain = BrainLoader(brain_file).get_brain()
+
+        # check if we have found and launched the synapse
+        synapse_launched = False
+        for synapse in brain.synapes:
+            if synapse.name == synapse_name_launch:
+                cls._run_synapse(synapse)
+                synapse_launched = True
+                # we found the synapse, we don't need to check the rest of the list
+                break
+
+        if not synapse_launched:
+            raise SynapseNameNotFound("The synapse name \"%s\" does not exist in the brain file" % name)
+
+    @classmethod
+    def _run_synapse(cls, synapse):
+        """
+        Start all neurons in the synapse
+        :param synapse: Synapse for which we run neurons
+        :return:
+        """
+        for neuron in synapse.neurons:
+            NeuroneLauncher.start_neurone(neuron)
+        return True

+ 15 - 4
jarvis.py

@@ -6,6 +6,8 @@ from core.MainController import MainController
 import signal
 import sys
 
+from core.SynapseLauncher import SynapseLauncher
+
 
 def signal_handler(signal, frame):
         print "\n"
@@ -30,17 +32,26 @@ def main():
         parser.print_usage()
         sys.exit(1)
 
+    # by default, no brain file is set. Use the default one: brain.yml in the root path
+    brain_file = None
+
     if args.action == "start":
+        # check if user set a brain.yml file
+        if args.brain_file:
+            print "Brain file arg: %s" % args.brain_file
+            brain_file = args.brain_file
+
         # user set a synapse to start
-        if args.synapse is not None:
-            print "Playing synapse: %s" % args.synapse
+        if args.run_synapse is not None:
+            print "Run synapse arg: %s" % args.run_synapse
+            SynapseLauncher.start_synapse(args.run_synapse, brain_file=brain_file)
 
-        if args.synapse is None:
+        if args.run_synapse is None:
             print "Starting JARVIS. Press Ctrl+C for stopping"
             # catch signal for killing on Ctrl+C pressed
             signal.signal(signal.SIGINT, signal_handler)
             # start the main controller
-            main_controller = MainController()
+            main_controller = MainController(brain_file=brain_file)
             main_controller.start()
 
     if args.action == "gui":

+ 5 - 5
test.py

@@ -15,9 +15,9 @@ logger.setLevel(logging.DEBUG)
 #
 # oa.start()
 
-# cmd = "python jarvis.py start --synapse \"this is my synapse\""
-#
-# os.system(cmd)
+cmd = "python jarvis.py start --run-synapse \"say hello\" --brain-file test.yml"
+
+os.system(cmd)
 
-crontab_manager = CrontabManager(brain_file="test.yml")
-crontab_manager.load_events_in_crontab()
+# crontab_manager = CrontabManager(brain_file="test.yml")
+# crontab_manager.load_events_in_crontab()