<commit_before>#!/usr/bin/python
# -*- coding: utf-8 -*-


import string
import logging
import torext
from torext.handlers import _BaseHandler
from torext.app import BaseApplication
from torext.server import run_api_server


class FlaskStyleApp(object):
    def __init__(self, brand):
        self.brand = brand
        self.handlers = {}
        torext.initialize()

    def _hdr_name(self, url):
        digit = 3
        prefix = self.brand
        suffix = 'Hdr'
        body = ''.join([i for i in url if i in string.letters])
        num = len(self.handlers)
        if len(str(num)) < digit:
            body += (digit - len(str(num))) * '0' + str(num)
        else:
            body += str(num)
        return prefix + body + suffix

    def route(self, method, url):
        hdr_name = self._hdr_name(url)
        logging.debug(hdr_name)
        if url in self.handlers:
            hdr = self.handlers[url]
        else:
            exec 'class %s(_BaseHandler): pass' % hdr_name in globals(), locals()
            hdr = locals()[hdr_name]
            self.handlers[url] = hdr

        def route_adaptor(fn):
            setattr(hdr, method, fn)
        return route_adaptor

    def run(self):
        run_api_server(
            BaseApplication(
                handlers=[i for i in self.handlers.iteritems()],
            )
        )


if __name__ == '__main__':
    app = FlaskStyleApp('justTest')

    @app.route('get', '/')
    def hello(hdr):
        hdr.write('ok')

    @app.route('post', '/user/profile')
    def hello_post(hdr):
        hdr.write('ok post')
<commit_msg>use type() instead of exec to define handler class in flask_style<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-


import string
import logging
import torext
from torext.handlers import _BaseHandler
from torext.app import BaseApplication
from torext.server import run_api_server
from torext import settings


class FlaskStyleApp(object):
    def __init__(self, brand):
        self.brand = brand
        self.handlers = {}
        self.settings = settings
        torext.initialize()

    def _hdr_name(self, url):
        digit = 3
        prefix = self.brand
        suffix = 'Hdr'
        body = ''.join([i for i in url if i in string.letters])
        num = len(self.handlers)
        if len(str(num)) < digit:
            body += (digit - len(str(num))) * '0' + str(num)
        else:
            body += str(num)
        return prefix + body + suffix

    def _no_endslash_url(self, url):
        if url.endswith('/'):
            return url[-1:]
        return url

    def route(self, method, url):
        url = self._no_endslash_url(url)
        hdr_name = self._hdr_name(url)
        logging.debug(hdr_name)

        if url in self.handlers:
            hdr = self.handlers[url]
        else:
            # exec 'class %s(_BaseHandler): pass' % hdr_name in globals(), locals()
            # hdr = locals()[hdr_name]
            hdr = type(hdr_name, (_BaseHandler, ), {})
            self.handlers[url] = hdr

        def route_adaptor(fn):
            setattr(hdr, method, fn)
            # hold a reference of app on the funciton
            fn.app = self
            return fn
        return route_adaptor

    def run(self):
        run_api_server(
            BaseApplication(
                handlers=[i for i in self.handlers.iteritems()],
            )
        )


if __name__ == '__main__':
    app = FlaskStyleApp('demoapp')
    app.settings.set('debug', True)

    @app.route('get', '/')
    def hello(hdr):
        hdr.write('ok')

    logging.info(hello)
    logging.info(hello.app)

    @app.route('post', '/user/profile')
    def hello_post(hdr):
        hdr.write('ok post')

{'fraction_non_alphanumeric': 0.12003244120032441, 'fraction_numerical': 0.001892403352257367, 'mean_word_length': 2.013029315960912, 'pattern_counts': {'":': 0, '<': 5, '<?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': '4854409', 'n_tokens_mistral': 1187, 'n_tokens_neox': 1126, 'n_words': 292}