<commit_before>'''
This file contains the functionality for the using of webpipes.
'''
from __future__ import print_function
import json
try:
    from urllib.request import urlopen, Request
except ImportError:
    from urllib2 import urlopen, Request

from exceptions import TypeError

class WebPypeBaseClient(object):
    '''
    Base class for implementing the client aspects of the spec. Inherit from
    or use this class for a more fundamental 'WebPipe' client.

    '''

    def _request(self, url, method=None):
        request = Request(url)
        if method:
            if isinstance(method, str):
                request.get_method = lambda: method
        resp = urlopen(request)
        return resp.read()

    def options(self, url):
        return self._request(url, method='OPTIONS')

    def execute(self, url, inputs):
        if not isinstance(inputs, dict) and not isinstance(inputs, str):
            raise TypeError('Your input value must be a dictionary or string. Got: %s' % inputs)
        if isinstance(inputs, dict):
            data = json.dumps({'inputs': [inputs]})
        if isinstance(inputs, str):
            data = "{'inputs' : [%s]}" % inputs

        request = Request(url, data, headers={'Content-Type': 'application/json'})
        resp = urlopen(request)
        return resp.read()


class WebPypeClient(WebPypeBaseClient):
    '''
    A class for interacting with webpipes that is a bit more full featured.

    '''

    def print_options(self, url):
        resp = self.options(url)
        return print(resp)

    def execute_from_file(self, url, file_var):
        '''
        Identical to WebPypeClient.execute(), except this function accepts a
        file path or file type instead of a dictionary.

        '''
        if isinstance(file_var, file):
            f = file_var
        elif isinstance(file_var, str):
            try:
                f = open(file_var)
            except IOError, e:
                raise e
        else:
            raise TypeError("This function only accepts a 'file' type  or file path")

        inputs = f.read()
        resp = self.execute(url, inputs)
        return resp<commit_msg>Added WebPypeBaseClient._wrapinput() for better handling of wrapping the 'input'. Added a fallback for trying both ways of wrapping the input within execute(). Added json.loads() to execute_from_file()<commit_after>'''
This file contains the functionality for the using of webpipes.
'''
from __future__ import print_function
import json
try:
    from urllib.request import urlopen, Request
    from urllib.error import HTTPError
except ImportError:
    from urllib2 import urlopen, Request, HTTPError

from exceptions import TypeError

class WebPypeBaseClient(object):
    '''
    Base class for implementing the client aspects of the spec. Inherit from
    or use this class for a more fundamental 'WebPipe' client.

    '''

    def _request(self, url, method=None):
        request = Request(url)
        if method:
            if isinstance(method, str):
                request.get_method = lambda: method
        resp = urlopen(request)
        return resp.read()

    def _wrapinput(self, inputs, array_wrap=False):
        if not isinstance(inputs, dict) and not isinstance(inputs, str):
            raise TypeError('''Your input value must be a dictionary or
                               string. Got: %s''' % inputs)
        if array_wrap:
            if isinstance(inputs, dict):
                data = json.dumps({'inputs': inputs})
            if isinstance(inputs, str):
                data = "{'inputs' : %s}" % inputs
        else:
            if isinstance(inputs, dict):
                data = json.dumps({'inputs': [inputs]})
            if isinstance(inputs, str):
                data = "{'inputs' : [%s]}" % inputs

        return data

    def options(self, url):
        return self._request(url, method='OPTIONS')

    def execute(self, url, inputs):
        data = self._wrapinput(inputs)
        request = Request(url, data, headers={'Content-Type': 'application/json'})
        try:
            resp = urlopen(request)
        except HTTPError:
            try:
                data = self._wrapinput(inputs, array_wrap=True)
                request = Request(url, data, headers={'Content-Type': 'application/json'})
                resp = urlopen(request)
            except HTTPError:
                raise HTTPError("Incorrect URL or structuring of data.")

        return resp.read()


class WebPypeClient(WebPypeBaseClient):
    '''
    A class for interacting with webpipes that is a bit more full featured.

    '''

    def print_options(self, url):
        resp = self.options(url)
        return print(resp)

    def execute_from_file(self, url, file_var):
        '''
        Identical to WebPypeClient.execute(), except this function accepts a
        file path or file type instead of a dictionary.

        '''
        if isinstance(file_var, file):
            f = file_var
        elif isinstance(file_var, str):
            try:
                f = open(file_var)
            except IOError, e:
                raise e
        else:
            raise TypeError("This function only accepts a 'file' type  or file path")

        inputs = json.loads(f.read())
        resp = self.execute(url, inputs)
        return resp
{'fraction_non_alphanumeric': 0.09314092953523238, 'fraction_numerical': 0.0003748125937031484, 'mean_word_length': 2.2522851919561244, 'pattern_counts': {'":': 0, '<': 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': '2711217', 'n_tokens_mistral': 1436, 'n_tokens_neox': 1406, 'n_words': 490}