Переглянути джерело

crontab feature ok. brain model now know the path of the current used brain

nico 8 роки тому
батько
коміт
c1b643e75c
6 змінених файлів з 49 додано та 18 видалено
  1. 8 0
      brain.yml
  2. 29 9
      core/ConfigurationManager/BrainLoader.py
  3. 4 2
      core/CrontabManager.py
  4. 2 1
      core/Models/Brain.py
  5. 5 5
      test.py
  6. 1 1
      test.yml

+ 8 - 0
brain.yml

@@ -1,4 +1,12 @@
 ---
+  - name: "say hello"
+    neurons:
+      - say:
+          message:
+            - "Bonjour monsieur"
+    signals:
+      - event: "57 22 * * *"
+      - order: "say hello"
 
   - name: "Meaning of life"
     neurons:

+ 29 - 9
core/ConfigurationManager/BrainLoader.py

@@ -1,3 +1,6 @@
+import inspect
+import logging
+import os
 
 from YAMLLoader import YAMLLoader
 from core.ConfigurationManager.ConfigurationChecker import ConfigurationChecker
@@ -7,17 +10,15 @@ from core.Models.Neuron import Neuron
 from core.Models.Order import Order
 from core.Models.Synapse import Synapse
 
-FILE_NAME = "brain.yml"
-
 
 class BrainLoader(YAMLLoader):
 
-    def __init__(self, filename=None):
-        self.fileName = filename
-        if filename is None:
-            self.fileName = FILE_NAME
-        self.filePath = "../../" + self.fileName
-        YAMLLoader.__init__(self, self.filePath)
+    def __init__(self, filepath=None):
+        self.brain_file_path = filepath
+        if filepath is None:
+            self.brain_file_path = self._get_root_brain_path()
+        # self.filePath = "../../" + self.fileName
+        YAMLLoader.__init__(self, self.brain_file_path)
 
     def get_config(self):
         return YAMLLoader.get_config(self)
@@ -44,12 +45,14 @@ class BrainLoader(YAMLLoader):
                 new_synapse = Synapse(name=name, neurons=neurons, signals=signals)
                 synapses.append(new_synapse)
         brain.synapes = synapses
+        brain.brain_file = self.brain_file_path
         # check that no synapse have the same name than another
         if ConfigurationChecker().check_synapes(synapses):
             return brain
         return None
 
-    def _get_neurons(self, neurons_dict):
+    @staticmethod
+    def _get_neurons(neurons_dict):
         """
         Get a list of Neuron object from a neuron dict
         :param neurons_dict:
@@ -94,4 +97,21 @@ class BrainLoader(YAMLLoader):
             if ConfigurationChecker.check_order_dict(order):
                 return Order(sentence=order)
 
+    @staticmethod
+    def _get_root_brain_path():
+        """
+        Return the full path of the default brain file
+        :return:
+        """
+        # get current script directory path. We are in /an/unknown/path/jarvis/core/ConfigurationManager
+        cur_script_directory = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
+        # get parent dir. Now we are in /an/unknown/path/jarvis
+        parent_dir = os.path.normpath(cur_script_directory + os.sep + os.pardir)
+        brain_path = parent_dir + "brain.yml"
+        logging.debug("Real brain.yml path: %s" % brain_path)
+        if os.path.isfile(brain_path):
+            return brain_path
+        raise IOError("Default brain.yml file not found")
+
+
 

+ 4 - 2
core/CrontabManager.py

@@ -17,8 +17,8 @@ class CrontabManager:
 
     def __init__(self, brain_file=None):
         self.my_user_cron = CronTab(user=True)
+        self.brain = BrainLoader(filepath=brain_file).get_brain()
         self.base_command = self._get_base_command()
-        self.brain = BrainLoader(filename=brain_file).get_brain()
 
     def load_events_in_crontab(self):
         """
@@ -87,5 +87,7 @@ class CrontabManager:
         # We test that the file exist before return it
         logging.debug("Real jarvis.py path: %s" % real_jarvis_entry_point_path)
         if os.path.isfile(real_jarvis_entry_point_path):
-            return real_jarvis_entry_point_path
+            crontab_cmd = "python %s start --brain-file %s --run-synapse " % (real_jarvis_entry_point_path,
+                                                                              self.brain.brain_file)
+            return crontab_cmd
         raise IOError("jarvis.py file not found")

+ 2 - 1
core/Models/Brain.py

@@ -1,4 +1,5 @@
 
 class Brain(object):
-    def __init__(self, synapes=None):
+    def __init__(self, synapes=None, brain_file=None):
         self.synapes = synapes
+        self.brain_file = brain_file

+ 5 - 5
test.py

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

+ 1 - 1
test.yml

@@ -5,5 +5,5 @@
           message:
             - "Bonjour monsieur"
     signals:
-      - event: "* * * 5 *"
+      - event: "23 23 * * *"
       - order: "say hello"