Browse Source

review yaml import statement

nico 8 years ago
parent
commit
9b2c5317b7

+ 9 - 6
Docs/brain.md

@@ -104,18 +104,21 @@ When you will pronounce "say hello", it will trigger both synapses.
 If you want a better visibly, or simply sort your actions with different file, you can split the main brain file into multiple one.
 
 To do that, use the import statement in the entry brain.yml file with the following syntax:
-
 ```
 ---
-  - !include path/to/sub_brain.yml
-  - !include path/to/another_sub_brain.yml
+  - includes:
+      - path/to/sub_brain.yml
+      - path/to/another_sub_brain.yml
 ```
 
 E.g:
 ```
 ---
-  - !include brains/rolling_shutter_commands.yml
-  - !include brains/find_my_phone.yml
+  - includes:
+      - brains/rolling_shutter_commands.yml
+      - brains/find_my_phone.yml
 ```
 
->**Note:** You can only use the `!include` statement in the main brain file. 
+>**Note:** You can only use the `include` statement in the main brain file. 
+
+>**Note:** the includes statement must start with a `-`

+ 14 - 14
brain.yml

@@ -1,15 +1,15 @@
 ---
-
-  !include brains/ansible_playbook.yml
-  !include brains/gmail_checker.yml
-  !include brains/kill_switch.yml
-  !include brains/openweathermap.yml
-  !include brains/push_message.yml
-  !include brains/say.yml
-  !include brains/script.yml
-  !include brains/shell.yml
-  !include brains/systemdate.yml
-  !include brains/tasker_autoremote.yml
-  !include brains/wake_on_lan.yml
-  !include brains/twitter.yml
-  !include brains/neurotransmitter.yml
+- includes:
+  - brains/ansible_playbook.yml
+  - brains/gmail_checker.yml
+  - brains/kill_switch.yml
+  - brains/openweathermap.yml
+  - brains/push_message.yml
+  - brains/say.yml
+  - brains/script.yml
+  - brains/shell.yml
+  - brains/systemdate.yml
+  - brains/tasker_autoremote.yml
+  - brains/wake_on_lan.yml
+  - brains/twitter.yml
+  - brains/neurotransmitter.yml

+ 8 - 8
core/ConfigurationManager/BrainLoader.py

@@ -41,14 +41,14 @@ class BrainLoader(object):
         # create list of Synapse
         synapses = list()
         for synapses_dict in dict_brain:
-            print synapses_dict
-            if ConfigurationChecker().check_synape_dict(synapses_dict):
-                # print "synapses_dict ok"
-                name = synapses_dict["name"]
-                neurons = cls._get_neurons(synapses_dict["neurons"])
-                signals = cls._get_signals(synapses_dict["signals"])
-                new_synapse = Synapse(name=name, neurons=neurons, signals=signals)
-                synapses.append(new_synapse)
+            if "includes" not in synapses_dict: # we don't need to check includes as it's not a synapse
+                if ConfigurationChecker().check_synape_dict(synapses_dict):
+                    # print "synapses_dict ok"
+                    name = synapses_dict["name"]
+                    neurons = cls._get_neurons(synapses_dict["neurons"])
+                    signals = cls._get_signals(synapses_dict["signals"])
+                    new_synapse = Synapse(name=name, neurons=neurons, signals=signals)
+                    synapses.append(new_synapse)
         brain.synapses = synapses
         if file_path is None:
             brain.brain_file = cls._get_root_brain_path()

+ 1 - 1
core/ConfigurationManager/ConfigurationChecker.py

@@ -50,7 +50,7 @@ class ConfigurationChecker:
 
         # check that the name is conform
         # Regex for [a - zA - Z0 - 9\-] with dashes allowed in between but not at the start or end
-        pattern = r'(?=[a-zA-Z0-9\-]{4,25}$)^[a-zA-Z0-9]+(\-[a-zA-Z0-9]+)*$'
+        pattern = r'(?=[a-zA-Z0-9\-]{4,100}$)^[a-zA-Z0-9]+(\-[a-zA-Z0-9]+)*$'
         prog = re.compile(pattern)
         result = prog.match(synape_dict["name"])
         if result is None:

+ 1 - 1
core/ConfigurationManager/SettingLoader.py

@@ -253,7 +253,7 @@ class SettingLoader(object):
                     raise SettingInvalidException("port must be in range 1024-65535")
 
             except KeyError, e:
-                print e
+                # print e
                 raise SettingNotFound("%s settings not found" % e)
 
             # config ok, we can return the rest api object

+ 30 - 3
core/ConfigurationManager/YAMLLoader.py

@@ -39,8 +39,9 @@ class YAMLLoader:
         file_path_to_load = os.path.join(root_dir, yaml_file)
         logger.debug("File path to load: %s " % file_path_to_load)
         if os.path.isfile(yaml_file):
-            data = IncludeLoader(open(file_path_to_load, 'r')).get_data()
+            # data = IncludeLoader(open(file_path_to_load, 'r')).get_data()
             # print Utils.print_yaml_nicely(data)
+            data = IncludeImport(file_path_to_load).get_data()
             return data
         else:
             raise YAMLFileNotFound("File %s not found" % file_path_to_load)
@@ -50,7 +51,7 @@ class IncludeLoader(yaml.Loader):
 
     def __init__(self, *args, **kwargs):
         super(IncludeLoader, self).__init__(*args, **kwargs)
-        self.add_constructor('!include', self._include)
+        self.add_constructor('#include', self._include)
         if 'root' in kwargs:
             self.root = kwargs['root']
         elif isinstance(self.stream, file):
@@ -64,4 +65,30 @@ class IncludeLoader(yaml.Loader):
         self.root = os.path.dirname(filename)
         data = yaml.load(open(filename, 'r'))
         self.root = oldRoot
-        return data
+        return data
+
+
+class IncludeImport(object):
+
+    def __init__(self, file_path):
+
+        self.data = yaml.load(open(file_path, 'r'))
+
+        print "content: %s" % self.data
+        if isinstance(self.data, list):
+            for el in self.data:
+                if "includes" in el:
+                    for inc in el["includes"]:
+                        self.update(yaml.load(open(inc)))
+
+    def get_data(self):
+        return self.data
+
+    def update(self, data_to_add):
+        # print "cur_data: %s" % self.data
+        # print "data to add %s" % data_to_add
+        # we add each synapse inside the extended brain into the main brain data
+        for el in data_to_add:
+            self.data.append(el)
+        # print "final data: %s" % self.data
+