<commit_before># -*- coding: utf-8 -*-
'''
    A Flask extension integrating relib.
'''
from __future__ import absolute_import

import logging

from flask import _app_ctx_stack
from driftconfig.relib import get_store_from_url


log = logging.getLogger(__name__)


class FlaskRelib(object):

    def __init__(self, app=None):
        if app is not None:
            self.init_app(app)

    def init_app(self, app):
        app.extensions['relib'] = self
        app.before_request(self._before_request)
        self._table_store = None

        url = app.config.get('RELIB_CONFIG_URL')
        if url:
            try:
                self._table_store = get_store_from_url(url)
            except Exception:
                log.exception("Relib DB failed loading from '%s'.", url)

    def _before_request(self):
        '''
        Bla
        '''
        setattr(_app_ctx_stack.top, 'ts', self._table_store)<commit_msg>Adjustments to flask middleware.<commit_after># -*- coding: utf-8 -*-
'''
    A Flask extension integrating relib.
'''
from __future__ import absolute_import

import logging

from flask import _app_ctx_stack, current_app
from flask import _app_ctx_stack as stack
from driftconfig.relib import get_store_from_url, create_backend



log = logging.getLogger(__name__)


class FlaskRelib(object):

    def __init__(self, app=None):
        if app is not None:
            self.init_app(app)

    def init_app(self, app):
        app.extensions['relib'] = self
        app.before_request(self._before_request)

    def refresh(self):
        """Invalidate Redis cache, if in use, and fetch new config from source."""
        ctx = stack.top
        if ctx is not None and hasattr(ctx, 'table_store'):
            delattr(ctx, 'table_store')

    def save(self, table_store):
        """Save 'table_store' to source."""
        b = create_backend(current_app.config.get('RELIB_CONFIG_URL'))
        table_store.save(b)
        self.refresh()

    @property
    def table_store(self):
        ctx = stack.top
        if ctx is not None:
            if not hasattr(ctx, 'table_store'):
                url = current_app.config.get('RELIB_CONFIG_URL')
                ctx.table_store = get_store_from_url(url)
            return ctx.table_store

    def _before_request(self):
        '''
        Bla
        '''
        pass
        #print "GHOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOORK YESYESYEYSEYSE"
        #ctx = _app_ctx_stack.top
        ##if ctx is not None:
            #ctx.table_store = self._table_store
{'fraction_non_alphanumeric': 0.09765780071456927, 'fraction_numerical': 0.0007939658594680428, 'mean_word_length': 2.4710743801652892, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, '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': '3797865', 'n_tokens_mistral': 786, 'n_tokens_neox': 757, 'n_words': 189}