<commit_before>import logging
import time
import warnings

import requests

import pycounter.exceptions


logger = logging.getLogger(__name__)


def _raw_to_full(raw_report):
    """Convert a raw report to CounterReport.

    :param raw_report: raw report as dict decoded from JSON
    :return: a :class:`pycounter.report.CounterReport`
    """
    #  FIXME


def get_sushi_stats_raw(
    wsdl_url=None,
    start_date=None,
    end_date=None,
    requestor_id=None,
    requestor_email=None,
    requestor_name=None,
    customer_reference=None,
    customer_name=None,
    report="TR_J1",
    release=5,
    sushi_dump=False,
    verify=True,
    url=None,
):
    """Get SUSHI stats for a given site in dict (decoded from JSON) format.

    :param wsdl_url: (Deprecated; for backward compatibility with COUNTER 4 SUSHI
    code. Use `url` instead.) URL to API endpoint for this provider

    :param start_date: start date for report (must be first day of a month)

    :param end_date: end date for report (must be last day of a month)

    :param requestor_id: requestor ID as defined by SUSHI protocol<commit_msg>create CounterReport from SUSHI JSON (headers only; data is TODO)<commit_after>import datetime
import logging
import time
import warnings

import pendulum
import requests

import pycounter.exceptions


logger = logging.getLogger(__name__)


def _dates_from_filters(filters):
    """Convert report filters to start and end date

    Args:
        filters: a list of dicts containing SUSHI report filters

    Returns: tuple of start, end dates as datetime.date

    """
    converted_filters = {
        k: datetime.datetime.strptime(v, "%Y-%m-%d")
        for k, v in filters.items()
        if k in ("Begin_Date", "End_Date")
    }
    try:
        return converted_filters["Begin_Date"], converted_filters["End_Date"]
    except KeyError:
        raise ValueError("filters must include a Begin_Date and End_Date")


def _raw_to_full(raw_report):
    """Convert a raw report to CounterReport.

    :param raw_report: raw report as dict decoded from JSON
    :return: a :class:`pycounter.report.CounterReport`
    """
    header = raw_report["Report_Header"]
    start_date, end_date = _dates_from_filters(header["Report_Filters"])
    date_run = header.get("Created")
    report_data = {
        "period": (start_date, end_date),
        "report_version": int(header["Release"]),
        "report_type": header["Report_ID"],
        "customer": header.get("Institution_Name", u""),
        "institutional_identifier": header.get("Customer_ID", u""),
        "date_run": pendulum.parse(date_run) if date_run else datetime.datetime.now(),
    }

    report = pycounter.report.CounterReport(**report_data)
    #  FIXME - add actual item data to report

    return report


def get_sushi_stats_raw(
    wsdl_url=None,
    start_date=None,
    end_date=None,
    requestor_id=None,
    requestor_email=None,
    requestor_name=None,
    customer_reference=None,
    customer_name=None,
    report="TR_J1",
    release=5,
    sushi_dump=False,
    verify=True,
    url=None,
):
    """Get SUSHI stats for a given site in dict (decoded from JSON) format.

    :param wsdl_url: (Deprecated; for backward compatibility with COUNTER 4 SUSHI
    code. Use `url` instead.) URL to API endpoint for this provider

    :param start_date: start date for report (must be first day of a month)

    :param end_date: end date for report (must be last day of a month)

    :param requestor_id: requestor ID as defined by SUSHI protocol
{'fraction_non_alphanumeric': 0.09031710079275199, 'fraction_numerical': 0.0016987542468856172, 'mean_word_length': 3.570504527813713, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '1635782', 'n_tokens_mistral': 1105, 'n_tokens_neox': 1064, 'n_words': 339}