Exemplo n.º 1
0
 def dic_add_key(self, key, value, append=False, prepend=False, replace=False):
     """Add the value to the key in the dictionnary, parse the key to create sub dictionnaries.
      Append the value if append is set to True.
      Prepend the value if prepend is set to True.
      Does not generate a warning when the key already exists if replace is set to True """
     current_dic = self.dic
     sub_keys = key.split(".")
     for k in sub_keys:
         if k == '':
             raise SyntaxErrorPL(self.path_parsed_file, self.lines[self.lineno - 1], self.lineno)
     for k in sub_keys[:-1]:  # creating sub dictionnaries
         current_dic[k] = current_dic.get(k, dict())
         current_dic = current_dic[k]
     last_key = sub_keys[-1]
     
     if last_key in current_dic and not append and not prepend and not replace:
         self.add_warning("Key '" + key + "' overwritten at line " + str(self.lineno))
     if append:
         if last_key not in current_dic:
             line = self._multiline_opened_lineno if self._multiline_key else self.lineno
             error = "Trying to append to non-existent key '" + key + "'."
             raise SemanticError(self.path_parsed_file, self.lines[line - 1], line, error)
         current_dic[last_key] += value
     elif prepend:
         if last_key not in current_dic:
             line = self._multiline_opened_lineno if self._multiline_key else self.lineno
             error = "Trying to prepend to non-existent key '" + key + "'."
             raise SemanticError(self.path_parsed_file, self.lines[line - 1], line, error)
         current_dic[last_key] = value + current_dic[last_key]
     else:
         current_dic[last_key] = value
Exemplo n.º 2
0
    def test_semantic_error_str(self):
        path = 'first1/second1'
        line = '3'
        lineno = '45'
        message = "Semantic error"
        str_message = path + " -- " + message + " at line " + lineno + "\n" + line

        self.assertEqual(str(SemanticError(path, line, lineno, message)),
                         str_message)
Exemplo n.º 3
0
    def from_file_line_match(self, match, line):
        """ Map (or append) the content if the file corresponding to file)
            to the key
            
            Raise from loader.exceptions:
                - SyntaxErrorPL if no group 'file' or 'key' was found
                - SemanticError if trying to append a nonexistent key
                - DirectoryNotFound if trying to load from a nonexistent directory
                - FileNotFound if the given file do not exists."""

        if not match.group('file') or not match.group(
                'key') or not match.group('operator'):
            raise SyntaxErrorPL(self.path_parsed_file, line, self.lineno)

        key = match.group('key')
        op = match.group('operator')

        # Add warning when overwritting a key
        if key in self.dic and '+' not in op:
            self.add_warning("Key '" + key + "' overwritten at line " +
                             str(self.lineno))

        try:
            directory, path = get_location(self.directory,
                                           match.group('file'),
                                           current=self.path_parsed_file)
            path = abspath(
                join(directory.root, path.replace(directory.name + '/', '')))
            with open(path, 'r') as f:
                if '+' in op:
                    if not key in self.dic:
                        raise SemanticError(
                            self.path_parsed_file, line, self.lineno,
                            "Trying to append to non-existent key '" + key +
                            "'.")
                    self.dic[key] += f.read()
                else:
                    self.dic[key] = f.read()
        except ObjectDoesNotExist:
            raise DirectoryNotFound(self.path_parsed_file, line,
                                    match.group('file'), self.lineno)
        except FileNotFoundError:
            raise FileNotFound(self.path_parsed_file,
                               line,
                               path,
                               lineno=self.lineno)
        except ValueError:
            raise FileNotFound(
                self.path_parsed_file,
                line,
                match.group('file'),
                lineno=self.lineno,
                message="Path from another directory must be absolute")