<commit_before># coding: utf-8

from __future__ import unicode_literals

import copy

import requests
import webbrowser

from .exceptions import ResponseProcessException


class TapiocaInstantiator(object):

    def __init__(self, adapter_class):
        self.adapter_class = adapter_class

    def __call__(self, *args, **kwargs):
        return TapiocaClient(self.adapter_class(), api_params=kwargs)


class TapiocaClient(object):

    def __init__(self, api, data=None, response=None, request_kwargs=None,
                 api_params={}, resource=None, *args, **kwargs):
        self._api = api
        self._data = data
        self._response = response
        self._api_params = api_params
        self._request_kwargs = request_kwargs
        self._resource = resource

    def _wrap_in_tapioca(self, data, *args, **kwargs):
        return TapiocaClient(self._api.__class__(),
            data=data, api_params=self._api_params, *args, **kwargs)

    def _wrap_in_tapioca_executor(self, data, *args, **kwargs):
        return TapiocaClientExecutor(self._api.__class__(),
            data=data, api_params=self._api_params, *args, **kwargs)

    def _get_doc(self):
        resources = copy.copy(self._resource)
        docs = ("Automatic generated __doc__ from resource_mapping.\n"
                "Resource: %s\n"
                "Docs: %s\n" % (resources.pop('resource', ''),
                                resources.pop('docs', '')))
        for key, value in sorted(resources.items()):
            docs += "%s: %s\n" % (key.title(), value)
        docs = docs.strip()
        return docs

    __doc__ = property(_get_doc)

    def __call__(self, *args, **kwargs):
        data = self._data
        if kwargs:
            data = self._api.fill_resource_template_url(self._data, kwargs)

        return TapiocaClientExecutor(self._api.__class__(), data=data, api_params=self._api_params,
            resource=self._resource, response=self._response)

    def _get_client_from_name(self, name):
        if self._data and \
            ((isinstance(self._data, list) and isinstance(name, int)) or \
                (hasattr(self._data, '__iter__') and name in self._data)):
            return TapiocaClient(self._api.__class__(), data=self._data[name], api_params=self._api_params)

        resource_mapping = self._api.resource_mapping
        if name in resource_mapping:
            resource = resource_mapping[name]
            api_root = self._api.get_api_root(self._api_params)

            url = api_root.rstrip('/') + '/' + resource['resource'].lstrip('/')
            return TapiocaClient(self._api.__class__(), data=url, api_params=self._api_params,
                                 resource=resource)

    def __getattr__(self, name):
        ret = self._get_client_from_name(name)
        if ret is None:
            raise AttributeError(name)
        return ret

    def __getitem__(self, key):<commit_msg>using wrap_in_tapioca helpers in TapiocaClient<commit_after># coding: utf-8

from __future__ import unicode_literals

import copy

import requests
import webbrowser

from .exceptions import ResponseProcessException


class TapiocaInstantiator(object):

    def __init__(self, adapter_class):
        self.adapter_class = adapter_class

    def __call__(self, *args, **kwargs):
        return TapiocaClient(self.adapter_class(), api_params=kwargs)


class TapiocaClient(object):

    def __init__(self, api, data=None, response=None, request_kwargs=None,
                 api_params={}, resource=None, *args, **kwargs):
        self._api = api
        self._data = data
        self._response = response
        self._api_params = api_params
        self._request_kwargs = request_kwargs
        self._resource = resource

    def _wrap_in_tapioca(self, data, *args, **kwargs):
        return TapiocaClient(self._api.__class__(),
            data=data, api_params=self._api_params, *args, **kwargs)

    def _wrap_in_tapioca_executor(self, data, *args, **kwargs):
        return TapiocaClientExecutor(self._api.__class__(),
            data=data, api_params=self._api_params, *args, **kwargs)

    def _get_doc(self):
        resources = copy.copy(self._resource)
        docs = ("Automatic generated __doc__ from resource_mapping.\n"
                "Resource: %s\n"
                "Docs: %s\n" % (resources.pop('resource', ''),
                                resources.pop('docs', '')))
        for key, value in sorted(resources.items()):
            docs += "%s: %s\n" % (key.title(), value)
        docs = docs.strip()
        return docs

    __doc__ = property(_get_doc)

    def __call__(self, *args, **kwargs):
        data = self._data
        if kwargs:
            data = self._api.fill_resource_template_url(self._data, kwargs)

        return self._wrap_in_tapioca_executor(data,
            resource=self._resource, response=self._response)

    def _get_client_from_name(self, name):
        if self._data and \
            ((isinstance(self._data, list) and isinstance(name, int)) or \
                (hasattr(self._data, '__iter__') and name in self._data)):
            return self._wrap_in_tapioca(data=self._data[name])

        resource_mapping = self._api.resource_mapping
        if name in resource_mapping:
            resource = resource_mapping[name]
            api_root = self._api.get_api_root(self._api_params)

            url = api_root.rstrip('/') + '/' + resource['resource'].lstrip('/')
            return self._wrap_in_tapioca(url, resource=resource)

    def __getattr__(self, name):
        ret = self._get_client_from_name(name)
        if ret is None:
            raise AttributeError(name)
        return ret

    def __getitem__(self, key):
{'fraction_non_alphanumeric': 0.11185016628741466, 'fraction_numerical': 0.00035007876772273763, 'mean_word_length': 2.653452685421995, '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': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6916002', 'n_tokens_mistral': 1714, 'n_tokens_neox': 1661, 'n_words': 361}