Esempio n. 1
0
def test_sqla_cache(app, db, blueprint, request):
    cache = Cache(app)

    class OAuth(db.Model, OAuthConsumerMixin):
        pass

    blueprint.backend = SQLAlchemyBackend(OAuth, db.session, cache=cache)

    db.create_all()
    def done():
        db.session.remove()
        db.drop_all()
    request.addfinalizer(done)

    with record_queries(db.engine) as queries:
        with app.test_client() as client:
            # reset the session before the request
            with client.session_transaction() as sess:
                sess["test-service_oauth_state"] = "random-string"
            # make the request
            resp = client.get(
                "/login/test-service/authorized?code=secret-code&state=random-string",
                base_url="https://a.b.c",
            )
            # check that we redirected the client
            assert resp.status_code == 302
            assert resp.headers["Location"] == "https://a.b.c/oauth_done"

    assert len(queries) == 3

    expected_token = {
        "access_token": "foobar",
        "token_type": "bearer",
        "scope": [""],
    }

    # check the database
    authorizations = OAuth.query.all()
    assert len(authorizations) == 1
    oauth = authorizations[0]
    assert oauth.provider == "test-service"
    assert isinstance(oauth.token, dict)
    assert oauth.token == expected_token

    # cache should be invalidated
    assert cache.get("flask_dance_token|test-service|None") is None

    # first reference to the token should generate SQL queries
    with record_queries(db.engine) as queries:
        assert blueprint.token == expected_token
    assert len(queries) == 1

    # should now be in the cache
    assert cache.get("flask_dance_token|test-service|None") == expected_token

    # subsequent references should not generate SQL queries
    with record_queries(db.engine) as queries:
        assert blueprint.token == expected_token
    assert len(queries) == 0
Esempio n. 2
0
 def test_21_redis_url_custom_db(self):
     config = {
         'CACHE_TYPE': 'redis',
         'CACHE_REDIS_URL': 'redis://localhost:6379/2',
     }
     cache = Cache()
     cache.init_app(self.app, config=config)
     rconn = self.app.extensions['cache'][cache] \
                 ._client.connection_pool.get_connection('foo')
     assert rconn.db == 2
Esempio n. 3
0
 def test_20_redis_url_default_db(self):
     config = {
         'CACHE_TYPE': 'redis',
         'CACHE_REDIS_URL': 'redis://localhost:6379',
     }
     cache = Cache()
     cache.init_app(self.app, config=config)
     from werkzeug.contrib.cache import RedisCache
     assert isinstance(self.app.extensions['cache'][cache], RedisCache)
     rconn = self.app.extensions['cache'][cache] \
                 ._client.connection_pool.get_connection('foo')
     assert rconn.db == 0
Esempio n. 4
0
    def setUp(self):
        app = Flask(__name__, template_folder=os.path.dirname(__file__))

        app.debug = True
        self._set_app_config(app)

        self.cache = Cache(app)

        self.app = app
Esempio n. 5
0
    def test_06a_memoize(self):
        self.app.config['CACHE_DEFAULT_TIMEOUT'] = 1
        self.cache = Cache(self.app)

        with self.app.test_request_context():
            @self.cache.memoize(50)
            def big_foo(a, b):
                return a+b+random.randrange(0, 100000)

            result = big_foo(5, 2)

            time.sleep(2)

            assert big_foo(5, 2) == result
Esempio n. 6
0
 def __init__(self, app=None, options=None, db=None):
     self._app = app
     self._db = db
     self.options = options
     if self.options is not None and 'conf_file' in self.options and self.options['conf_file'] is not None:
         logging_fileConfig(self.options['conf_file'])
     self._listener = None
     self._listener_lock = None
     self._sleep = 0.25
     self.menu_left = []
     # Bower
     self.bower = Bower()
     # Caching
     self.cache = Cache()
Esempio n. 7
0
    def test_06b_memoize_func_only_special_args(self):
        self.app.config['CACHE_DEFAULT_TIMEOUT'] = 1
        self.cache = Cache(self.app)

        with self.app.test_request_context():
            @self.cache.memoize(10)
            def big_foo(*args, **kwargs):
                return sum(args)

            res_1 = big_foo(1, 2, 3)

            time.sleep(1)

            res_2 = big_foo(4, 5, 6)

            assert res_1 != res_2
            assert res_1 == 6
            assert res_2 == 15
Esempio n. 8
0
    def test_17_dict_config(self):
        cache = Cache(config={'CACHE_TYPE': 'simple'})
        cache.init_app(self.app)

        assert cache.config['CACHE_TYPE'] == 'simple'
Esempio n. 9
0
# pylint: disable=invalid-name
app = Flask("kernelci-frontend")

app.root_path = os.path.abspath(os.path.dirname(__file__))

app.config.from_object("dashboard.default_settings")
if os.path.isfile(DEFAULT_CONFIG_FILE):
    app.config.from_pyfile(DEFAULT_CONFIG_FILE)

if os.environ.get(APP_ENVVAR):
    app.config.from_envvar(APP_ENVVAR)

# Save the function.
app_conf_get = app.config.get

app.cache = Cache(app)
app.csrf = CsrfProtect(app)

# Use the custom CSRF token generation.
app.jinja_env.globals["csrf_token_r"] = generate_csrf_token

# Add the custom regular expression converter.
app.url_map.converters["regex"] = RegexConverter

# Initialize the app routes, config and other necessary stuff.
# The app context here is needed since we are using variables defined in the
# config files and we need to access them.
with app.app_context():
    import dashboard.utils.backend as backend
    import dashboard.utils.route as route
Esempio n. 10
0
from flask_cache import Cache
from flask_login import LoginManager
from flask_sslify import SSLify

from slot import app

# Set up logging
log = logging.getLogger('slot')
log.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
log.addHandler(ch)

app.config.from_object('config')
sslify = SSLify(app, age=300)
cache = Cache(app, config={'CACHE_TYPE': 'redis'})

with app.app_context():
    cache.clear()

from slot.users.views import users_blueprint
from routes import dashboard, render_new_procedure_form, receive_sms, complete_procedure
import slot.users.controller as user_controller
import db_fieldbook as db
import error_mailer

error_mailer.initialize_app(app, additional_loggers=['slot'])

# Register blueprints
app.register_blueprint(users_blueprint)
Esempio n. 11
0
    app.discover()

    # This will be removed at some point
    with app.app_context():
        if app.config.get('DB_URI'):
            db.metadata.create_all(db.get_engine(app))

    return app

# Init plugins here if possible
login_manager = LoginManager()

db = SQLAlchemy()
# Add a 5 second timeout, to also work with offline git + text editor commits
cache = Cache(config={'CACHE_DEFAULT_TIMEOUT': 5.0})
assets = Assets()
search = Search()
ldap = MyLDAPLoginManager()

assets.register('main.js',
                'vendor/jquery/dist/jquery.js',
                'vendor/components-bootstrap/js/bootstrap.js',
                'vendor/handlebars/handlebars.js',
                'vendor/js-yaml/dist/js-yaml.js',
                'vendor/markdown-it/dist/markdown-it.js',
                'vendor/markdown-it-anchor/index.0',
                'js/html-sanitizer-minified.js',  # don't minify?
                'vendor/highlightjs/highlight.pack.js',
                'vendor/parsleyjs/dist/parsley.js',
                'vendor/datatables/media/js/jquery.dataTables.js',
Esempio n. 12
0
'''
    创建应用程序,并注册相关蓝图
'''
from flask import Flask
# from flask_wtf.csrf import CsrfProtect
from flask_login import LoginManager
from app.models.base import db
from app.libs.email import mail
from flask_cache import Cache
from app.libs.limiter import Limiter

__author__ = '七月'

login_manager = LoginManager()
cache = Cache(config={'CACHE_TYPE': 'simple'})
limiter = Limiter()


def register_web_blueprint(app):
    from app.web import web
    app.register_blueprint(web)


# def register_api_blueprint(app):
#     from app.api import account
#     app.register_blueprint(account.app,url_prefix='/api')


def create_app(config=None):
    app = Flask(__name__)
Esempio n. 13
0
    - Supported web service
    - Supported communication to human by sound
    - Record and process sound of human
    - Organize code to class method
"""

__author__ = "Chau.Tran"
__version__ = "1.4"

# TO DO LISTS
# 1. Add web interface
# 2. Add to process and support sound from human

flask_chatbot = Flask(__name__)

cache = Cache(flask_chatbot, config={'CACHE_TYPE': 'simple'})
cache.init_app(flask_chatbot)


def make_cached_key():
    get_args = request.args
    post_args = request.form

    if post_args:
        return flask.request.path + '?' + urllib.urlencode(
            [(k, v) for k in sorted(post_args)
             for v in sorted(post_args.getlist(k))])

    if get_args:
        return flask.request.path + '?' + urllib.urlencode(
            [(k, v) for k in sorted(get_args)
Esempio n. 14
0
            self.api (StrictRedis instance):
        """

        # If no host or port are provided, connect to client with Flask application configuration.
        if host is None:
            host = app.config['REDIS_SERVER']
        if port is None:
            port = app.config['REDIS_PORT']

        try:
            # Initialize Redis client. Set as instance attribute.
            self.api = StrictRedis(host=host, port=port, db=db)
            return self.api
        except Exception as err:
            raise err

    def get_api(self):
        """
        Getter for Redis client
        Returns:
            self.api (StrictRedis instance):
        """
        return self.api


# py-redis - http://redis-py.readthedocs.io/en/latest/
redis_cache = RedisCache()

# Flask-Cache - https://pythonhosted.org/Flask-Cache/
flask_cache = Cache()
Esempio n. 15
0
 def test_19_dict_config_both(self):
     cache = Cache(config={'CACHE_TYPE': 'null'})
     cache.init_app(self.app, config={'CACHE_TYPE': 'simple'})
     from werkzeug.contrib.cache import SimpleCache
     assert isinstance(self.app.extensions['cache'][cache], SimpleCache)
Esempio n. 16
0
 def test_21_init_app_sets_app_attribute(self):
     cache = Cache()
     cache.init_app(self.app)
     assert cache.app == self.app
Esempio n. 17
0
def test(data):
    return data['message'].startswith('v2ex')


def handle(data, cache=None, **kwargs):
    message = data['message']
    ids = fetch(cache=cache, force=(True if u'刷新' in message else False))
    contents = []
    for id in ids:
        topic = cache.get(TOPIC_KEY.format(id))
        if not topic:
            continue
        node = topic['node']
        msg = u'<{0}|{1} [{2}]>   <{3}|{4}>'.format(TOPIC_URL.format(id),
                                                    cgi.escape(topic['title']),
                                                    topic['published'],
                                                    NODE_URL.format(node),
                                                    node)
        contents.append(msg)
    return '\n'.join(contents)


if __name__ == '__main__':
    from flask import Flask
    from flask_cache import Cache
    app = Flask(__name__)
    cache = Cache()
    cache.init_app(app, config={'CACHE_TYPE': 'simple'})
    with app.app_context():
        print handle({'message': 'v2ex'}, cache, app)
Esempio n. 18
0
from app import config  # config

#实例化app
#app = Flask(__name__)

app = Flask(
    __name__,
    template_folder='templates',  #指定模板路径,可以是相对路径,也可以是绝对路径。 
    static_folder='static',  #指定静态文件前缀,默认静态文件路径同前缀
)

CORS(app, supports_credentials=True)

#引入全局配置
app.config.from_object(config)

#跨域密匙
app.secret_key = '\x12my\x0bVO\xeb\xf8\x18\x15\xc5_?\x91\xd7h\x06AC'

#回调
login_manager = LoginManager()
login_manager.session_protection = 'strong'
login_manager.login_view = 'login'
login_manager.init_app(app=app)

#绑定对象
db = SQLAlchemy(app)

cache = Cache(app)
cache.init_app(app)
Esempio n. 19
0
# 集成第三方
from flask_cache import Cache
from flask_mail import Mail

from App.apis import init_api
from App.models import init_db

mail = Mail()

cache = Cache(config={
    'CACHE_TYPE': 'redis',
    'CACHE_REDIS_HOST': '127.0.0.1',
    'CACHE_REDIS_DB': 4
})


def init_ext(app):
    # 初始化数据库
    init_db(app)

    init_api(app)

    # 初始化邮箱模块
    mail.init_app(app)

    # 初始化缓存
    cache.init_app(app)
Esempio n. 20
0
from flask import Flask
from config import Config
from flask_cache import Cache

app = Flask(__name__)
app.config.from_object(Config)
cache = Cache(app, config={'CACHE_TYPE': 'null'})
cache.init_app(app)

app.config["CACHE_TYPE"] = "null"
app.config['GOOGLEMAPS_KEY'] = "yourGoogleMapsAPIkey"

from app import routes
Esempio n. 21
0
def register_cache(app):
    cache = Cache(config={'CACHE_TYPE': 'redis'})
    cache.init_app(app)
    return cache
Esempio n. 22
0
 def _config_caching(self):
     from flask_cache import Cache
     self.cache = Cache(self)
Esempio n. 23
0
#/usr/bin/env python3
# -*- coding: utf-8 -*-

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from config import config
from flaskext.markdown import Markdown
from flask_cache import Cache
import pymysql

pymysql.install_as_MySQLdb()

db = SQLAlchemy()
cache = Cache()


def create_app(config_name):
    app = Flask(__name__)
    app.config.from_object(config[config_name])
    config[config_name].init_app(app)
    db.init_app(app)
    cache.init_app(app)
    Markdown(app)
    from .main import main as main_bluprint
    app.register_blueprint(main_bluprint)
    from .api_1_0 import api as api_1_0_blueprint
    app.register_blueprint(api_1_0_blueprint)

    return app
Esempio n. 24
0
class CacheTestCase(unittest.TestCase):
    def _set_app_config(self, app):
        app.config['CACHE_TYPE'] = 'simple'

    def setUp(self):
        app = Flask(__name__, template_folder=os.path.dirname(__file__))

        app.debug = True
        self._set_app_config(app)

        self.cache = Cache(app)

        self.app = app

    def tearDown(self):
        self.app = None
        self.cache = None
        self.tc = None

    def test_00_set(self):
        self.cache.set('hi', 'hello')

        assert self.cache.get('hi') == 'hello'

    def test_01_add(self):
        self.cache.add('hi', 'hello')

        assert self.cache.get('hi') == 'hello'

        self.cache.add('hi', 'foobar')

        assert self.cache.get('hi') == 'hello'

    def test_02_delete(self):
        self.cache.set('hi', 'hello')

        self.cache.delete('hi')

        assert self.cache.get('hi') is None

    def test_03_cached_view(self):
        @self.app.route('/')
        @self.cache.cached(5)
        def cached_view():
            return str(time.time())

        tc = self.app.test_client()

        rv = tc.get('/')
        the_time = rv.data.decode('utf-8')

        time.sleep(2)

        rv = tc.get('/')

        assert the_time == rv.data.decode('utf-8')

        time.sleep(5)

        rv = tc.get('/')
        assert the_time != rv.data.decode('utf-8')

    def test_04_cached_view_unless(self):
        @self.app.route('/a')
        @self.cache.cached(5, unless=lambda: True)
        def non_cached_view():
            return str(time.time())

        @self.app.route('/b')
        @self.cache.cached(5, unless=lambda: False)
        def cached_view():
            return str(time.time())

        tc = self.app.test_client()

        rv = tc.get('/a')
        the_time = rv.data.decode('utf-8')

        time.sleep(1)

        rv = tc.get('/a')
        assert the_time != rv.data.decode('utf-8')

        rv = tc.get('/b')
        the_time = rv.data.decode('utf-8')

        time.sleep(1)
        rv = tc.get('/b')

        assert the_time == rv.data.decode('utf-8')

    def test_05_cached_function(self):

        with self.app.test_request_context():

            @self.cache.cached(2, key_prefix='MyBits')
            def get_random_bits():
                return [random.randrange(0, 2) for i in range(50)]

            my_list = get_random_bits()
            his_list = get_random_bits()

            assert my_list == his_list

            time.sleep(4)

            his_list = get_random_bits()

            assert my_list != his_list

    def test_06_memoize(self):

        with self.app.test_request_context():

            @self.cache.memoize(5)
            def big_foo(a, b):
                return a + b + random.randrange(0, 100000)

            result = big_foo(5, 2)

            time.sleep(1)

            assert big_foo(5, 2) == result

            result2 = big_foo(5, 3)
            assert result2 != result

            time.sleep(6)

            assert big_foo(5, 2) != result

            time.sleep(1)

            assert big_foo(5, 3) != result2

    def test_06a_memoize(self):
        self.app.config['CACHE_DEFAULT_TIMEOUT'] = 1
        self.cache = Cache(self.app)

        with self.app.test_request_context():

            @self.cache.memoize(50)
            def big_foo(a, b):
                return a + b + random.randrange(0, 100000)

            result = big_foo(5, 2)

            time.sleep(2)

            assert big_foo(5, 2) == result

    def test_07_delete_memoize(self):

        with self.app.test_request_context():

            @self.cache.memoize(5)
            def big_foo(a, b):
                return a + b + random.randrange(0, 100000)

            result = big_foo(5, 2)
            result2 = big_foo(5, 3)

            time.sleep(1)

            assert big_foo(5, 2) == result
            assert big_foo(5, 2) == result
            assert big_foo(5, 3) != result
            assert big_foo(5, 3) == result2

            self.cache.delete_memoized(big_foo)

            assert big_foo(5, 2) != result
            assert big_foo(5, 3) != result2

    def test_07b_delete_memoized_verhash(self):
        with self.app.test_request_context():

            @self.cache.memoize(5)
            def big_foo(a, b):
                return a + b + random.randrange(0, 100000)

            result = big_foo(5, 2)
            result2 = big_foo(5, 3)

            time.sleep(1)

            assert big_foo(5, 2) == result
            assert big_foo(5, 2) == result
            assert big_foo(5, 3) != result
            assert big_foo(5, 3) == result2

            self.cache.delete_memoized_verhash(big_foo)

            _fname, _fname_instance = function_namespace(big_foo)
            version_key = self.cache._memvname(_fname)
            assert self.cache.get(version_key) is None

            assert big_foo(5, 2) != result
            assert big_foo(5, 3) != result2

            assert self.cache.get(version_key) is not None

    def test_08_delete_memoize(self):

        with self.app.test_request_context():

            @self.cache.memoize()
            def big_foo(a, b):
                return a + b + random.randrange(0, 100000)

            result_a = big_foo(5, 1)
            result_b = big_foo(5, 2)

            assert big_foo(5, 1) == result_a
            assert big_foo(5, 2) == result_b
            self.cache.delete_memoized(big_foo, 5, 2)

            assert big_foo(5, 1) == result_a
            assert big_foo(5, 2) != result_b

            ## Cleanup bigfoo 5,1 5,2 or it might conflict with
            ## following run if it also uses memecache
            self.cache.delete_memoized(big_foo, 5, 2)
            self.cache.delete_memoized(big_foo, 5, 1)

    def test_09_args_memoize(self):

        with self.app.test_request_context():

            @self.cache.memoize()
            def big_foo(a, b):
                return sum(a) + sum(b) + random.randrange(0, 100000)

            result_a = big_foo([5, 3, 2], [1])
            result_b = big_foo([3, 3], [3, 1])

            assert big_foo([5, 3, 2], [1]) == result_a
            assert big_foo([3, 3], [3, 1]) == result_b

            self.cache.delete_memoized(big_foo, [5, 3, 2], [1])

            assert big_foo([5, 3, 2], [1]) != result_a
            assert big_foo([3, 3], [3, 1]) == result_b

            ## Cleanup bigfoo 5,1 5,2 or it might conflict with
            ## following run if it also uses memecache
            self.cache.delete_memoized(big_foo, [5, 3, 2], [1])
            self.cache.delete_memoized(big_foo, [3, 3], [1])

    def test_10_kwargs_memoize(self):

        with self.app.test_request_context():

            @self.cache.memoize()
            def big_foo(a, b=None):
                return a + sum(b.values()) + random.randrange(0, 100000)

            result_a = big_foo(1, dict(one=1, two=2))
            result_b = big_foo(5, dict(three=3, four=4))

            assert big_foo(1, dict(one=1, two=2)) == result_a
            assert big_foo(5, dict(three=3, four=4)) == result_b

            self.cache.delete_memoized(big_foo, 1, dict(one=1, two=2))

            assert big_foo(1, dict(one=1, two=2)) != result_a
            assert big_foo(5, dict(three=3, four=4)) == result_b

    def test_10a_kwargonly_memoize(self):

        with self.app.test_request_context():

            @self.cache.memoize()
            def big_foo(a=None):
                if a is None:
                    a = 0
                return a + random.random()

            result_a = big_foo()
            result_b = big_foo(5)

            assert big_foo() == result_a
            assert big_foo() < 1
            assert big_foo(5) == result_b
            assert big_foo(5) >= 5 and big_foo(5) < 6

    def test_10a_arg_kwarg_memoize(self):
        with self.app.test_request_context():

            @self.cache.memoize()
            def f(a, b, c=1):
                return a + b + c + random.randrange(0, 100000)

            assert f(1, 2) == f(1, 2, c=1)
            assert f(1, 2) == f(1, 2, 1)
            assert f(1, 2) == f(1, 2)
            assert f(1, 2, 3) != f(1, 2)
            with self.assertRaises(TypeError):
                f(1)

    def test_10b_classarg_memoize(self):
        @self.cache.memoize()
        def bar(a):
            return a.value + random.random()

        class Adder(object):
            def __init__(self, value):
                self.value = value

        adder = Adder(15)
        adder2 = Adder(20)

        y = bar(adder)
        z = bar(adder2)

        assert y != z
        assert bar(adder) == y
        assert bar(adder) != z
        adder.value = 14
        assert bar(adder) == y
        assert bar(adder) != z

        assert bar(adder) != bar(adder2)
        assert bar(adder2) == z

    def test_10c_classfunc_memoize(self):
        class Adder(object):
            def __init__(self, initial):
                self.initial = initial

            @self.cache.memoize()
            def add(self, b):
                return self.initial + b

        adder1 = Adder(1)
        adder2 = Adder(2)

        x = adder1.add(3)
        assert adder1.add(3) == x
        assert adder1.add(4) != x
        assert adder1.add(3) != adder2.add(3)

    def test_10d_classfunc_memoize_delete(self):
        with self.app.test_request_context():

            class Adder(object):
                def __init__(self, initial):
                    self.initial = initial

                @self.cache.memoize()
                def add(self, b):
                    return self.initial + b + random.random()

            adder1 = Adder(1)
            adder2 = Adder(2)

            a1 = adder1.add(3)
            a2 = adder2.add(3)

            assert a1 != a2
            assert adder1.add(3) == a1
            assert adder2.add(3) == a2

            self.cache.delete_memoized(adder1.add)

            a3 = adder1.add(3)
            a4 = adder2.add(3)

            self.assertNotEqual(a1, a3)
            assert a1 != a3
            self.assertEqual(a2, a4)

            self.cache.delete_memoized(Adder.add)

            a5 = adder1.add(3)
            a6 = adder2.add(3)

            self.assertNotEqual(a5, a6)
            self.assertNotEqual(a3, a5)
            self.assertNotEqual(a4, a6)

    def test_10e_delete_memoize_classmethod(self):
        with self.app.test_request_context():

            class Mock(object):
                @classmethod
                @self.cache.memoize(5)
                def big_foo(cls, a, b):
                    return a + b + random.randrange(0, 100000)

            result = Mock.big_foo(5, 2)
            result2 = Mock.big_foo(5, 3)

            time.sleep(1)

            assert Mock.big_foo(5, 2) == result
            assert Mock.big_foo(5, 2) == result
            assert Mock.big_foo(5, 3) != result
            assert Mock.big_foo(5, 3) == result2

            self.cache.delete_memoized(Mock.big_foo)

            assert Mock.big_foo(5, 2) != result
            assert Mock.big_foo(5, 3) != result2

    def test_11_cache_key_property(self):
        @self.app.route('/')
        @self.cache.cached(5)
        def cached_view():
            return str(time.time())

        assert hasattr(cached_view, "make_cache_key")
        assert callable(cached_view.make_cache_key)

        tc = self.app.test_client()

        rv = tc.get('/')
        the_time = rv.data.decode('utf-8')

        with self.app.test_request_context():
            cache_data = self.cache.get(cached_view.make_cache_key())
            assert the_time == cache_data

    def test_12_make_cache_key_function_property(self):
        @self.app.route('/<foo>/<bar>')
        @self.cache.memoize(5)
        def cached_view(foo, bar):
            return str(time.time())

        assert hasattr(cached_view, "make_cache_key")
        assert callable(cached_view.make_cache_key)

        tc = self.app.test_client()

        rv = tc.get('/a/b')
        the_time = rv.data.decode('utf-8')

        cache_key = cached_view.make_cache_key(cached_view.uncached,
                                               foo=u"a",
                                               bar=u"b")
        cache_data = self.cache.get(cache_key)
        assert the_time == cache_data

        different_key = cached_view.make_cache_key(cached_view.uncached,
                                                   foo=u"b",
                                                   bar=u"a")
        different_data = self.cache.get(different_key)
        assert the_time != different_data

    def test_13_cache_timeout_property(self):
        @self.app.route('/')
        @self.cache.memoize(5)
        def cached_view1():
            return str(time.time())

        @self.app.route('/<foo>/<bar>')
        @self.cache.memoize(10)
        def cached_view2(foo, bar):
            return str(time.time())

        assert hasattr(cached_view1, "cache_timeout")
        assert hasattr(cached_view2, "cache_timeout")
        assert cached_view1.cache_timeout == 5
        assert cached_view2.cache_timeout == 10

        # test that this is a read-write property
        cached_view1.cache_timeout = 15
        cached_view2.cache_timeout = 30

        assert cached_view1.cache_timeout == 15
        assert cached_view2.cache_timeout == 30

        tc = self.app.test_client()

        rv1 = tc.get('/')
        time1 = rv1.data.decode('utf-8')
        time.sleep(1)
        rv2 = tc.get('/a/b')
        time2 = rv2.data.decode('utf-8')

        # VIEW1
        # it's been 1 second, cache is still active
        assert time1 == tc.get('/').data.decode('utf-8')
        time.sleep(16)
        # it's been >15 seconds, cache is not still active
        assert time1 != tc.get('/').data.decode('utf-8')

        # VIEW2
        # it's been >17 seconds, cache is still active
        self.assertEqual(time2, tc.get('/a/b').data.decode('utf-8'))
        time.sleep(30)
        # it's been >30 seconds, cache is not still active
        assert time2 != tc.get('/a/b').data.decode('utf-8')

    def test_14_memoized_multiple_arg_kwarg_calls(self):
        with self.app.test_request_context():

            @self.cache.memoize()
            def big_foo(a, b, c=[1, 1], d=[1, 1]):
                return sum(a) + sum(b) + sum(c) + sum(d) + random.randrange(
                    0, 100000)

            result_a = big_foo([5, 3, 2], [1], c=[3, 3], d=[3, 3])

            assert big_foo([5, 3, 2], [1], d=[3, 3], c=[3, 3]) == result_a
            assert big_foo(b=[1], a=[5, 3, 2], c=[3, 3], d=[3, 3]) == result_a
            assert big_foo([5, 3, 2], [1], [3, 3], [3, 3]) == result_a

    def test_15_memoize_multiple_arg_kwarg_delete(self):
        with self.app.test_request_context():

            @self.cache.memoize()
            def big_foo(a, b, c=[1, 1], d=[1, 1]):
                return sum(a) + sum(b) + sum(c) + sum(d) + random.randrange(
                    0, 100000)

            result_a = big_foo([5, 3, 2], [1], c=[3, 3], d=[3, 3])
            self.cache.delete_memoized(big_foo, [5, 3, 2], [1], [3, 3], [3, 3])
            result_b = big_foo([5, 3, 2], [1], c=[3, 3], d=[3, 3])
            assert result_a != result_b

            self.cache.delete_memoized(big_foo, [5, 3, 2],
                                       b=[1],
                                       c=[3, 3],
                                       d=[3, 3])
            result_b = big_foo([5, 3, 2], [1], c=[3, 3], d=[3, 3])
            assert result_a != result_b

            self.cache.delete_memoized(big_foo, [5, 3, 2], [1],
                                       c=[3, 3],
                                       d=[3, 3])
            result_a = big_foo([5, 3, 2], [1], c=[3, 3], d=[3, 3])
            assert result_a != result_b

            self.cache.delete_memoized(big_foo, [5, 3, 2],
                                       b=[1],
                                       c=[3, 3],
                                       d=[3, 3])
            result_a = big_foo([5, 3, 2], [1], c=[3, 3], d=[3, 3])
            assert result_a != result_b

            self.cache.delete_memoized(big_foo, [5, 3, 2], [1],
                                       c=[3, 3],
                                       d=[3, 3])
            result_b = big_foo([5, 3, 2], [1], c=[3, 3], d=[3, 3])
            assert result_a != result_b

            self.cache.delete_memoized(big_foo, [5, 3, 2], [1], [3, 3], [3, 3])
            result_a = big_foo([5, 3, 2], [1], c=[3, 3], d=[3, 3])
            assert result_a != result_b

    def test_16_memoize_kwargs_to_args(self):
        with self.app.test_request_context():

            def big_foo(a, b, c=None, d=None):
                return sum(a) + sum(b) + random.randrange(0, 100000)

            expected = (1, 2, 'foo', 'bar')

            args, kwargs = self.cache._memoize_kwargs_to_args(
                big_foo, 1, 2, 'foo', 'bar')
            assert (args == expected)
            args, kwargs = self.cache._memoize_kwargs_to_args(big_foo,
                                                              2,
                                                              'foo',
                                                              'bar',
                                                              a=1)
            assert (args == expected)
            args, kwargs = self.cache._memoize_kwargs_to_args(big_foo,
                                                              a=1,
                                                              b=2,
                                                              c='foo',
                                                              d='bar')
            assert (args == expected)
            args, kwargs = self.cache._memoize_kwargs_to_args(big_foo,
                                                              d='bar',
                                                              b=2,
                                                              a=1,
                                                              c='foo')
            assert (args == expected)
            args, kwargs = self.cache._memoize_kwargs_to_args(big_foo,
                                                              1,
                                                              2,
                                                              d='bar',
                                                              c='foo')
            assert (args == expected)

    def test_17_dict_config(self):
        cache = Cache(config={'CACHE_TYPE': 'simple'})
        cache.init_app(self.app)

        assert cache.config['CACHE_TYPE'] == 'simple'

    def test_18_dict_config_initapp(self):
        cache = Cache()
        cache.init_app(self.app, config={'CACHE_TYPE': 'simple'})
        from werkzeug.contrib.cache import SimpleCache
        assert isinstance(self.app.extensions['cache'][cache], SimpleCache)

    def test_19_dict_config_both(self):
        cache = Cache(config={'CACHE_TYPE': 'null'})
        cache.init_app(self.app, config={'CACHE_TYPE': 'simple'})
        from werkzeug.contrib.cache import SimpleCache
        assert isinstance(self.app.extensions['cache'][cache], SimpleCache)

    def test_20_jinja2ext_cache(self):
        somevar = ''.join(
            [random.choice(string.ascii_letters) for x in range(6)])

        testkeys = [
            make_template_fragment_key("fragment1"),
            make_template_fragment_key("fragment1", vary_on=["key1"]),
            make_template_fragment_key("fragment1", vary_on=["key1", somevar]),
        ]
        delkey = make_template_fragment_key("fragment2")

        with self.app.test_request_context():
            #: Test if elements are cached
            render_template("test_template.html", somevar=somevar, timeout=60)
            for k in testkeys:
                assert self.cache.get(k) == somevar
            assert self.cache.get(delkey) == somevar

            #: Test timeout=del to delete key
            render_template("test_template.html",
                            somevar=somevar,
                            timeout="del")
            for k in testkeys:
                assert self.cache.get(k) == somevar
            assert self.cache.get(delkey) is None

            #: Test rendering templates from strings
            output = render_template_string(
                """{% cache 60, "fragment3" %}{{somevar}}{% endcache %}""",
                somevar=somevar)
            assert self.cache.get(
                make_template_fragment_key("fragment3")) == somevar
            assert output == somevar

            #: Test backwards compatibility
            output = render_template_string(
                """{% cache 30 %}{{somevar}}{% endcache %}""", somevar=somevar)
            assert self.cache.get(
                make_template_fragment_key("None1")) == somevar
            assert output == somevar

            output = render_template_string(
                """{% cache 30, "fragment4", "fragment5"%}{{somevar}}{% endcache %}""",
                somevar=somevar)
            k = make_template_fragment_key("fragment4", vary_on=["fragment5"])
            assert self.cache.get(k) == somevar
            assert output == somevar
Esempio n. 25
0
app = Flask(__name__)
app.config.from_object(CONFIG_MODULE)
conf = app.config

if not app.debug:
    # In production mode, add log handler to sys.stderr.
    app.logger.addHandler(logging.StreamHandler())
    app.logger.setLevel(logging.INFO)
logging.getLogger('pyhive.presto').setLevel(logging.INFO)

db = SQLA(app)


utils.pessimistic_connection_handling(db.engine.pool)

cache = Cache(app, config=app.config.get('CACHE_CONFIG'))
tables_cache = Cache(app, config=app.config.get('TABLE_NAMES_CACHE_CONFIG'))


migrate = Migrate(app, db, directory=APP_DIR + "/migrations")

# Logging configuration
logging.basicConfig(format=app.config.get('LOG_FORMAT'))
logging.getLogger().setLevel(app.config.get('LOG_LEVEL'))

if app.config.get('ENABLE_TIME_ROTATE'):
    logging.getLogger().setLevel(app.config.get('TIME_ROTATE_LOG_LEVEL'))
    handler = TimedRotatingFileHandler(app.config.get('FILENAME'),
                                       when=app.config.get('ROLLOVER'),
                                       interval=app.config.get('INTERVAL'),
                                       backupCount=app.config.get('BACKUP_COUNT'))
Esempio n. 26
0
 def test_19_dict_config_both(self):
     cache = Cache(config={'CACHE_TYPE': 'null'})
     cache.init_app(self.app, config={'CACHE_TYPE': 'simple'})
     from werkzeug.contrib.cache import SimpleCache
     assert isinstance(self.app.extensions['cache'][cache], SimpleCache)
Esempio n. 27
0
VERSION = version.VERSION_STRING

APP_DIR = os.path.dirname(__file__)
CONFIG_MODULE = os.environ.get('CARAVEL_CONFIG', 'caravel.config')

app = Flask(__name__)
app.config.from_object(CONFIG_MODULE)
if not app.debug:
    # In production mode, add log handler to sys.stderr.
    app.logger.addHandler(logging.StreamHandler())
    app.logger.setLevel(logging.INFO)

db = SQLA(app)

cache = Cache(app, config=app.config.get('CACHE_CONFIG'))

migrate = Migrate(app, db, directory=APP_DIR + "/migrations")

# Logging configuration
logging.basicConfig(format=app.config.get('LOG_FORMAT'))
logging.getLogger().setLevel(app.config.get('LOG_LEVEL'))

if app.config.get('ENABLE_TIME_ROTATE'):
    logging.getLogger().setLevel(app.config.get('TIME_ROTATE_LOG_LEVEL'))
    handler = TimedRotatingFileHandler(
        app.config.get('FILENAME'),
        when=app.config.get('ROLLOVER'),
        interval=app.config.get('INTERVAL'),
        backupCount=app.config.get('BACKUP_COUNT'))
    logging.getLogger().addHandler(handler)
Esempio n. 28
0
import flask as f
import wtforms as wt
from wtforms import validators as vdr

from flask_cache import Cache
from flask_wtf import Form

try:
    from google.appengine.api import memcache
    cache_type = 'gaememcached'
except ImportError:
    cache_type = 'simple'


cache = Cache(config={'CACHE_TYPE': cache_type})


def make_app(name):
    """Creates a Flask application."""
    app = f.Flask(__name__)
    app.config.from_object('invatar.configs')
    cache.init_app(app)
    return app


class ApiForm(Form):
    """API request model."""
    s = wt.IntegerField('Size', [vdr.Optional(),
                                 vdr.NumberRange(min=10, max=2000)])
    h = wt.StringField('Identifier', [vdr.Optional()])
    font_size = wt.IntegerField('Font size', [vdr.Optional(),
Esempio n. 29
0
from flask import Flask
from flask_restful import Resource, Api, reqparse
from flask_cache import Cache
from flask_pymongo import PyMongo
from flask_cors import CORS
import pymongo
import logging
import json

app = Flask(__name__)
CORS(app, origins=["*"])
app.config.from_pyfile('config.py')
api = Api(app)
mongo = PyMongo(app)
cache = Cache(app, config={'CACHE_TYPE': 'redis'})

parser = reqparse.RequestParser()
parser.add_argument('url', location='json', required=True)
parser.add_argument('data', location='json', required=True)


class Task(Resource):
    def post(self):
        args = parser.parse_args()
        data = json.loads(args.data)
        save2db(data, args.url)

    def get(self):
        return {
            'url':
Esempio n. 30
0
def setup_cache(app, cache_config):
    """Setup the flask-cache on a flask app"""
    if cache_config and cache_config.get('CACHE_TYPE') != 'null':
        return Cache(app, config=cache_config)
Esempio n. 31
0
    def setUp(self):
        if not foo.setup_call:
            test_config = {
                'LIVEBLOG_DEBUG': True,
                'EMBED_PROTOCOL': 'http://',
                'CORS_ENABLED': False,
                'DEBUG': False,
            }
            self.app.config.update(test_config)
            foo.setup_called()
            blogs.init_app(self.app)
            items_app.init_app(self.app)
            users_app.init_app(self.app)
            client_modules.init_app(self.app)
            self.app.register_blueprint(blog_posts_blueprint)
            self.client = self.app.test_client()
            self.app.cache = Cache(self.app, config={'CACHE_TYPE': 'simple'})

        self.client_item_service = get_resource_service('client_items')
        self.client_comment_service = get_resource_service('client_comments')

        self.item_docs = [{
            "commenter": "test commenter",
            "text": "test comment",
            "client_blog": ObjectId("5ab90249fd16ad1752b39b74"),
            "item_type": "comment",
            "_updated": datetime.datetime(2018, 4, 11, 6, 43, 47),
            "_created": datetime.datetime(2018, 4, 11, 6, 43, 47),
            "type": "text",
            "pubstatus": "usable",
            "flags": {
                "marked_for_not_publication": False,
                "marked_for_legal": False,
                "marked_archived_only": False,
                "marked_for_sms": False
            },
            "format": "HTML",
            "particular_type": "item",
            "group_type": "default",
            "meta": {},
            "_current_version": 1
        }]

        res = self.app.data.insert('client_items', self.item_docs)

        self.comment_docs = [{
            "post_status":
            "comment",
            "client_blog":
            ObjectId("5ab90249fd16ad1752b39b74"),
            "groups": [{
                "id": "root",
                "refs": [{
                    "idRef": "main"
                }],
                "role": "grpRole:NEP"
            }, {
                "id": "main",
                "refs": [{
                    "residRef": res[0]
                }],
                "role": "grpRole:Main"
            }],
            "_updated":
            datetime.datetime(2018, 4, 11, 6, 44, 1),
            "_created":
            datetime.datetime(2018, 4, 11, 6, 44, 1),
            "type":
            "text",
            "pubstatus":
            "usable",
            "flags": {
                "marked_for_not_publication": False,
                "marked_for_legal": False,
                "marked_archived_only": False,
                "marked_for_sms": False
            },
            "format":
            "HTML",
            "particular_type":
            "post",
            "lb_highlight":
            False,
            "sticky":
            False,
            "deleted":
            False,
            "order":
            0.0,
            "_current_version":
            1
        }]

        self.blog_post_service = get_resource_service('client_blog_posts')
        self.blogs_service = get_resource_service('blogs')
        self.client_blog_service = get_resource_service('client_blogs')
        self.users_service = get_resource_service('users')

        self.user_list = [{
            '_created': '2018-03-20T00:00:00+00:00',
            '_updated': '2018-03-20T10:00:00+00:00',
            'username': '******',
            'display_name': 'Edwin the admin',
            'first_name': 'Edwin',
            'is_active': True,
            'is_enabled': True,
            'last_name': 'the admin',
            'sign_off': 'off',
            'byline': 'by',
            'email': '*****@*****.**',
        }]

        self.user_ids = self.app.data.insert('users', self.user_list)

        self.blogs_list = [{
            "_created": "2018-03-27T12:04:58+00:00",
            "_etag": "b962afec2413ddf43fcf0273a1a422a2fec1e34d",
            "_type": "blogs",
            "_updated": "2018-04-03T05:54:32+00:00",
            "blog_preferences": {
                "language": "en",
                "theme": "classic"
            },
            "blog_status": "open",
            "category": "",
            "description": "title: end to end Five",
            "firstcreated": "2018-03-27T12:04:58+00:00",
            "last_created_post": {
                "_id":
                "urn:newsml:localhost:2018-04-03T11:12:43.187311:ad5e39b1-2fb2-4676-bd2f-425dca184765",
                "_updated": "2018-04-03T05:42:43+00:00"
            },
            "last_updated_post": {
                "_id":
                "urn:newsml:localhost:2018-04-03T11:12:43.187311:ad5e39b1-2fb2-4676-bd2f-425dca184765",
                "_updated": "2018-04-03T05:43:12+00:00"
            },
            "market_enabled": False,
            "members": [],
            "original_creator": self.user_ids[0],
            "picture":
            "urn:newsml:localhost:2018-03-27T17:34:58.093848:973b4459-5511-45fc-9bfe-4855159ea917",
            "picture_renditions": {
                "baseImage": {
                    "height": 1075,
                    "href":
                    "http://*****:*****@other.com",
                "sign_off": "abc",
                "username": "******"
            },
            "pubstatus":
            "usable",
            "schedule_settings": {
                "time_zone": None,
                "utc_embargo": None
            },
            "sign_off":
            "abc",
            "source":
            "Liveblog",
            "state":
            "draft",
            "sticky":
            False,
            "type":
            "composite",
            "unique_id":
            235,
            "unique_name":
            "#235",
            "urgency":
            3,
            "version_creator":
            self.user_ids[0],
            "versioncreated":
            "2018-04-03T05:42:43+00:00"
        }]

        self.blog_post_ids = self.app.data.insert('client_blog_posts',
                                                  self.blog_posts)
Esempio n. 32
0
from flask_cache import Cache
from log import slogger
import file as ufile
import sys

log = slogger('dquest-ext')

cache = Cache(
    config={
        'CACHE_TYPE': 'filesystem',
        'CACHE_DEFAULT_TIMEOUT': 86400,
        'CACHE_THRESHOLD': 500,
        'CACHE_DIR': 'app/resources/cache'
    })
Esempio n. 33
0
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from flask import Flask
from flask_cache import Cache
from flask_login import LoginManager
from flask_mail import Mail

from app.models.base import db

login_manager = LoginManager()  #实例化登录模块的对象
cache = Cache()  #实例化缓存对象

mail = Mail()


def creat_app():
    app = Flask(__name__)
    app.config.from_object('app.source')  # 引入config.py配置文件
    app.config.from_object('app.setting')
    # 调用下面的 def registe_blueprint(app)函数注册app
    # 实际上执行这句代码   web = blueprints.Blueprint('web',__name__)
    #得到一个蓝图对象
    registe_blueprint(app)

    login_manager.init_app(app)  #注册login_manager
    login_manager.login_view = 'web.login'  #不会报没有权限的错误,而是直接跳转到登录页面
    login_manager.login_message = '请先登录或注册'  #跳转登录页面后的提示信息

    with app.app_context():  #这三行跟下面注释的两行效果一样
        db.init_app(app)
        db.create_all()
Esempio n. 34
0
import os
from flask_cache import Cache
from app.load_app import app

cache = Cache(app,
              config={
                  'CACHE_TYPE': os.environ.get('CACHE_TYPE', 'filesystem'),
                  'CACHE_DIR': os.path.join(app.config['BASE_DIR'], 'cache')
              })
Esempio n. 35
0
from flask_cache import Cache
from flask_debugtoolbar import DebugToolbarExtension
from flask_migrate import Migrate
from flask_restful import Api
from flask_session import Session
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()
migrate = Migrate()
debugtoolbar = DebugToolbarExtension()
cache = Cache(config={
    "CACHE_TYPE": 'redis',
    "CACHE_KEY_PREFIX": 'AD_',
    "ACHE_DEFAULT_TIMEOUT": 60 * 2,
    #TODO:和别的使用redis的服务分开数据库,使用密码
    # "CACHE_REDIS_URL": 'redis://*****:*****@localhost:6379/2',
})
api = Api()

def init_extension(app):
    db.init_app(app)
    Session(app)
    migrate.init_app(app, db=db)
    debugtoolbar.init_app(app)
    cache.init_app(app)
    api.init_app(app)

Esempio n. 36
0
 def setup_cache(self):
     # configuring for in-memory cache for now
     self.cache = Cache(self)
Esempio n. 37
0
app = Flask(__name__)
CORS(app)
app.config.from_pyfile('settings.py')

# config cache
if app.config['CACHE_TYPE'] == 'redis':

    redis_auth = app.config['REDIS_USER'] + ':' + app.config[
        'REDIS_PASSWD'] + '@' \
        if app.config['REDIS_USER'] and app.config['REDIS_PASSWD'] else ''
    redis_url = 'redis://' + redis_auth + app.config['REDIS_HOST'] + \
                ':' + app.config['REDIS_PORT']

    cache = Cache(app,
                  config={
                      'CACHE_TYPE': 'redis',
                      'CACHE_DEFAULT_TIMEOUT': 0,
                      'CACHE_REDIS_URL': redis_url
                  })

elif app.config['CACHE_TYPE'] == 'simple':
    cache = Cache(app,
                  config={
                      'CACHE_TYPE': 'simple',
                      'CACHE_DEFAULT_TIMEOUT': 0
                  })

else:
    print("Invalid cache type.")
    sys.exit(1)

# keep temporary request errors
Esempio n. 38
0
def register_cache(app):
    cache = Cache()
    cache.init_app(app)
    return cache
Esempio n. 39
0
import time

from flask import Blueprint, render_template, request, redirect, session, url_for, flash
from flask_cache import Cache
from HomeworkApp.models import Student, User

blue = Blueprint("myblue", __name__)

c = Cache()


@blue.route("/student/<int:sid>/")
@c.cached(timeout=30)
def stuedent(sid):
    print("查询MySQL数据库......")
    time.sleep(3)
    student = Student.query.get(sid)
    if student:
        return render_template("student.html", student=student)
    else:
        return "<h3 style='color:red'>无此学生的信息!</h3>"


@blue.route("/login/", methods=["GET", "POST"])
def login_view():
    if request.method == "GET":
        return render_template('login.html')
    elif request.method == "POST":
        logname = request.form.get("logname")
        logpwd = request.form.get("logpwd")
        user = User.query.filter_by(username=logname, password=logpwd).first()
Esempio n. 40
0
class CacheTestCase(unittest.TestCase):

    def _set_app_config(self, app):
        app.config['CACHE_TYPE'] = 'simple'

    def setUp(self):
        app = Flask(__name__, template_folder=os.path.dirname(__file__))

        app.debug = True
        self._set_app_config(app)

        self.cache = Cache(app)

        self.app = app

    def tearDown(self):
        self.app = None
        self.cache = None
        self.tc = None

    def test_00_set(self):
        self.cache.set('hi', 'hello')

        assert self.cache.get('hi') == 'hello'

    def test_01_add(self):
        self.cache.add('hi', 'hello')

        assert self.cache.get('hi') == 'hello'

        self.cache.add('hi', 'foobar')

        assert self.cache.get('hi') == 'hello'

    def test_02_delete(self):
        self.cache.set('hi', 'hello')

        self.cache.delete('hi')

        assert self.cache.get('hi') is None

    def test_03_cached_view(self):

        @self.app.route('/')
        @self.cache.cached(5)
        def cached_view():
            return str(time.time())

        tc = self.app.test_client()

        rv = tc.get('/')
        the_time = rv.data.decode('utf-8')

        time.sleep(2)

        rv = tc.get('/')

        assert the_time == rv.data.decode('utf-8')

        time.sleep(5)

        rv = tc.get('/')
        assert the_time != rv.data.decode('utf-8')

    def test_04_cached_view_unless(self):
        @self.app.route('/a')
        @self.cache.cached(5, unless=lambda: True)
        def non_cached_view():
            return str(time.time())

        @self.app.route('/b')
        @self.cache.cached(5, unless=lambda: False)
        def cached_view():
            return str(time.time())

        tc = self.app.test_client()

        rv = tc.get('/a')
        the_time = rv.data.decode('utf-8')

        time.sleep(1)

        rv = tc.get('/a')
        assert the_time != rv.data.decode('utf-8')

        rv = tc.get('/b')
        the_time = rv.data.decode('utf-8')

        time.sleep(1)
        rv = tc.get('/b')

        assert the_time == rv.data.decode('utf-8')

    def test_05_cached_function(self):

        with self.app.test_request_context():
            @self.cache.cached(2, key_prefix='MyBits')
            def get_random_bits():
                return [random.randrange(0, 2) for i in range(50)]

            my_list = get_random_bits()
            his_list = get_random_bits()

            assert my_list == his_list

            time.sleep(4)

            his_list = get_random_bits()

            assert my_list != his_list

    def test_06_memoize(self):

        with self.app.test_request_context():
            @self.cache.memoize(5)
            def big_foo(a, b):
                return a+b+random.randrange(0, 100000)

            result = big_foo(5, 2)

            time.sleep(1)

            assert big_foo(5, 2) == result

            result2 = big_foo(5, 3)
            assert result2 != result

            time.sleep(6)

            assert big_foo(5, 2) != result

            time.sleep(1)

            assert big_foo(5, 3) != result2

    def test_06a_memoize(self):
        self.app.config['CACHE_DEFAULT_TIMEOUT'] = 1
        self.cache = Cache(self.app)

        with self.app.test_request_context():
            @self.cache.memoize(50)
            def big_foo(a, b):
                return a+b+random.randrange(0, 100000)

            result = big_foo(5, 2)

            time.sleep(2)

            assert big_foo(5, 2) == result

    def test_07_delete_memoize(self):

        with self.app.test_request_context():
            @self.cache.memoize(5)
            def big_foo(a, b):
                return a+b+random.randrange(0, 100000)

            result = big_foo(5, 2)
            result2 = big_foo(5, 3)

            time.sleep(1)

            assert big_foo(5, 2) == result
            assert big_foo(5, 2) == result
            assert big_foo(5, 3) != result
            assert big_foo(5, 3) == result2

            self.cache.delete_memoized(big_foo)

            assert big_foo(5, 2) != result
            assert big_foo(5, 3) != result2

    def test_07b_delete_memoized_verhash(self):
        with self.app.test_request_context():
            @self.cache.memoize(5)
            def big_foo(a, b):
                return a+b+random.randrange(0, 100000)

            result = big_foo(5, 2)
            result2 = big_foo(5, 3)

            time.sleep(1)

            assert big_foo(5, 2) == result
            assert big_foo(5, 2) == result
            assert big_foo(5, 3) != result
            assert big_foo(5, 3) == result2

            self.cache.delete_memoized_verhash(big_foo)

            _fname, _fname_instance = function_namespace(big_foo)
            version_key = self.cache._memvname(_fname)
            assert self.cache.get(version_key) is None

            assert big_foo(5, 2) != result
            assert big_foo(5, 3) != result2

            assert self.cache.get(version_key) is not None

    def test_08_delete_memoize(self):

        with self.app.test_request_context():
            @self.cache.memoize()
            def big_foo(a, b):
                return a+b+random.randrange(0, 100000)

            result_a = big_foo(5, 1)
            result_b = big_foo(5, 2)

            assert big_foo(5, 1) == result_a
            assert big_foo(5, 2) == result_b
            self.cache.delete_memoized(big_foo, 5, 2)

            assert big_foo(5, 1) == result_a
            assert big_foo(5, 2) != result_b

            ## Cleanup bigfoo 5,1 5,2 or it might conflict with
            ## following run if it also uses memecache
            self.cache.delete_memoized(big_foo, 5, 2)
            self.cache.delete_memoized(big_foo, 5, 1)

    def test_09_args_memoize(self):

        with self.app.test_request_context():
            @self.cache.memoize()
            def big_foo(a, b):
                return sum(a)+sum(b)+random.randrange(0, 100000)

            result_a = big_foo([5,3,2], [1])
            result_b = big_foo([3,3], [3,1])

            assert big_foo([5,3,2], [1]) == result_a
            assert big_foo([3,3], [3,1]) == result_b

            self.cache.delete_memoized(big_foo, [5,3,2], [1])

            assert big_foo([5,3,2], [1]) != result_a
            assert big_foo([3,3], [3,1]) == result_b

            ## Cleanup bigfoo 5,1 5,2 or it might conflict with
            ## following run if it also uses memecache
            self.cache.delete_memoized(big_foo, [5,3,2], [1])
            self.cache.delete_memoized(big_foo, [3,3], [1])

    def test_10_kwargs_memoize(self):

        with self.app.test_request_context():
            @self.cache.memoize()
            def big_foo(a, b=None):
                return a+sum(b.values())+random.randrange(0, 100000)

            result_a = big_foo(1, dict(one=1,two=2))
            result_b = big_foo(5, dict(three=3,four=4))

            assert big_foo(1, dict(one=1,two=2)) == result_a
            assert big_foo(5, dict(three=3,four=4)) == result_b

            self.cache.delete_memoized(big_foo, 1, dict(one=1,two=2))

            assert big_foo(1, dict(one=1,two=2)) != result_a
            assert big_foo(5, dict(three=3,four=4)) == result_b

    def test_10a_kwargonly_memoize(self):

        with self.app.test_request_context():
            @self.cache.memoize()
            def big_foo(a=None):
                if a is None:
                    a = 0
                return a+random.random()

            result_a = big_foo()
            result_b = big_foo(5)

            assert big_foo() == result_a
            assert big_foo() < 1
            assert big_foo(5) == result_b
            assert big_foo(5) >= 5 and big_foo(5) < 6

    def test_10a_arg_kwarg_memoize(self):
        with self.app.test_request_context():
            @self.cache.memoize()
            def f(a, b, c=1):
                return a+b+c+random.randrange(0, 100000)

            assert f(1,2) == f(1,2,c=1)
            assert f(1,2) == f(1,2,1)
            assert f(1,2) == f(1,2)
            assert f(1,2,3) != f(1,2)
            with self.assertRaises(TypeError):
                f(1)

    def test_10b_classarg_memoize(self):

        @self.cache.memoize()
        def bar(a):
            return a.value + random.random()

        class Adder(object):
            def __init__(self, value):
                self.value = value

        adder = Adder(15)
        adder2 = Adder(20)

        y = bar(adder)
        z = bar(adder2)

        assert y != z
        assert bar(adder) == y
        assert bar(adder) != z
        adder.value = 14
        assert bar(adder) == y
        assert bar(adder) != z

        assert bar(adder) != bar(adder2)
        assert bar(adder2) == z

    def test_10c_classfunc_memoize(self):
        class Adder(object):
            def __init__(self, initial):
                self.initial = initial

            @self.cache.memoize()
            def add(self, b):
                return self.initial + b

        adder1 = Adder(1)
        adder2 = Adder(2)

        x = adder1.add(3)
        assert adder1.add(3) == x
        assert adder1.add(4) != x
        assert adder1.add(3) != adder2.add(3)

    def test_10d_classfunc_memoize_delete(self):
        with self.app.test_request_context():
            class Adder(object):
                def __init__(self, initial):
                    self.initial = initial

                @self.cache.memoize()
                def add(self, b):
                    return self.initial + b + random.random()

            adder1 = Adder(1)
            adder2 = Adder(2)

            a1 = adder1.add(3)
            a2 = adder2.add(3)

            assert a1 != a2
            assert adder1.add(3) == a1
            assert adder2.add(3) == a2

            self.cache.delete_memoized(adder1.add)

            a3 = adder1.add(3)
            a4 = adder2.add(3)

            self.assertNotEqual(a1, a3)
            assert a1 != a3
            self.assertEqual(a2, a4)

            self.cache.delete_memoized(Adder.add)

            a5 = adder1.add(3)
            a6 = adder2.add(3)

            self.assertNotEqual(a5, a6)
            self.assertNotEqual(a3, a5)
            self.assertNotEqual(a4, a6)

    def test_10e_delete_memoize_classmethod(self):
        with self.app.test_request_context():
            class Mock(object):
                @classmethod
                @self.cache.memoize(5)
                def big_foo(cls, a, b):
                    return a+b+random.randrange(0, 100000)

            result = Mock.big_foo(5, 2)
            result2 = Mock.big_foo(5, 3)

            time.sleep(1)

            assert Mock.big_foo(5, 2) == result
            assert Mock.big_foo(5, 2) == result
            assert Mock.big_foo(5, 3) != result
            assert Mock.big_foo(5, 3) == result2

            self.cache.delete_memoized(Mock.big_foo)

            assert Mock.big_foo(5, 2) != result
            assert Mock.big_foo(5, 3) != result2

    def test_11_cache_key_property(self):
        @self.app.route('/')
        @self.cache.cached(5)
        def cached_view():
            return str(time.time())

        assert hasattr(cached_view, "make_cache_key")
        assert callable(cached_view.make_cache_key)

        tc = self.app.test_client()

        rv = tc.get('/')
        the_time = rv.data.decode('utf-8')

        with self.app.test_request_context():
            cache_data = self.cache.get(cached_view.make_cache_key())
            assert the_time == cache_data

    def test_12_make_cache_key_function_property(self):
        @self.app.route('/<foo>/<bar>')
        @self.cache.memoize(5)
        def cached_view(foo, bar):
            return str(time.time())

        assert hasattr(cached_view, "make_cache_key")
        assert callable(cached_view.make_cache_key)

        tc = self.app.test_client()

        rv = tc.get('/a/b')
        the_time = rv.data.decode('utf-8')

        cache_key = cached_view.make_cache_key(cached_view.uncached, foo=u"a", bar=u"b")
        cache_data = self.cache.get(cache_key)
        assert the_time == cache_data

        different_key = cached_view.make_cache_key(cached_view.uncached, foo=u"b", bar=u"a")
        different_data = self.cache.get(different_key)
        assert the_time != different_data

    def test_13_cache_timeout_property(self):
        @self.app.route('/')
        @self.cache.memoize(5)
        def cached_view1():
            return str(time.time())

        @self.app.route('/<foo>/<bar>')
        @self.cache.memoize(10)
        def cached_view2(foo, bar):
            return str(time.time())

        assert hasattr(cached_view1, "cache_timeout")
        assert hasattr(cached_view2, "cache_timeout")
        assert cached_view1.cache_timeout == 5
        assert cached_view2.cache_timeout == 10

        # test that this is a read-write property
        cached_view1.cache_timeout = 15
        cached_view2.cache_timeout = 30

        assert cached_view1.cache_timeout == 15
        assert cached_view2.cache_timeout == 30

        tc = self.app.test_client()

        rv1 = tc.get('/')
        time1 = rv1.data.decode('utf-8')
        time.sleep(1)
        rv2 = tc.get('/a/b')
        time2 = rv2.data.decode('utf-8')

        # VIEW1
        # it's been 1 second, cache is still active
        assert time1 == tc.get('/').data.decode('utf-8')
        time.sleep(16)
        # it's been >15 seconds, cache is not still active
        assert time1 != tc.get('/').data.decode('utf-8')

        # VIEW2
        # it's been >17 seconds, cache is still active
        self.assertEqual(time2, tc.get('/a/b').data.decode('utf-8'))
        time.sleep(30)
        # it's been >30 seconds, cache is not still active
        assert time2 != tc.get('/a/b').data.decode('utf-8')

    def test_14_memoized_multiple_arg_kwarg_calls(self):
        with self.app.test_request_context():
            @self.cache.memoize()
            def big_foo(a, b,c=[1,1],d=[1,1]):
                return sum(a)+sum(b)+sum(c)+sum(d)+random.randrange(0, 100000)

            result_a = big_foo([5,3,2], [1], c=[3,3], d=[3,3])

            assert big_foo([5,3,2], [1], d=[3,3], c=[3,3]) == result_a
            assert big_foo(b=[1],a=[5,3,2],c=[3,3],d=[3,3]) == result_a
            assert big_foo([5,3,2], [1], [3,3], [3,3]) == result_a

    def test_15_memoize_multiple_arg_kwarg_delete(self):
        with self.app.test_request_context():
            @self.cache.memoize()
            def big_foo(a, b,c=[1,1],d=[1,1]):
                return sum(a)+sum(b)+sum(c)+sum(d)+random.randrange(0, 100000)

            result_a = big_foo([5,3,2], [1], c=[3,3], d=[3,3])
            self.cache.delete_memoized(big_foo, [5,3,2],[1],[3,3],[3,3])
            result_b = big_foo([5,3,2], [1], c=[3,3], d=[3,3])
            assert result_a != result_b

            self.cache.delete_memoized(big_foo, [5,3,2],b=[1],c=[3,3],d=[3,3])
            result_b = big_foo([5,3,2], [1], c=[3,3], d=[3,3])
            assert result_a != result_b

            self.cache.delete_memoized(big_foo, [5,3,2],[1],c=[3,3],d=[3,3])
            result_a = big_foo([5,3,2], [1], c=[3,3], d=[3,3])
            assert result_a != result_b

            self.cache.delete_memoized(big_foo, [5,3,2],b=[1],c=[3,3],d=[3,3])
            result_a = big_foo([5,3,2], [1], c=[3,3], d=[3,3])
            assert result_a != result_b

            self.cache.delete_memoized(big_foo, [5,3,2],[1],c=[3,3],d=[3,3])
            result_b = big_foo([5,3,2], [1], c=[3,3], d=[3,3])
            assert result_a != result_b

            self.cache.delete_memoized(big_foo, [5,3,2],[1],[3,3],[3,3])
            result_a = big_foo([5,3,2], [1], c=[3,3], d=[3,3])
            assert result_a != result_b

    def test_16_memoize_kwargs_to_args(self):
        with self.app.test_request_context():
            def big_foo(a, b, c=None, d=None):
                return sum(a)+sum(b)+random.randrange(0, 100000)

            expected = (1,2,'foo','bar')

            args, kwargs = self.cache._memoize_kwargs_to_args(big_foo, 1,2,'foo','bar')
            assert (args == expected)
            args, kwargs = self.cache._memoize_kwargs_to_args(big_foo, 2,'foo','bar',a=1)
            assert (args == expected)
            args, kwargs = self.cache._memoize_kwargs_to_args(big_foo, a=1,b=2,c='foo',d='bar')
            assert (args == expected)
            args, kwargs = self.cache._memoize_kwargs_to_args(big_foo, d='bar',b=2,a=1,c='foo')
            assert (args == expected)
            args, kwargs = self.cache._memoize_kwargs_to_args(big_foo, 1,2,d='bar',c='foo')
            assert (args == expected)

    def test_17_dict_config(self):
        cache = Cache(config={'CACHE_TYPE': 'simple'})
        cache.init_app(self.app)

        assert cache.config['CACHE_TYPE'] == 'simple'

    def test_18_dict_config_initapp(self):
        cache = Cache()
        cache.init_app(self.app, config={'CACHE_TYPE': 'simple'})
        from werkzeug.contrib.cache import SimpleCache
        assert isinstance(self.app.extensions['cache'][cache], SimpleCache)

    def test_19_dict_config_both(self):
        cache = Cache(config={'CACHE_TYPE': 'null'})
        cache.init_app(self.app, config={'CACHE_TYPE': 'simple'})
        from werkzeug.contrib.cache import SimpleCache
        assert isinstance(self.app.extensions['cache'][cache], SimpleCache)

    def test_20_jinja2ext_cache(self):
        somevar = ''.join([random.choice(string.ascii_letters) for x in range(6)])

        testkeys = [
            make_template_fragment_key("fragment1"),
            make_template_fragment_key("fragment1", vary_on=["key1"]),
            make_template_fragment_key("fragment1", vary_on=["key1", somevar]),
        ]
        delkey = make_template_fragment_key("fragment2")

        with self.app.test_request_context():
            #: Test if elements are cached
            render_template("test_template.html", somevar=somevar, timeout=60)
            for k in testkeys:
                assert self.cache.get(k) == somevar
            assert self.cache.get(delkey) == somevar

            #: Test timeout=del to delete key
            render_template("test_template.html", somevar=somevar, timeout="del")
            for k in testkeys:
                assert self.cache.get(k) == somevar
            assert self.cache.get(delkey) is None

            #: Test rendering templates from strings
            output = render_template_string(
                """{% cache 60, "fragment3" %}{{somevar}}{% endcache %}""",
                somevar=somevar
            )
            assert self.cache.get(make_template_fragment_key("fragment3")) == somevar
            assert output == somevar

            #: Test backwards compatibility
            output = render_template_string(
                """{% cache 30 %}{{somevar}}{% endcache %}""",
                somevar=somevar)
            assert self.cache.get(make_template_fragment_key("None1")) == somevar
            assert output == somevar

            output = render_template_string(
                """{% cache 30, "fragment4", "fragment5"%}{{somevar}}{% endcache %}""",
                somevar=somevar)
            k = make_template_fragment_key("fragment4", vary_on=["fragment5"])
            assert self.cache.get(k) == somevar
            assert output == somevar
Bootstrap(app)
login_manager = LoginManager()
login_manager.init_app(app)

DEFAULT_CALLBACK_PATH = app.config['DEFAULT_CALLBACK_PATH']
HOST = app.config['HOST']  # This host's name
CLIENT_SECRET = app.config['CLIENT_SECRET']  # Client Secret
CLIENT_ID = app.config['CLIENT_ID']  # Client ID
REALM = app.config['REALM']  # Keycloak realm
OIDC_HOST = app.config['OIDC_HOST']  # Keycloak host
OIDC_INFO_URL = '{:s}/auth/realms/{:s}/'.format(OIDC_HOST, REALM)
OIDC_REDIRECT_URI = 'http://{:s}/{:s}'.format(HOST, DEFAULT_CALLBACK_PATH)


# Initialize Cache
app.cache = Cache(app, config={'CACHE_TYPE': 'simple'})

# initialize components
from models import User

db_adapter = SQLAlchemyAdapter(db, User)
user_manager = UserManager(db_adapter=db_adapter, app=app, login_manager=login_manager)

import views

from um import um

app.register_blueprint(um)

from cm import cm
Esempio n. 42
0
    def test_17_dict_config(self):
        cache = Cache(config={'CACHE_TYPE': 'simple'})
        cache.init_app(self.app)

        assert cache.config['CACHE_TYPE'] == 'simple'
Esempio n. 43
0
def create_app(config=None):
    app = Flask(__name__)
    app.secret_key = configuration.get('webserver', 'SECRET_KEY')
    app.config['LOGIN_DISABLED'] = not configuration.getboolean(
        'webserver', 'AUTHENTICATE')

    csrf.init_app(app)

    #app.config = config
    airflow.load_login()
    airflow.login.login_manager.init_app(app)

    cache = Cache(app=app,
                  config={
                      'CACHE_TYPE': 'filesystem',
                      'CACHE_DIR': '/tmp'
                  })

    app.register_blueprint(routes)

    with app.app_context():
        from airflow.www import views

        admin = Admin(
            app,
            name='Airflow',
            static_url_path='/admin',
            index_view=views.HomeView(endpoint='', url='/admin', name="DAGs"),
            template_mode='bootstrap3',
        )
        av = admin.add_view
        vs = views
        av(vs.Airflow(name='DAGs', category='DAGs'))

        av(vs.QueryView(name='Ad Hoc Query', category="Data Profiling"))
        av(
            vs.ChartModelView(models.Chart,
                              Session,
                              name="Charts",
                              category="Data Profiling"))
        av(
            vs.KnowEventView(models.KnownEvent,
                             Session,
                             name="Known Events",
                             category="Data Profiling"))
        av(
            vs.SlaMissModelView(models.SlaMiss,
                                Session,
                                name="SLA Misses",
                                category="Browse"))
        av(
            vs.TaskInstanceModelView(models.TaskInstance,
                                     Session,
                                     name="Task Instances",
                                     category="Browse"))
        av(vs.LogModelView(models.Log, Session, name="Logs",
                           category="Browse"))
        av(
            vs.JobModelView(jobs.BaseJob,
                            Session,
                            name="Jobs",
                            category="Browse"))
        av(
            vs.PoolModelView(models.Pool,
                             Session,
                             name="Pools",
                             category="Admin"))
        av(vs.ConfigurationView(name='Configuration', category="Admin"))
        av(
            vs.UserModelView(models.User,
                             Session,
                             name="Users",
                             category="Admin"))
        av(
            vs.ConnectionModelView(models.Connection,
                                   Session,
                                   name="Connections",
                                   category="Admin"))
        av(
            vs.VariableView(models.Variable,
                            Session,
                            name="Variables",
                            category="Admin"))

        admin.add_link(
            base.MenuLink(category='Docs',
                          name='Documentation',
                          url='http://pythonhosted.org/airflow/'))
        admin.add_link(
            base.MenuLink(category='Docs',
                          name='Github',
                          url='https://github.com/airbnb/airflow'))

        av(vs.VersionView(name='Version', category="About"))

        av(
            vs.DagRunModelView(models.DagRun,
                               Session,
                               name="DAG Runs",
                               category="Browse"))
        av(vs.DagModelView(models.DagModel, Session, name=None))
        # Hack to not add this view to the menu
        admin._menu = admin._menu[:-1]

        def integrate_plugins():
            """Integrate plugins to the context"""
            from airflow.plugins_manager import (admin_views, flask_blueprints,
                                                 menu_links)
            for v in admin_views:
                admin.add_view(v)
            for bp in flask_blueprints:
                app.register_blueprint(bp)
            for ml in sorted(menu_links, key=lambda x: x.name):
                admin.add_link(ml)

        integrate_plugins()

        @app.context_processor
        def jinja_globals():
            return {
                'hostname': socket.getfqdn(),
            }

        @app.teardown_appcontext
        def shutdown_session(exception=None):
            settings.Session.remove()

        return app
Esempio n. 44
0
def register_cache(app):
    cache = Cache(config={'CACHE_TYPE': 'redis'})
    cache.init_app(app)
    return cache
Esempio n. 45
0
from flask_compress import Compress
from flask_cache import Cache

# Initialize a developmentConfig in case of APP_SETTINGS not provided

app_settings = os.getenv('APP_SETTINGS', 'config.DevelopmentConfig')

app = Flask(__name__,
            template_folder='templates',
            static_folder='templates/static')
Compress(app)

# create multiple sqlalchemy engines
db = SQLAlchemy(app)

cache = Cache(config={'CACHE_TYPE': 'simple'})
cache.init_app(app)

# set config parameters
app.config.from_object(app_settings)

# auth basic
auth = HTTPBasicAuth()

# Compress Extenstion
Compress(app)

# Cors - Multiples Origins HEADERS
cors = CORS(app, resources={r"/v1/*": {"origins": "*"}})

# OAuth2 Server Provider
Esempio n. 46
0
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied. See the License for the specific language governing
# permissions and limitations under the License.

import os
import sys
from flask import Flask, jsonify, request
from flask import current_app
#from flask.ext.cache import Cache
from flask_cache import Cache
sys.path.append(os.path.abspath("../supv"))
from rf import *

#REST service random forest prediction
app = Flask(__name__)
cache = Cache()
app.config['CACHE_TYPE'] = 'simple'
cache.init_app(app)

configPath = sys.argv[1]
portNum = int(sys.argv[2])

@app.route('/rf/predict/<string:recs>', methods=['GET'])
def predict(recs):
	print recs
	nrecs = recs.replace(",,", "\n")
	print nrecs
 	resp = getResponse(nrecs)
	return resp

Esempio n. 47
0
class FlaskJanitoo(object):

    def __init__(self, app=None, options=None, db=None):
        self._app = app
        self._db = db
        self.options = options
        if self.options is not None and 'conf_file' in self.options and self.options['conf_file'] is not None:
            logging_fileConfig(self.options['conf_file'])
        self._listener = None
        self._listener_lock = None
        self._sleep = 0.25
        self.menu_left = []
        # Bower
        self.bower = Bower()
        # Caching
        self.cache = Cache()

    def __del__(self):
        """
        """
        try:
            self.stop_listener()
        except Exception:
            pass

    def init_app(self, app, options, db=None):
        """
        """
        if app is not None:
            self._app = app
        if options is not None:
            self.options = options
        if db is not None:
            self._db = db
        if self.options is not None and 'conf_file' in self.options and self.options['conf_file'] is not None:
            logging_fileConfig(self.options['conf_file'])

        # Flask-Cache
        self.cache.init_app(self._app)
        # Flask-Bower
        self.bower.init_app(self._app)

        self._event_manager = EventManager(self._app)
        self._app.jinja_env.globals["emit_event"] = self._event_manager.template_emit
        if not hasattr(self._app, 'extensions'):
            self._app.extensions = {}
        self._app.extensions['options'] = self.options
        self._app.extensions['bower'] = self.bower
        self._app.extensions['cache'] = self.cache
        self._app.extensions['janitoo'] = self
        try:
            self._sleep = int(self._app.config['FLASKJANITOO_SLEEP'])
            if self._sleep <= 0 :
                self._sleep = 0.25
        except KeyError:
            self._sleep = 0.25
        except ValueError:
            self._sleep = 0.25
        # Use the newstyle teardown_appcontext if it's available,
        # otherwise fall back to the request context
        if hasattr(self._app, 'teardown_appcontext'):
            self._app.teardown_appcontext(self.teardown)
        else:
            self._app.teardown_request(self.teardown)
        signal.signal(signal.SIGTERM, self.signal_term_handler)
        signal.signal(signal.SIGINT, self.signal_term_handler)
        self._listener_lock = threading.Lock()

        self.create_listener()

    def create_listener(self):
        """Create the listener on first call
        """
        self._listener = ListenerThread(self._app, self.options)

    @property
    def listener(self):
        """Start the listener on first call
        """
        self.start_listener()
        return self._listener

    def start_listener(self):
        """Start the listener on first call
        """
        try:
            self._listener_lock.acquire()
            if not self._listener.is_alive():
                self._listener.start()
        finally:
            self._listener_lock.release()

    def stop_listener(self):
        """Stop the listener
        """
        try:
            self._listener_lock.acquire()
            self._listener.stop()
            try:
                self._listener.join()
            except RuntimeError:
                pass
            self._listener = None
        finally:
            self._listener_lock.release()

    def extend_blueprints(self, group):
        """
        """
        for entrypoint in iter_entry_points(group = '%s.blueprint'%'janitoo_manager'):
            logger.info('Add blueprint from %s', entrypoint.module_name )
            bprint, url = entrypoint.load()()
            self._app.register_blueprint(bprint, url_prefix=url)
        for entrypoint in iter_entry_points(group = '%s.menu_left'%group):
            logger.info('Extend menu_left with %s', entrypoint.module_name )
            self.menu_left.append(entrypoint.load()())
        self._app.template_context_processors[None].append(lambda : dict(jnt_menu_left=self.menu_left))

    def extend_network(self, group):
        """"Extend the network with methods found in entrypoints
        """
        if self._listener and self._listener.network:
            self._listener.network.extend_from_entry_points(group)
        else:
            raise RuntimeError("Can't extend an uninitialized network")

    def extend_listener(self, group):
        """"Extend the network with methods found in entrypoints
        """
        if self._listener:
            self._listener.extend_from_entry_points(group)
        else:
            raise RuntimeError("Can't extend an uninitialized listener")

    def signal_term_handler(self, signal, frame):
        """
        """
        logger.info("[ %s ] - Received signal %s", self.__class__.__name__, signal)
        if self._listener and self._listener.is_alive():
            self._listener.stop()
            try:
                self._listener.join()
            except AssertionError:
                logger.exception("Catched exception in signal_term_handler: ")
            except RuntimeError:
                logger.exception("Catched exception in signal_term_handler: ")
        sys.exit(0)

    #~ def connect(self):
        #~ return sqlite3.connect(current_app.config['SQLITE3_DATABASE'])

    #~ @property
    #~ def backend(self):
        #~ ctx = stack.top
        #~ if ctx is not None:
            #~ if not hasattr(ctx, 'tinyflow_backend'):
                #~ ctx.tinyflow_backend = self._backend
            #~ return ctx.tinyflow_backend
#~
    #~ @property
    #~ def thread(self):
        #~ ctx = stack.top
        #~ if ctx is not None:
            #~ if not hasattr(ctx, 'tinyflow_server'):
                #~ ctx.tinyflow_server = self._server
            #~ return ctx.tinyflow_server
#~
    def teardown(self, exception):
        pass