<commit_before>#!/usr/bin/env python
# coding=utf8
import json, sys, os, datetime

from setting import useport, CACHE_TIMEOUT
from flask import Flask, g, request, Response, session, redirect, render_template
from flask.templating import DispatchingJinjaLoader
from flask.globals import _request_ctx_stack
from flask.ext.sqlalchemy import SQLAlchemy
from werkzeug.contrib.cache import SimpleCache
from werkzeug.routing import BaseConverter
from util.session import Session

from blueprint.task.views import task
from blueprint.script.views import script
from blueprint.user.views import user

cache = SimpleCache()
def cached(func):
    def decorator(*args, **kwargs):
        key = request.path + '&'.join(['%s=%s'%(k,v.encode('utf8')) for k,v in
                                      request.args.items()])
        response = cache.get(key)
        if response is None:
            print 'call func:', key
            response = func(*args, **kwargs)
            cache.set(key, response, CACHE_TIMEOUT)
        return response
    return decorator


class LeafinlineLoader(DispatchingJinjaLoader):
    def _iter_loaders(self, template):
        bp = _request_ctx_stack.top.request.blueprint
        if bp is not None and bp in self.app.blueprints:
            loader = self.app.blueprints[bp].jinja_loader
            if loader is not None:
                yield loader, template

        loader = self.app.jinja_loader
        if loader is not None:
            yield loader, template


app = Flask(__name__, static_folder='static', static_path='/static', template_folder='template')
app.config.from_object(__name__)
app.secret_key = 'super secret key'
app.config['SESSION_TYPE'] = 'redis'
app.config['SESSION_PERMANENT'] = False
app.permanent_session_lifetime = datetime.timedelta(days=1)
Session(app)
# app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://hotel2:hotel0115@172.16.31.10:3306/hotel20'
# db = SQLAlchemy(app)
# g['db'] = db

app.jinja_options = Flask.jinja_options.copy() 
app.jinja_options['loader'] = LeafinlineLoader(app)

app.register_blueprint(admin, url_prefix='/api/task')
app.register_blueprint(script, url_prefix='/api/script')
app.register_blueprint(user, url_prefix='/api/user')

@app.route('/', methods=['GET'])
def index():
    return render_template('index.html', appname=g.appname)


# @app.context_processor
# def override_url_for():
#     return dict(url_for=static_url_for)

# def static_url_for(endpoint, **values):
#     if endpoint == 'static':
#         filename = values.get('filename', None)
#         if filename:
#             file_path = STATIC_URL_ROOT + filename
#             return file_path
#     else:
#         return url_for(endpoint, **values)

class RegexConverter(BaseConverter):
    def __init__(self, map, *args):<commit_msg>fix bug<commit_after>#!/usr/bin/env python
# coding=utf8
import json, sys, os, datetime

from setting import useport, CACHE_TIMEOUT
from flask import Flask, g, request, Response, session, redirect, render_template
from flask.templating import DispatchingJinjaLoader
from flask.globals import _request_ctx_stack
from flask.ext.sqlalchemy import SQLAlchemy
from werkzeug.contrib.cache import SimpleCache
from werkzeug.routing import BaseConverter
from util.session import Session

from blueprint.task.views import task
from blueprint.script.views import script
from blueprint.user.views import user

cache = SimpleCache()
def cached(func):
    def decorator(*args, **kwargs):
        key = request.path + '&'.join(['%s=%s'%(k,v.encode('utf8')) for k,v in
                                      request.args.items()])
        response = cache.get(key)
        if response is None:
            print 'call func:', key
            response = func(*args, **kwargs)
            cache.set(key, response, CACHE_TIMEOUT)
        return response
    return decorator


class LeafinlineLoader(DispatchingJinjaLoader):
    def _iter_loaders(self, template):
        bp = _request_ctx_stack.top.request.blueprint
        if bp is not None and bp in self.app.blueprints:
            loader = self.app.blueprints[bp].jinja_loader
            if loader is not None:
                yield loader, template

        loader = self.app.jinja_loader
        if loader is not None:
            yield loader, template


app = Flask(__name__, static_folder='static', static_path='/static', template_folder='template')
app.config.from_object(__name__)
app.secret_key = 'super secret key'
app.config['SESSION_TYPE'] = 'redis'
app.config['SESSION_PERMANENT'] = False
app.permanent_session_lifetime = datetime.timedelta(days=1)
Session(app)
# app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://hotel2:hotel0115@172.16.31.10:3306/hotel20'
# db = SQLAlchemy(app)
# g['db'] = db

app.jinja_options = Flask.jinja_options.copy() 
app.jinja_options['loader'] = LeafinlineLoader(app)

app.register_blueprint(admin, url_prefix='/api/task')
app.register_blueprint(script, url_prefix='/api/script')
app.register_blueprint(user, url_prefix='/api/user')

@app.route('/', methods=['GET'])
def index():
    return render_template('index.html', appname=g.appname)


def allow_cross_domain(fun):
    @wraps(fun)
    def wrapper_fun(*args, **kwargs):
        rst = fun(*args, **kwargs)
        if type(rst) == str:
            rst = make_response(fun(*args, **kwargs))
        rst.headers['Access-Control-Allow-Origin'] = '*'
        return rst
    return wrapper_fun

class CJsonEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, datetime):
            return obj.strftime('%Y-%m-%d %H:%M:%S')
        elif isinstance(obj, date):
            return obj.strftime('%Y-%m-%d')
        else:
            return json.JSONEncoder.default(self, obj)
# @app.context_processor
# def override_url_for():
#     return dict(url_for=static_url_for)

# def static_url_for(endpoint, **values):
#     if endpoint == 'static':
#         filename = values.get('filename', None)
#         if filename:
#             file_path = STATIC_URL_ROOT + filename
#             return file_path
#     else:
#         return url_for(endpoint, **values)

class RegexConverter(BaseConverter):
    def __init__(self, map, *args):
{'fraction_non_alphanumeric': 0.11901282675759052, 'fraction_numerical': 0.007468744926124371, 'mean_word_length': 3.5260837619397503, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1295816', 'n_tokens_mistral': 1966, 'n_tokens_neox': 1895, 'n_words': 440}