<commit_before># vim: tabstop=4 shiftwidth=4 softtabstop=4
# tab stops are 8.  ^^ this is wrong

import logging
from functools import partial

import abstraction


_cached_apis = {}


class RoushApi(object):
    def __init__(self):
        self.model_list = {}
        classname = self.__class__.__name__.lower()
        self.logger = logging.getLogger('%s.%s' % (__name__, classname))

    def transactions(self):
        result = {}
        for model, backend in self.model_list.items():
            if getattr(backend, 'transactions', None) is not None:
                trans = backend.transactions()
                if trans is not None:
                    result[model] = trans

        return result

    def _get_models(self):
        return self.model_list.keys()

    def _call_model(self, function, model, *args, **kwargs):
        model = model.lower()

        if not model in self.model_list:
            raise KeyError('unknown model %s' % model)

        if not hasattr(self.model_list[model], function):
            raise ValueError('unknown model function %s' % function)

        return getattr(self.model_list[model], function)(*args, **kwargs)

    def _model_get_all(self, model):
        return self._call_model('get_all', model)

    def _model_get_by_id(self, model, id):
        return self._call_model('get', model, id)

    def _model_get_columns(self, model):
        return self._call_model('get_columns', model)

    def _model_get_schema(self, model):
        return self._call_model('get_schema', model)

    def _model_create(self, model, data):
        return self._call_model('create', model, data)

    def _model_delete_by_id(self, model, id):
        return self._call_model('delete', model, id)

    def _model_get_by_filter(self, model, filters):
        return self._call_model('filter', model, filters)

    def _model_get_first_by_filter(self, model, filters):
        return self._call_model('first_by_filter', model, filters)

    def _model_query(self, model, query):
        return self._call_model('query', model, query)

    def _model_update_by_id(self, model, id, data):
        return self._call_model('update', model, id, data)

    def add_model(self, name, abstracted_backend):
        model = name.lower()
        sing = model[:-1]

        self.model_list[model] = abstracted_backend

        setattr(self, '%s_get_all' % model,
                partial(self._model_get_all, model))
        setattr(self, '%s_delete_by_id' % sing,
                partial(self._model_delete_by_id, model))
        setattr(self, '%s_get_columns' % sing,
                partial(self._model_get_columns, model))
        setattr(self, '%s_get_first_by_filter' % sing,
                partial(self._model_get_first_by_filter, model))
        setattr(self, '%s_get_by_id' % sing,
                partial(self._model_get_by_id, model))
        setattr(self, '%s_create' % sing,
                partial(self._model_create, model))
        setattr(self, '%s_update_by_id' % sing,
                partial(self._model_update_by_id, model))
        setattr(self, '%s_query' % model,
                partial(self._model_query, model))


def api_from_endpoint(endpoint):
    if 'endpoint-based-%s' % endpoint in _cached_apis:
        return _cached_apis['endpoint-based-%s' % endpoint]

    from roushclient import client
    from roush.db import models

    new_api = RoushApi()
    ep = client.RoushEndpoint(endpoint)

    # the index page should be wired up right, and we should
    # have an api call to get all the object types
    for model_name in ['nodes', 'filters',
                       'adventures', 'facts']:<commit_msg>add tasks to endpoint abstraction<commit_after># vim: tabstop=4 shiftwidth=4 softtabstop=4
# tab stops are 8.  ^^ this is wrong

import logging
from functools import partial

import abstraction


_cached_apis = {}


class RoushApi(object):
    def __init__(self):
        self.model_list = {}
        classname = self.__class__.__name__.lower()
        self.logger = logging.getLogger('%s.%s' % (__name__, classname))

    def transactions(self):
        result = {}
        for model, backend in self.model_list.items():
            if getattr(backend, 'transactions', None) is not None:
                trans = backend.transactions()
                if trans is not None:
                    result[model] = trans

        return result

    def _get_models(self):
        return self.model_list.keys()

    def _call_model(self, function, model, *args, **kwargs):
        model = model.lower()

        if not model in self.model_list:
            raise KeyError('unknown model %s' % model)

        if not hasattr(self.model_list[model], function):
            raise ValueError('unknown model function %s' % function)

        return getattr(self.model_list[model], function)(*args, **kwargs)

    def _model_get_all(self, model):
        return self._call_model('get_all', model)

    def _model_get_by_id(self, model, id):
        return self._call_model('get', model, id)

    def _model_get_columns(self, model):
        return self._call_model('get_columns', model)

    def _model_get_schema(self, model):
        return self._call_model('get_schema', model)

    def _model_create(self, model, data):
        return self._call_model('create', model, data)

    def _model_delete_by_id(self, model, id):
        return self._call_model('delete', model, id)

    def _model_get_by_filter(self, model, filters):
        return self._call_model('filter', model, filters)

    def _model_get_first_by_filter(self, model, filters):
        return self._call_model('first_by_filter', model, filters)

    def _model_query(self, model, query):
        return self._call_model('query', model, query)

    def _model_update_by_id(self, model, id, data):
        return self._call_model('update', model, id, data)

    def add_model(self, name, abstracted_backend):
        model = name.lower()
        sing = model[:-1]

        self.model_list[model] = abstracted_backend

        setattr(self, '%s_get_all' % model,
                partial(self._model_get_all, model))
        setattr(self, '%s_delete_by_id' % sing,
                partial(self._model_delete_by_id, model))
        setattr(self, '%s_get_columns' % sing,
                partial(self._model_get_columns, model))
        setattr(self, '%s_get_first_by_filter' % sing,
                partial(self._model_get_first_by_filter, model))
        setattr(self, '%s_get_by_id' % sing,
                partial(self._model_get_by_id, model))
        setattr(self, '%s_create' % sing,
                partial(self._model_create, model))
        setattr(self, '%s_update_by_id' % sing,
                partial(self._model_update_by_id, model))
        setattr(self, '%s_query' % model,
                partial(self._model_query, model))


def api_from_endpoint(endpoint):
    if 'endpoint-based-%s' % endpoint in _cached_apis:
        return _cached_apis['endpoint-based-%s' % endpoint]

    from roushclient import client
    from roush.db import models

    new_api = RoushApi()
    ep = client.RoushEndpoint(endpoint)

    # the index page should be wired up right, and we should
    # have an api call to get all the object types
    for model_name in ['nodes', 'filters', 'tasks',
                       'adventures', 'facts']:
{'fraction_non_alphanumeric': 0.10896476669840838, 'fraction_numerical': 0.0013603591348115903, 'mean_word_length': 2.7663934426229506, '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': '3047764', 'n_tokens_mistral': 2290, 'n_tokens_neox': 2199, 'n_words': 544}