Browse Source

remove unnecessary parameter in event, get real path of jarvi.py to generate the crontab command

nico 8 years ago
parent
commit
c74e0ae538

+ 1 - 1
core/ConfigurationManager/BrainLoader.py

@@ -87,7 +87,7 @@ class BrainLoader(YAMLLoader):
             # print "is event"
             event = signal_or_event_dict["event"]
             if ConfigurationChecker.check_event_dict(event):
-                return Event(period=event["period"])
+                return Event(period=event)
 
         if 'order' in signal_or_event_dict:
             order = signal_or_event_dict["order"]

+ 19 - 6
core/ConfigurationManager/ConfigurationChecker.py

@@ -1,3 +1,6 @@
+import re
+
+
 class NoSynapeName(Exception):
     pass
 
@@ -26,6 +29,10 @@ class MultipleSameSynapseName(Exception):
     pass
 
 
+class NotValidSynapseName(Exception):
+    pass
+
+
 class ConfigurationChecker:
 
     def __init__(self):
@@ -58,9 +65,8 @@ class ConfigurationChecker:
 
     @staticmethod
     def check_event_dict(event_dict):
-        # if 'id' not in event_dict:
-        #     raise NoEventID("Event must contain a unique ID: %s" % event_dict)
-        if 'period' not in event_dict:
+        print event_dict
+        if event_dict is None:
             raise NoEventPeriod("Event must contain a period: %s" % event_dict)
 
         return True
@@ -74,14 +80,21 @@ class ConfigurationChecker:
     @staticmethod
     def check_synapes(synapses_list):
         """
-        Check the synapse list is ok. No double same name
+        Check the synapse list is ok:
+         - No double same name
+         - No accent of special character
         :param synapses_list:
         :type synapses_list: list of Synapse
         :return:
         """
         seen = set()
         for synapse in synapses_list:
-            if synapse.name in seen:
-                raise MultipleSameSynapseName("Synapse with same name: %s" % synapse.name)
+            # convert ascii to UTF-8
+            synapse_name = synapse.name.encode('utf-8')
+            if synapse_name in seen:
+                raise MultipleSameSynapseName("Multiple synapse found with the same name: %s" % synapse_name)
             seen.add(synapse.name)
+            if not re.match("^[a-zA-Z0-9_\s]*$", synapse.name):
+                raise NotValidSynapseName("Synapse's name %s not valid." % synapse_name)
+
         return True

+ 23 - 2
core/CrontabManager.py

@@ -10,13 +10,14 @@ class InvalidCrontabPeriod(Exception):
     pass
 
 CRONTAB_COMMENT = "JARVIS"
+JARVIS_ENTRY_POINT_SCRIPT = "jarvis.py"
 
 
 class CrontabManager:
 
     def __init__(self, brain_file=None):
         self.my_user_cron = CronTab(user=True)
-        self.base_command = "/path/to/jarvis/"
+        self.base_command = self._get_base_command()
         self.brain = BrainLoader(filename=brain_file).get_brain()
 
     def load_events_in_crontab(self):
@@ -38,7 +39,7 @@ class CrontabManager:
 
     def _add_event(self, period_string, event_id):
         my_user_cron = CronTab(user=True)
-        job = my_user_cron.new(command=self.base_command+" "+str("\""+ event_id + "\""), comment=CRONTAB_COMMENT)
+        job = my_user_cron.new(command=self.base_command+" "+str("\"" + event_id + "\""), comment=CRONTAB_COMMENT)
         if CronSlices.is_valid(period_string):
             job.setall(period_string)
             job.enable()
@@ -68,3 +69,23 @@ class CrontabManager:
         sum_job = sum(1 for _ in new_iter)
         while sum_job > 0:
             self._remove_all_jarvis_job()
+
+    def _get_base_command(self):
+        """
+        Return the path of the entry point of Jarvis
+        Example: /home/user/jarvis/jarvis.py
+        :return: The path of the entry point script jarvis.py
+        """
+        import inspect
+        import os
+        # get current script directory path. We are in /an/unknown/path/jarvis/core
+        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)
+        # we add the jarvis.py file name
+        real_jarvis_entry_point_path = parent_dir + os.sep + JARVIS_ENTRY_POINT_SCRIPT
+        # 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
+        raise IOError("jarvis.py file not found")

+ 1 - 1
jarvis.py

@@ -20,7 +20,7 @@ def main():
     # create arguments
     parser = argparse.ArgumentParser(description='JARVIS')
     parser.add_argument("action", help="[start|gui]")
-    parser.add_argument("--synapse", help="SYNAPSE. Name of a synapse to load in quote")
+    parser.add_argument("--run-synapse", help="SYNAPSE. Name of a synapse to load surrounded by quote")
     parser.add_argument("--brain-file", help="BRAIN_PATH_FILE")
 
     # parse arguments from script parameters

+ 2 - 3
test.yml

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