<commit_before>"""
Client to ensure accuracy
~~~~~~~~~~~~~~~~~~

This module contains a client side to call for partials the funsize server

"""

import argparse
import requests
import time
try:
    import simplejson as json
except ImportError:
    import json


FUNSIZE_SERVER_SIDE = 'http://127.0.0.1:5000'


class FunsizeClient(object):
    """ Class to hold funsize client methods """

    lifecycles = 20 # number of lifecycles to keep polling for results
    cycle_sleep = 30 # number of seconds to sleep in a cycle

    def __init__(self):
        pass

    def _write_to_file(self, ret, output_file):
        """ Private method used to write content from response to file """
        with open(output_file, 'a') as fobj:
            fobj.write(ret.content)

    def demand_partial(self, args):
        """ Method to contain th general logic of the module """

        trigger_resource_uri = FUNSIZE_SERVER_SIDE + '/partial'
        ret = self.trigger_partial_request(trigger_resource_uri, args)

        if ret.status_code == 500:
            raise SystemError('Encoutered error on funsize server side - 500')

        _uri = json.loads(ret.content).get('result', None)
        getter_resource_uri = FUNSIZE_SERVER_SIDE + _uri
        self.poll_for_partial(getter_resource_uri, args.output)

    def trigger_partial_request(self, resource_uri, iargs):
        """ Method used to request partial from funsize server side """

        payload = {
            'mar_from': iargs.from_url,
            'mar_to': iargs.to_url,
            'mar_from_hash': iargs.from_hash,
            'mar_to_hash': iargs.to_hash,
            'channel_id': iargs.channel,
            'product_version': iargs.version,
        }

        return requests.post(resource_uri, data=payload)

    def poll_for_partial(self, resource_uri, output_file):
        """ Method to retrieve partial in max  lifecycles * cycle_sleep time """

        ret = requests.get(resource_uri)
        if ret.status_code == 400:
            raise ValueError("Bad identifier request!")

        counter = self.lifecycles
        while counter:
            ret = requests.get(resource_uri)
            if ret.status_code == 200:
                self._write_to_file(ret, output_file)
                break
            counter -= 1
            time.sleep(self.cycle_sleep)

        if not counter:<commit_msg>Logging done right in funsize_client.py<commit_after>"""
Client to ensure accuracy
~~~~~~~~~~~~~~~~~~

This module contains a client side to call for partials the funsize server

"""

import argparse
import requests
import time
import logging
import sys

try:
    import simplejson as json
except ImportError:
    import json

logger = logging.getLogger()
logger.setLevel(logging.DEBUG)

handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.DEBUG)
logger.addHandler(handler)

FUNSIZE_SERVER_SIDE = 'http://funsize-env.elasticbeanstalk.com'


class FunsizeClient(object):
    """ Class to hold funsize client methods """
    lifecycles = 20  # number of lifecycles to keep polling for results
    cycle_sleep = 30  # number of seconds to sleep in a cycle

    def __init__(self):
        logger.info("Funsize client successfully initiated.")
        pass

    def _write_to_file(self, ret, output_file):
        """ Private method used to write content from response to file """
        with open(output_file, 'a') as fobj:
            fobj.write(ret.content)

    def demand_partial(self, args):
        """ Method to contain th general logic of the module """
        trigger_resource_uri = FUNSIZE_SERVER_SIDE + '/partial'
        logger.info("Triggering job at %s", trigger_resource_uri)
        ret = self.trigger_partial_request(trigger_resource_uri, args)

        if ret.status_code == 500:
            raise SystemError('Encoutered error on funsize server side - 500')

        logger.info("Server returned. Investigating response ...")
        _uri = json.loads(ret.content).get('result', None)
        getter_resource_uri = FUNSIZE_SERVER_SIDE + _uri
        self.poll_for_partial(getter_resource_uri, args.output)

    def trigger_partial_request(self, resource_uri, iargs):
        """ Method used to request partial from funsize server side """
        payload = {
            'mar_from': iargs.from_url,
            'mar_to': iargs.to_url,
            'mar_from_hash': iargs.from_hash,
            'mar_to_hash': iargs.to_hash,
            'channel_id': iargs.channel,
            'product_version': iargs.version,
        }

        return requests.post(resource_uri, data=payload)

    def poll_for_partial(self, resource_uri, output_file):
        """ Method to retrieve partial in max  lifecycles * cycle_sleep time """
        logger.info("Polling for the partial at %s", resource_uri)
        ret = requests.get(resource_uri)
        if ret.status_code == 400:
            raise ValueError("Bad identifier request!")

        logger.info("Start querying for the partial resource ...")
        counter = self.lifecycles
        while counter:
            ret = requests.get(resource_uri)
            if ret.status_code == 200:
                logger.info("Partial resource retrieved, writing to file ...")
                self._write_to_file(ret, output_file)
                break
            counter -= 1
            time.sleep(self.cycle_sleep)

        if not counter:
{'fraction_non_alphanumeric': 0.09380234505862646, 'fraction_numerical': 0.008189093616229295, 'mean_word_length': 2.653297076818491, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, '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': '7186524', 'n_tokens_mistral': 1564, 'n_tokens_neox': 1484, 'n_words': 430}