<commit_before>#
# Copyright (C) 2012  Red Hat, 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; either version 2.1 of the License, or
# (at your option) any later version.
#
# 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.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Author: <NAME> <<EMAIL>>
#
# -*- coding: utf-8 -*-

import os
import simplejson as json
from lxml import etree

import utils


BASE_PATH = os.path.dirname(os.path.abspath(__file__))
TRANSFORM_MAP = {
    "job-rst": os.path.join(BASE_PATH, "data", "job-report.rst.xsl"),
    "testplan-rst": os.path.join(BASE_PATH, "data", "testplan-report.rst.xsl")
}


def job_status_to_report_json(txt):
    """Apply the plaintext report transformation to a json obj (str)
    """
    d = json.loads(txt)
    return status_to_report(d)


def job_status_to_report(d):
    """Transform a job status dict to a report
    """
    t="job-rst"
    assert t in TRANSFORM_MAP, "Unknown transformation: %s" % t
    return transform_dict(TRANSFORM_MAP[t], d, "status")


def testplan_status_to_report(d):
    """Transform a testplan status dict to a report
    """
    t="testplan-rst"
    assert t in TRANSFORM_MAP, "Unknown transformation: %s" % t
    return transform_dict(TRANSFORM_MAP[t], d, "status")


def transform_dict(stylefile, d, rootname):
    """Apply a transformation to a dict
    """
    xml = utils.obj2xml(rootname, d)
    transform = etree.XSLT(etree.parse(stylefile))
    report = transform(xml)
    return report<commit_msg>Add initial junit report<commit_after>#
# Copyright (C) 2012  Red Hat, 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; either version 2.1 of the License, or
# (at your option) any later version.
#
# 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.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Author: <NAME> <<EMAIL>>
#
# -*- coding: utf-8 -*-

import os
import simplejson as json
from lxml import etree

import utils


BASE_PATH = os.path.dirname(os.path.abspath(__file__))
TRANSFORM_MAP = {
    "job-rst": os.path.join(BASE_PATH, "data", "job-report.rst.xsl"),
    "testplan-rst": os.path.join(BASE_PATH, "data", "testplan-report.rst.xsl"),
    "testplan-junit-xml": os.path.join(BASE_PATH, "data", "testplan-report.junit.xsl"),
}


def job_status_to_report_json(txt):
    """Apply the plaintext report transformation to a json obj (str)
    """
    d = json.loads(txt)
    return status_to_report(d)

def job_status_to_report(d):
    """Transform a job status dict to a report
    """
    return _map_transform(d, "job-rst")

def testplan_status_to_report(d):
    """Transform a testplan status dict to a report
    """
    return _map_transform(d, "testplan-rst")

def testplan_status_to_junit_report(d):
    """Transform a testplan status dict to a junit report
    """
    return _map_transform(d, "testplan-junit-xml")



def _map_transform(d, t, rootname="status"):
    assert t in TRANSFORM_MAP, "Unknown transformation: %s" % t
    return transform_dict(TRANSFORM_MAP[t], d, rootname)

def transform_dict(stylefile, d, rootname):
    """Apply a transformation to a dict
    """
    xml = utils.obj2xml(rootname, d)
    transform = etree.XSLT(etree.parse(stylefile))
    report = transform(xml)
    return report
{'fraction_non_alphanumeric': 0.10964912280701754, 'fraction_numerical': 0.003898635477582846, 'mean_word_length': 3.969733656174334, 'pattern_counts': {'":': 5, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 10}, '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': '6387742', 'n_tokens_mistral': 1310, 'n_tokens_neox': 1280, 'n_words': 452}