<commit_before>#!/usr/bin/env python
# Copyright (c) 2011 Citrix Systems, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation; version 2.1 only. with the special
# exception on linking described in file LICENSE.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.

"""xmlunwrap - general methods to unwrap XML elements & attributes"""

import xml.dom.minidom


class XmlUnwrapError(Exception):
    pass

def getText(nodelist):
    rc = ""

    for node in nodelist.childNodes:
        if node.nodeType == node.TEXT_NODE:
            rc = rc + node.data
    return rc.encode().strip()

def getElementsByTagName(el, tags, mandatory = False):
    matching = []
    for tag in tags:
        matching.extend(el.getElementsByTagName(tag))
    if mandatory and len(matching) == 0:
        raise XmlUnwrapError, "Missing mandatory element %s" % tags[0]
    return matching

def getStrAttribute(el, attrs, default = '', mandatory = False):
    matching = []
    for attr in attrs:
        val = el.getAttribute(attr)
        if val != '':
            matching.append(val)
    if len(matching) == 0:
        if mandatory:
            raise XmlUnwrapError, "Missing mandatory attribute %s" % attrs[0]
        return default
    return matching[0]

def getBoolAttribute(el, attrs, default = None):
    mandatory = (default == None)<commit_msg>Encode attribute names<commit_after>#!/usr/bin/env python
# Copyright (c) 2011 Citrix Systems, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation; version 2.1 only. with the special
# exception on linking described in file LICENSE.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.

"""xmlunwrap - general methods to unwrap XML elements & attributes"""

import xml.dom.minidom


class XmlUnwrapError(Exception):
    pass

def getText(nodelist):
    rc = ""

    for node in nodelist.childNodes:
        if node.nodeType == node.TEXT_NODE:
            rc = rc + node.data
    return rc.encode().strip()

def getElementsByTagName(el, tags, mandatory = False):
    matching = []
    for tag in tags:
        matching.extend(el.getElementsByTagName(tag))
    if mandatory and len(matching) == 0:
        raise XmlUnwrapError, "Missing mandatory element %s" % tags[0]
    return matching

def getStrAttribute(el, attrs, default = '', mandatory = False):
    matching = []
    for attr in attrs:
        val = el.getAttribute(attr).encode()
        if val != '':
            matching.append(val)
    if len(matching) == 0:
        if mandatory:
            raise XmlUnwrapError, "Missing mandatory attribute %s" % attrs[0]
        return default
    return matching[0]

def getBoolAttribute(el, attrs, default = None):
    mandatory = (default == None)
{'fraction_non_alphanumeric': 0.08930704898446834, 'fraction_numerical': 0.006571087216248507, 'mean_word_length': 3.2285353535353534, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6761429', 'n_tokens_mistral': 931, 'n_tokens_neox': 907, 'n_words': 365}