<commit_before># -*- coding: UTF-8 -*-
# Copyright (C) 2009 Itaapy, ArsAperta, Pierlis, Talend

# Import from lpod
from xmlpart import register_element_class, odf_element
from xmlpart import PREV_SIBLING, NEXT_SIBLING
from document import odf_create_list_item


def get_parent_item(element):
        while element.get_name() != 'text:list-item':
            element = element.get_parent()
        return element



class odf_list(odf_element):
    """Specialised element for lists.
    """

    def get_item_list_by_content(self, regex):
        paragraphs = self.get_element_list('//text:p')
        return [get_parent_item(paragraph) for paragraph in paragraphs
                                               if paragraph.match(regex)]

    def get_item_by_content(self, regex):
        items = self.get_item_list_by_content(regex)
        if items:
            return items[0]
        return None


    def insert_item(self, item, position=None, before=None, after=None):
        # Check if the item is already a list-item
        tag_name = item.get_name() if isinstance(item, odf_element) else None
        if tag_name != 'text:list-item':
            item = odf_create_list_item(item)

        if before is not None:
            before.insert_element(item, xmlposition=PREV_SIBLING)
        elif after is not None:
            after.insert_element(item, xmlposition=NEXT_SIBLING)
        elif position is not None:
            self.insert_element(item, position=position)
        else:
            raise ValueError, "position must be defined"


    def append_item(self, item):
        # Check if the item is already a list-item
        tag_name = item.get_name() if isinstance(item, odf_element) else None
        if tag_name != 'text:list-item':
            item = odf_create_list_item(item)
        self.append_element(item)


    def get_formated_text(self, context):
        formated_text = u''
        for list_item in self.get_element_list('text:list-item'):
            text = u''
            for children in list_item.get_element_list('*'):
                text = u'%s%s' % (text, children.get_formated_text(context))
            text = text.strip('\n')
            # Indent the text
            text = u'- %s\n' % text.replace(u'\n', u'\n  ')
            formated_text = formated_text + text
        return formated_text


<commit_msg>python: odf_list: Improve get_formated_text.<commit_after># -*- coding: UTF-8 -*-
# Copyright (C) 2009 Itaapy, ArsAperta, Pierlis, Talend

# Import from lpod
from xmlpart import register_element_class, odf_element
from xmlpart import PREV_SIBLING, NEXT_SIBLING
from document import odf_create_list_item


def get_parent_item(element):
        while element.get_name() != 'text:list-item':
            element = element.get_parent()
        return element



class odf_list(odf_element):
    """Specialised element for lists.
    """

    def get_item_list_by_content(self, regex):
        paragraphs = self.get_element_list('//text:p')
        return [get_parent_item(paragraph) for paragraph in paragraphs
                                               if paragraph.match(regex)]

    def get_item_by_content(self, regex):
        items = self.get_item_list_by_content(regex)
        if items:
            return items[0]
        return None


    def insert_item(self, item, position=None, before=None, after=None):
        # Check if the item is already a list-item
        tag_name = item.get_name() if isinstance(item, odf_element) else None
        if tag_name != 'text:list-item':
            item = odf_create_list_item(item)

        if before is not None:
            before.insert_element(item, xmlposition=PREV_SIBLING)
        elif after is not None:
            after.insert_element(item, xmlposition=NEXT_SIBLING)
        elif position is not None:
            self.insert_element(item, position=position)
        else:
            raise ValueError, "position must be defined"


    def append_item(self, item):
        # Check if the item is already a list-item
        tag_name = item.get_name() if isinstance(item, odf_element) else None
        if tag_name != 'text:list-item':
            item = odf_create_list_item(item)
        self.append_element(item)


    def get_formated_text(self, context):
        result = u''
        for list_item in self.get_element_list('text:list-item'):
            text = u''
            for children in list_item.get_children():
                text += children.get_formated_text(context)
            text = text.strip('\n')
            # Indent the text
            text = u'- %s\n' % text.replace(u'\n', u'\n  ')
            result += text
        return result



{'fraction_non_alphanumeric': 0.09265996148084742, 'fraction_numerical': 0.0025679435052428845, 'mean_word_length': 2.4067055393586005, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7609188', 'n_tokens_mistral': 1412, 'n_tokens_neox': 1377, 'n_words': 356}