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

# Import from the Standard Library
from copy import deepcopy

# Import from lpod
from container import odf_get_container, odf_new_container_from_template
from container import odf_new_container_from_class, odf_container
from xmlpart import odf_element, odf_xmlpart, LAST_CHILD
from xmlpart import odf_create_element



def odf_create_section(style):
    data = '<text:section text:style-name="%s"></text:section>' % style
    return odf_create_element(data)



def odf_create_paragraph(style, text=''):
    data = '<text:p text:style-name="%s">%s</text:p>' % (style, text)
    return odf_create_element(data)



def odf_create_heading(style, level, text=''):
    data = '<text:h text:style-name="%s" text:outline-level="%d">%s</text:h>'
    return odf_create_element(data % (style, level, text))



def odf_create_frame(name, style, width, height, page=None, x=None, y=None):
    if page is None:
        anchor = 'text:anchor-type="paragraph"'
    else:
        anchor = 'text:anchor-type="page" text:anchor-page-number="%d"' % page
        if x is not None:
            anchor += ' svg:x="%s"' % x
        if y is not None:
            anchor += ' svg:y="%s"' % y
    data = ('<draw:frame draw:name="%s" draw:style-name="%s" '
            'svg:width="%s" svg:height="%s" %s/>')

    return odf_create_element(data % (name, style, width, height, anchor))


def odf_create_image(link):
    return odf_create_element('<draw:image xlink:href="%s"/>' % link)


def odf_create_cell():
    return odf_create_element(
                    '<table:table-cell office:value-type="String"/>')


def odf_create_row(width=None):
    row = odf_create_element('<table:table-row/>')
    if width is not None:
        for i in xrange(width):
            cell = odf_create_cell()
            row.insert_element(cell, LAST_CHILD)
    return row


def odf_create_column(style):
    data = '<table:table-column table:style-name="%s"/>'
    return odf_create_element(data % style)


def odf_create_table(name, style, width=None, height=None):
    data = '<table:table table:name="%s" table:style-name="%s"/>'
    table = odf_create_element(data % (name, style))
    if width is not None or height is not None:
        width = width if width is not None else 1
        height = height if height is not None else 1
        for i in xrange(height):
            row = odf_create_row(width)
            table.insert_element(row, LAST_CHILD)
    return table


def odf_create_item():
    return odf_create_element('<text:list-item/>')


def odf_create_list(style):
    return odf_create_element('<text:list text:style-name="%s"/>' % style)



def _generate_xpath_query(element_name, attributes={}, position=None,
                          context=None):
    if context is not None:
        query = [context._get_xpath_path(), '//']
    else:
        query = ['//']
    query.append(element_name)
    # Sort attributes for reproducible test cases
    for name in sorted(attributes):
        value = attributes[name]
        if value is not None:
            query.append('[@{name}="{value}"]'.format(name=name,
                                                      value=str(value)))
        else:
            query.append('[@{name}]'.format(name=name))
    if position is not None:
        query.append('[{position}]'.format(position=str(position)))
    return ''.join(query)



def _check_arguments(context=None, position=None, style=None, level=None):
    if context is not None:
        if not isinstance(context, odf_element):
            raise TypeError, "an odf element is expected"
    if position is not None:
        if not isinstance(position, int):
            raise TypeError, "an integer position is expected"
        if position < 1:
            raise ValueError, "position count begin at 1"
    if style is not None:
        if not isinstance(style, str):
            raise TypeError, "a style name is expected"
    if level is not None:
        if not isinstance(level, int):
            raise TypeError, "an integer level is expected"
        if level < 1:
            raise ValueError, "level count begin at 1"



class odf_document(object):

    def __init__(self, container):
        if not isinstance(container, odf_container):
            raise TypeError, "container is not an ODF container"
        self.container = container

        # Cache of XML parts
        self.__xmlparts = {}


    def __get_xmlpart(self, part_name):
        parts = self.__xmlparts
        part = parts.get(part_name)
        if part is None:
            container = self.container
            part = odf_xmlpart(part_name, container)
            parts[part_name] = part
        return part


    def __get_element_list(self, name, style=None, level=None,
                           frame_style=None, context=None):
        _check_arguments(style=style, level=level, context=context)
        content = self.__get_xmlpart('content')
        attributes = {}
        if style:
            attributes['text:style-name'] = style
        if level:
            attributes['text:outline-level'] = level
        if frame_style:
            attributes['draw:style-name'] = frame_style
        query = _generate_xpath_query(name, attributes=attributes,
                                      context=context)
        return content.get_element_list(query)


    def __get_element(self, name, position, level=None, context=None):
        _check_arguments(position=position, context=context)
        content = self.__get_xmlpart('content')
        attributes = {}
        if level:
            attributes['text:outline-level'] = level
        query = _generate_xpath_query(name, attributes=attributes,
                                      position=position, context=context)
        result = content.get_element_list(query)
        if not result:
            return None
        return result[0]

<commit_msg>python: try to tell appart element names from style names<commit_after># -*- coding: UTF-8 -*-
# Copyright (C) 2009 Itaapy, ArsAperta, Pierlis, Talend

# Import from the Standard Library
from copy import deepcopy

# Import from lpod
from container import odf_get_container, odf_new_container_from_template
from container import odf_new_container_from_class, odf_container
from xmlpart import odf_element, odf_xmlpart, LAST_CHILD
from xmlpart import odf_create_element



def odf_create_section(style):
    data = '<text:section text:style-name="%s"></text:section>' % style
    return odf_create_element(data)



def odf_create_paragraph(style, text=''):
    data = '<text:p text:style-name="%s">%s</text:p>' % (style, text)
    return odf_create_element(data)



def odf_create_heading(style, level, text=''):
    data = '<text:h text:style-name="%s" text:outline-level="%d">%s</text:h>'
    return odf_create_element(data % (style, level, text))



def odf_create_frame(name, style, width, height, page=None, x=None, y=None):
    if page is None:
        anchor = 'text:anchor-type="paragraph"'
    else:
        anchor = 'text:anchor-type="page" text:anchor-page-number="%d"' % page
        if x is not None:
            anchor += ' svg:x="%s"' % x
        if y is not None:
            anchor += ' svg:y="%s"' % y
    data = ('<draw:frame draw:name="%s" draw:style-name="%s" '
            'svg:width="%s" svg:height="%s" %s/>')

    return odf_create_element(data % (name, style, width, height, anchor))


def odf_create_image(link):
    return odf_create_element('<draw:image xlink:href="%s"/>' % link)


def odf_create_cell():
    return odf_create_element(
                    '<table:table-cell office:value-type="String"/>')


def odf_create_row(width=None):
    row = odf_create_element('<table:table-row/>')
    if width is not None:
        for i in xrange(width):
            cell = odf_create_cell()
            row.insert_element(cell, LAST_CHILD)
    return row


def odf_create_column(style):
    data = '<table:table-column table:style-name="%s"/>'
    return odf_create_element(data % style)


def odf_create_table(name, style, width=None, height=None):
    data = '<table:table table:name="%s" table:style-name="%s"/>'
    table = odf_create_element(data % (name, style))
    if width is not None or height is not None:
        width = width if width is not None else 1
        height = height if height is not None else 1
        for i in xrange(height):
            row = odf_create_row(width)
            table.insert_element(row, LAST_CHILD)
    return table


def odf_create_item():
    return odf_create_element('<text:list-item/>')


def odf_create_list(style):
    return odf_create_element('<text:list text:style-name="%s"/>' % style)



def _generate_xpath_query(element_name, attributes={}, position=None,
                          context=None):
    if context is not None:
        query = [context._get_xpath_path(), '//']
    else:
        query = ['//']
    query.append(element_name)
    # Sort attributes for reproducible test cases
    for qname in sorted(attributes):
        value = attributes[qname]
        if value is not None:
            query.append('[@{qname}="{value}"]'.format(qname=qname,
                                                      value=str(value)))
        else:
            query.append('[@{qname}]'.format(qname=qname))
    if position is not None:
        query.append('[{position}]'.format(position=str(position)))
    return ''.join(query)



def _check_arguments(context=None, position=None, style=None, level=None):
    if context is not None:
        if not isinstance(context, odf_element):
            raise TypeError, "an odf element is expected"
    if position is not None:
        if not isinstance(position, int):
            raise TypeError, "an integer position is expected"
        if position < 1:
            raise ValueError, "position count begin at 1"
    if style is not None:
        if not isinstance(style, str):
            raise TypeError, "a style name is expected"
    if level is not None:
        if not isinstance(level, int):
            raise TypeError, "an integer level is expected"
        if level < 1:
            raise ValueError, "level count begin at 1"



class odf_document(object):

    def __init__(self, container):
        if not isinstance(container, odf_container):
            raise TypeError, "container is not an ODF container"
        self.container = container

        # Cache of XML parts
        self.__xmlparts = {}


    def __get_xmlpart(self, part_name):
        parts = self.__xmlparts
        part = parts.get(part_name)
        if part is None:
            container = self.container
            part = odf_xmlpart(part_name, container)
            parts[part_name] = part
        return part


    def __get_element_list(self, qname, style=None, level=None,
                           frame_style=None, context=None):
        _check_arguments(style=style, level=level, context=context)
        content = self.__get_xmlpart('content')
        attributes = {}
        if style:
            attributes['text:style-name'] = style
        if level:
            attributes['text:outline-level'] = level
        if frame_style:
            attributes['draw:style-name'] = frame_style
        query = _generate_xpath_query(qname, attributes=attributes,
                                      context=context)
        return content.get_element_list(query)


    def __get_element(self, qname, position, level=None, context=None):
        _check_arguments(position=position, context=context)
        content = self.__get_xmlpart('content')
        attributes = {}
        if level:
            attributes['text:outline-level'] = level
        query = _generate_xpath_query(qname, attributes=attributes,
                                      position=position, context=context)
        result = content.get_element_list(query)
        if not result:
            return None
        return result[0]


{'fraction_non_alphanumeric': 0.11256675567423231, 'fraction_numerical': 0.0020026702269692926, 'mean_word_length': 2.671875, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 18}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2084940', 'n_tokens_mistral': 3624, 'n_tokens_neox': 3522, 'n_words': 927}