Exemplo n.º 1
0
    def parse(self, line):
        """
        Note that every value in the returned touple *besides the value* is
        unescaped. This is so since the value is meant to be put into a Setting
        later thus the escapes may be needed there.

        :param line: the line to parse
        :return:     section_name (empty string if it's no section name),
                     [(section_override, key), ...], value, comment
        """
        line, comment = self.__seperate_by_first_occurrence(
            line,
            self.comment_seperators)
        comment = unescape(comment)
        if line == "":
            return '', [], '', comment

        section_name = unescape(self.__get_section_name(line))
        if section_name != '':
            return section_name, [], '', comment

        # Escapes in value might be needed by the bears
        keys, value = self.__extract_keys_and_value(line)

        # Add all the delimiters that stored as tuples
        all_delimiters = self.key_value_delimiters
        all_delimiters += self.key_delimiters
        all_delimiters += self.comment_seperators
        all_delimiters += self.section_override_delimiters
        all_delimiters = "".join(all_delimiters)

        # Add all keys and values in section_name_surroundings, which is
        # stored as a dict
        all_delimiters += "".join(self.section_name_surroundings.keys())
        all_delimiters += "".join(self.section_name_surroundings.values())

        value = convert_to_raw(value, all_delimiters)

        key_touples = []
        for key in keys:
            key = convert_to_raw(key, all_delimiters)
            section, key = self.__seperate_by_first_occurrence(
                key,
                self.section_override_delimiters,
                True,
                True)
            key_touples.append((unescape(section), unescape(key)))

        return '', key_touples, value, comment
Exemplo n.º 2
0
    def parse(self, line):
        """
        Note that every value in the returned touple *besides the value* is
        unescaped. This is so since the value is meant to be put into a Setting
        later thus the escapes may be needed there.

        :param line: the line to parse
        :return:     section_name (empty string if it's no section name),
                     [(section_override, key), ...], value, comment
        """
        line, comment = self.__seperate_by_first_occurrence(
            line, self.comment_seperators)
        comment = unescape(comment)
        if line == "":
            return '', [], '', comment

        section_name = unescape(self.__get_section_name(line))
        if section_name != '':
            return section_name, [], '', comment

        # Escapes in value might be needed by the bears
        keys, value = self.__extract_keys_and_value(line)

        # Add all the delimiters that stored as tuples
        all_delimiters = self.key_value_delimiters
        all_delimiters += self.key_delimiters
        all_delimiters += self.comment_seperators
        all_delimiters += self.section_override_delimiters
        all_delimiters = "".join(all_delimiters)

        # Add all keys and values in section_name_surroundings, which is
        # stored as a dict
        all_delimiters += "".join(self.section_name_surroundings.keys())
        all_delimiters += "".join(self.section_name_surroundings.values())

        value = convert_to_raw(value, all_delimiters)

        key_touples = []
        for key in keys:
            key = convert_to_raw(key, all_delimiters)
            section, key = self.__seperate_by_first_occurrence(
                key, self.section_override_delimiters, True, True)
            key_touples.append((unescape(section), unescape(key)))

        return '', key_touples, value, comment
Exemplo n.º 3
0
 def test_convert_to_raw(self):
     # In (input, output) format
     test_data = [(r"test", r"test"), (r"test_path", r"test_path"),
                  (r"test, path", r"test, path"),
                  (r"test\ path", r"test\ path"),
                  (r"test\path", r"test\\path"),
                  (r"test\\path", r"test\\path"),
                  (r"test\=path", r"test\=path"),
                  (r"test=path", r"test=path"),
                  (r"value\=as\something", r"value\=as\\something")]
     for test in test_data:
         self.assertEqual(convert_to_raw(test[0], ",.=# "), test[1])
Exemplo n.º 4
0
 def test_convert_to_raw(self):
     # In (input, output) format
     test_data = [
         (r"test", r"test"),
         (r"test_path", r"test_path"),
         (r"test, path", r"test, path"),
         (r"test\ path", r"test\ path"),
         (r"test\path", r"test\\path"),
         (r"test\\path", r"test\\path"),
         (r"test\=path", r"test\=path"),
         (r"test=path", r"test=path"),
         (r"value\=as\something", r"value\=as\\something")]
     for test in test_data:
         self.assertEqual(convert_to_raw(test[0], ",.=# "), test[1])