<commit_before># -*- coding: utf-8 -*-
"""
    flaskext.mongokit
    ~~~~~~~~~~~~~~~~~

    Make the integration of :mod:`mongokit` simpler into :mod:`Flask`.
    
    :copyright: 2011 by <NAME> <<EMAIL>
    :license: BSD, see LICENSE for more details.
"""

from __future__ import absolute_import

from mongokit import Connection, Database, Document
from flask import _request_ctx_stack

class MongoKit(object):
    
    def __init__(self, app=None):
        #: list of MongoKit.Document which will be automated registed at
        #: connection
        self.registered_documents = []
        
        if app is not None:
            self.app = app
            self.init_app(self.app)
        else:
            self.app = None

    def init_app(self, app):
        app.config.setdefault('MONGODB_HOST', '127.0.0.1')
        app.config.setdefault('MONGODB_PORT', 27017)
        app.config.setdefault('MONGODB_DATABASE', 'flask')
        app.config.setdefault('MONGODB_SLAVE_OKAY', False)
        app.config.setdefault('MONGODB_USERNAME', None)
        app.config.setdefault('MONGODB_PASSWORD', None)

        app.before_request(self.open_connection)
        if hasattr(app, 'teardown_request'):
            app.teardown_request(self.close_connection)
        else:
            app.after_request(self.close_connection)
    
        # register extension with app
        app.extensions = getattr(app, 'extensions', {})
        app.extensions['mongokit'] = self
        
        self.app = app
    
    def register(self, documents):
        for document in documents:
            if document not in self.registered_documents:
                self.registered_documents.append(document)
        return self.registered_documents
    
    def connect(self):
        ctx = _request_ctx_stack.top
        ctx.mongokit_connection = Connection(
            host=self.app.config.get('MONGODB_HOST'),
            port=self.app.config.get('MONGODB_PORT'),
            slave_okay=self.app.config.get('MONGODB_SLAVE_OKAY')
        )
        ctx.mongokit_connection.register(self.registered_documents)
        ctx.mongokit_db = Database(ctx.mongokit_connection,     
                                   self.app.config.get('MONGODB_DATABASE'))
        if self.app.config.get('MONGODB_USERNAME') is not None:
            ctx.mongokit_db.authenticate(
                self.app.config.get('MONGODB_USERNAME'),
                self.app.config.get('MONGODB_PASSWORD')
            )
        return ctx.mongokit_db
     
    def disconnect(self):
        ctx = _request_ctx_stack.top
        ctx.mongokit_connection.disconnect()
       
    def open_connection(self):
        return self.connect()
        
    def close_connection(self, request):
        self.disconnect()
        return request<commit_msg>Rename open_ and close_ connection functions to before and teardown also add __getattr__ as forworder to the database object<commit_after># -*- coding: utf-8 -*-
"""
    flaskext.mongokit
    ~~~~~~~~~~~~~~~~~

    Make the integration of :mod:`mongokit` simpler into :mod:`Flask`.
    
    :copyright: 2011 by <NAME> <<EMAIL>
    :license: BSD, see LICENSE for more details.
"""

from __future__ import absolute_import

from mongokit import Connection, Database, Document
from flask import _request_ctx_stack

class MongoKit(object):
    
    def __init__(self, app=None):
        #: list of MongoKit.Document which will be automated registed at
        #: connection
        self.registered_documents = []
        
        if app is not None:
            self.app = app
            self.init_app(self.app)
        else:
            self.app = None

    def init_app(self, app):
        app.config.setdefault('MONGODB_HOST', '127.0.0.1')
        app.config.setdefault('MONGODB_PORT', 27017)
        app.config.setdefault('MONGODB_DATABASE', 'flask')
        app.config.setdefault('MONGODB_SLAVE_OKAY', False)
        app.config.setdefault('MONGODB_USERNAME', None)
        app.config.setdefault('MONGODB_PASSWORD', None)

        app.before_request(self.before_request)
        if hasattr(app, 'teardown_request'):
            app.teardown_request(self.teardown_request)
        else:
            app.after_request(self.teardown_request)
    
        # register extension with app
        app.extensions = getattr(app, 'extensions', {})
        app.extensions['mongokit'] = self
        
        self.app = app
    
    def register(self, documents):
        for document in documents:
            if document not in self.registered_documents:
                self.registered_documents.append(document)
        return self.registered_documents
    
    def connect(self):
        ctx = _request_ctx_stack.top
        ctx.mongokit_connection = Connection(
            host=self.app.config.get('MONGODB_HOST'),
            port=self.app.config.get('MONGODB_PORT'),
            slave_okay=self.app.config.get('MONGODB_SLAVE_OKAY')
        )
        ctx.mongokit_connection.register(self.registered_documents)
        ctx.mongokit_db = Database(ctx.mongokit_connection,     
                                   self.app.config.get('MONGODB_DATABASE'))
        if self.app.config.get('MONGODB_USERNAME') is not None:
            ctx.mongokit_db.authenticate(
                self.app.config.get('MONGODB_USERNAME'),
                self.app.config.get('MONGODB_PASSWORD')
            )
        
    def disconnect(self):
        ctx = _request_ctx_stack.top
        ctx.mongokit_connection.disconnect()
        
    def before_request(self):
        return self.connect()
        
    def teardown_request(self, request):
        self.disconnect()
        return request

    def __getattr__(self, *args, **kwargs):
        ctx = _request_ctx_stack.top
        return ctx.mongokit_db.__getattr__(*args, **kwargs)
{'fraction_non_alphanumeric': 0.10345423143350604, 'fraction_numerical': 0.005526770293609672, 'mean_word_length': 2.3319907940161104, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, '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': '6773650', 'n_tokens_mistral': 1791, 'n_tokens_neox': 1725, 'n_words': 326}