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) key_touples = [] for key in keys: 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
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
def __prepare_list(self): self.__escaped_list = self.__get_raw_list() if self.__strip_whitespaces: self.__escaped_list = [unescaped_strip(elem) for elem in self.__escaped_list] self.__unescaped_list = [unescape(elem) for elem in self.__escaped_list] if self.__remove_empty_iter_elements: # Need to do after stripping, cant use builtin functionality of # split. while "" in self.__unescaped_list: self.__unescaped_list.remove("") while "" in self.__escaped_list: self.__escaped_list.remove("")
def __prepare_dict(self): # We must keep order here, user can drop it later. self.__dict = OrderedDict() for elem in self.__get_raw_list(): key_val = unescaped_split(self.__dict_delimiter, elem, max_split=1) if self.__strip_whitespaces: key_val = [unescaped_strip(item) for item in key_val] key_val = [unescape(item) for item in key_val] if not any(item != "" for item in key_val): continue if len(key_val) < 2: self.__dict[key_val[0]] = "" else: self.__dict[key_val[0]] = key_val[1]
def __str__(self): return unescape(self.value)
def test_extended(self): self.assertEqual(unescape("hello\\"), "hello") self.assertEqual(unescape("te\\st\\\\"), "test\\") self.assertEqual(unescape("\\\\\\"), "\\")