<commit_before>import re
import json
import webapp2

from utils.decorators import as_view


class BaseResourceHandler(webapp2.RequestHandler):
    resource_class = None

    def _keys_to_span(self):
        if '_s' in self.request.arguments():
            return self.request.get('_s').split(',')

        return None

    def _res(self, method, *args, **kwargs):
        kwargs['span_keys'] = self._keys_to_span()
        return getattr(self.resource_class, method)(*args, **kwargs)

    def get(self, *args, **kwargs):
        action = 'list'

        if len(args) != 0:
            if args[0] == 'describe':
                action = 'describe'
            elif args[0] != '':
                action = 'get'

        if action == 'describe':
            ret = self._res('describe')
        elif action == 'get':
            ret = self._res('get', args[0])
            if ret is None:
                self.response.set_status(404)
                return
        else:
            if self.request.arguments():
                filter = dict((a, self.request.get(a)) for a in self.request.arguments() if a not in ('_o', '_s'))
                ordering = None

                if '_o' in self.request.arguments():
                    ordering = self.request.get('_o').split(',')

                try:
                    ret = self._res('query', ordering=ordering, **filter)
                except (self.resource_class.InvalidFilter, self.resource_class.InvalidOrderingProperty), e:
                    self.response.set_status(400)
                    self.response.write(unicode(e))
                    return
            else:
                ret = self._res('list')

        self.response.write(json.dumps(ret))

    def post(self, *args, **kwargs):
        if len(args) == 0 or args[0] == '':
            self.response.set_status(400)
            return

        id = args[0]

        try:
            data = json.loads(self.request.body)
        except ValueError, e:
            self.response.set_status(400)
            self.response.write(unicode(e))
            return

        try:
            if id is not None:
                ret = self._res('update', id, data)
            else:
                ret = self._res('create', data)
        except self.resource_class.InvalidValue, e:
            self.response.set_status(400)
            self.response.write(unicode(e))
            return

        self.response.write(json.dumps(ret))

    def delete(self, *args, **kwargs):
        if not len(args) or args[0] == '':
            self.response.set_status(400)
            return

        if not self._res('delete', args[0]):
            self.response.set_status(404)

        return

    @classmethod
    def slugify(cls):
        '''converts CamelCase to camel-case'''
        name = cls.resource_class.model.__name__
        s1 = re.sub('(.)([A-Z][a-z]+)', r'\1-\2', name)
        return re.sub('([a-z0-9])([A-Z])', r'\1-\2', s1).lower()

    @classmethod
    def routes(cls):
        return (
            ('/%s' % cls.slugify(), cls),
            ('/%s/([^\/]*)\/?' % cls.slugify(), cls),
        )

@as_view
def model_list(request, response):
    from resources import register
    response.write(json.dumps([
        {'model': m.__name__, 'resource': m.__name__.lower()} for m in register.models()
    ]))<commit_msg>Slugify me<commit_after>import re
import json
import webapp2

from utils.decorators import as_view


def slugify(name):
    s1 = re.sub('(.)([A-Z][a-z]+)', r'\1-\2', name)
    return re.sub('([a-z0-9])([A-Z])', r'\1-\2', s1).lower()


class BaseResourceHandler(webapp2.RequestHandler):
    resource_class = None

    def _keys_to_span(self):
        if '_s' in self.request.arguments():
            return self.request.get('_s').split(',')

        return None

    def _res(self, method, *args, **kwargs):
        kwargs['span_keys'] = self._keys_to_span()
        return getattr(self.resource_class, method)(*args, **kwargs)

    def get(self, *args, **kwargs):
        action = 'list'

        if len(args) != 0:
            if args[0] == 'describe':
                action = 'describe'
            elif args[0] != '':
                action = 'get'

        if action == 'describe':
            ret = self._res('describe')
        elif action == 'get':
            ret = self._res('get', args[0])
            if ret is None:
                self.response.set_status(404)
                return
        else:
            if self.request.arguments():
                filter = dict((a, self.request.get(a)) for a in self.request.arguments() if a not in ('_o', '_s'))
                ordering = None

                if '_o' in self.request.arguments():
                    ordering = self.request.get('_o').split(',')

                try:
                    ret = self._res('query', ordering=ordering, **filter)
                except (self.resource_class.InvalidFilter, self.resource_class.InvalidOrderingProperty), e:
                    self.response.set_status(400)
                    self.response.write(unicode(e))
                    return
            else:
                ret = self._res('list')

        self.response.write(json.dumps(ret))

    def post(self, *args, **kwargs):
        if len(args) == 0 or args[0] == '':
            self.response.set_status(400)
            return

        id = args[0]

        try:
            data = json.loads(self.request.body)
        except ValueError, e:
            self.response.set_status(400)
            self.response.write(unicode(e))
            return

        try:
            if id is not None:
                ret = self._res('update', id, data)
            else:
                ret = self._res('create', data)
        except self.resource_class.InvalidValue, e:
            self.response.set_status(400)
            self.response.write(unicode(e))
            return

        self.response.write(json.dumps(ret))

    def delete(self, *args, **kwargs):
        if not len(args) or args[0] == '':
            self.response.set_status(400)
            return

        if not self._res('delete', args[0]):
            self.response.set_status(404)

        return

    @classmethod
    def slugify(cls):
        '''converts CamelCase to camel-case'''
        return slugify(cls.resource_class.model.__name__)

    @classmethod
    def routes(cls):
        return (
            ('/%s' % cls.slugify(), cls),
            ('/%s/([^\/]*)\/?' % cls.slugify(), cls),
        )

@as_view
def model_list(request, response):
    from resources import register
    response.write(json.dumps([
        {'model': m.__name__, 'resource': slugify(m.__name__)} for m in register.models()
    ]))
{'fraction_non_alphanumeric': 0.14210841569332136, 'fraction_numerical': 0.011979634621144056, 'mean_word_length': 1.7553630363036303, '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': '7442742', 'n_tokens_mistral': 2058, 'n_tokens_neox': 1947, 'n_words': 431}