Ejemplo n.º 1
0
    def _parse(self, line):
        """
        Note that every value in the returned tuple *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, to_append (True if
                     append delimiter is found else False), comment
        """
        line, comment = self.__separate_by_first_occurrence(
            line,
            self.comment_separators)
        comment = unescape(comment)
        if line == '':
            return '', [], '', False, comment

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

        # Escapes in value might be needed by the bears
        append = True
        keys, value = self.__extract_keys_and_value(
            line, self.key_value_append_delimiters)
        if not value:
            keys, value = self.__extract_keys_and_value(
                line, self.key_value_delimiters, True)
            append = False

        # Add all the delimiters that stored as tuples
        all_delimiters = self.key_value_delimiters
        all_delimiters += self.key_value_append_delimiters
        all_delimiters += self.key_delimiters
        all_delimiters += self.comment_separators
        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_tuples = []
        for key in keys:
            key = convert_to_raw(key, all_delimiters)
            section, key = self.__separate_by_first_occurrence(
                key,
                self.section_override_delimiters,
                True,
                True)
            key_tuples.append((unescape(section), unescape(key)))

        return '', key_tuples, value, append, comment
Ejemplo n.º 2
0
    def _parse(self, line):
        """
        Note that every value in the returned tuple *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, to_append (True if
                     append delimiter is found else False), comment
        """
        line, comment = self.__separate_by_first_occurrence(
            line, self.comment_separators)
        comment = unescape(comment)
        if line == '':
            return '', [], '', False, comment

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

        # Escapes in value might be needed by the bears
        append = True
        keys, value = self.__extract_keys_and_value(
            line, self.key_value_append_delimiters)
        if not value:
            keys, value = self.__extract_keys_and_value(
                line, self.key_value_delimiters, True)
            append = False

        # Add all the delimiters that stored as tuples
        all_delimiters = self.key_value_delimiters
        all_delimiters += self.key_value_append_delimiters
        all_delimiters += self.key_delimiters
        all_delimiters += self.comment_separators
        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_tuples = []
        for key in keys:
            key = convert_to_raw(key, all_delimiters)
            section, key = self.__separate_by_first_occurrence(
                key, self.section_override_delimiters, True, True)
            key_tuples.append((unescape(section), unescape(key)))

        return '', key_tuples, value, append, comment
Ejemplo 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])
Ejemplo n.º 4
0
    def _parse(self, line):
        """
        Note that every value in the returned tuple *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, to_append (True if
                     append delimiter is found else False), comment
        """
        for separator in self.comment_separators:
            if (re.match('[^ ]' + separator, line)
                    or re.match(separator + '[^ ]', line)):
                logging.warning('This comment does not have whitespace' +
                                ' before or after ' + separator + ' in: ' +
                                repr(line.replace('\n', '')) + '. If you ' +
                                'didn\'t mean to make a comment, use a ' +
                                'backslash for escaping.')

        line, comment = self.__separate_by_first_occurrence(
            line,
            self.comment_separators)
        comment = unescape(comment)
        if line == '':
            return '', [], '', False, comment

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

        # Escapes in value might be needed by the bears
        append = True
        keys, value = self.__extract_keys_and_value(
            line, self.key_value_append_delimiters)
        if not value:
            keys, value = self.__extract_keys_and_value(
                line, self.key_value_delimiters, True)
            append = False

        # Add all the delimiters that stored as tuples
        all_delimiters = self.key_value_delimiters
        all_delimiters += self.key_value_append_delimiters
        all_delimiters += self.key_delimiters
        all_delimiters += self.comment_separators
        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_tuples = []
        for key in keys:
            key = convert_to_raw(key, all_delimiters)
            section, key = self.__separate_by_first_occurrence(
                key,
                self.section_override_delimiters,
                True,
                True)
            key_tuples.append((unescape(section), unescape(key)))

        return '', key_tuples, value, append, comment
Ejemplo n.º 5
0
    def _parse(self, line):
        """
        Note that every value in the returned tuple *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, to_append (True if
                     append delimiter is found else False), comment
        """
        for separator in self.comment_separators:
            if (re.match('[^ ]' + separator, line)
                    or re.match(separator + '[^ ]', line)):
                logging.warning('This comment does not have whitespace' +
                                ' before or after ' + separator + ' in: ' +
                                repr(line.replace('\n', '')) + '. If you ' +
                                'didn\'t mean to make a comment, use a ' +
                                'backslash for escaping.')

        line, comment = self.__separate_by_first_occurrence(
            line, self.comment_separators)
        comment = unescape(comment)
        if line == '':
            return '', [], '', False, comment

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

        # Escapes in value might be needed by the bears
        append = True
        keys, value = self.__extract_keys_and_value(
            line, self.key_value_append_delimiters)
        if not value:
            keys, value = self.__extract_keys_and_value(
                line, self.key_value_delimiters, True)
            append = False

        # Add all the delimiters that stored as tuples
        all_delimiters = self.key_value_delimiters
        all_delimiters += self.key_value_append_delimiters
        all_delimiters += self.key_delimiters
        all_delimiters += self.comment_separators
        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_tuples = []
        for key in keys:
            key = convert_to_raw(key, all_delimiters)
            section, key = self.__separate_by_first_occurrence(
                key, self.section_override_delimiters, True, True)
            key_tuples.append((unescape(section), unescape(key)))

        return '', key_tuples, value, append, comment