<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> import bson <EOL> from mongokit import Connection , Database , Collection , Document <EOL> from pymongo . errors import OperationFailure <EOL> from werkzeug . routing import BaseConverter <EOL> from flask import abort , _request_ctx_stack <EOL> try : <EOL> from flask import _app_ctx_stack <EOL> ctx_stack = _app_ctx_stack <EOL> except ImportError : <EOL> ctx_stack = _request_ctx_stack <EOL> class AuthenticationIncorrect ( Exception ) : <EOL> pass <EOL> class BSONObjectIdConverter ( BaseConverter ) : <EOL> """<STR_LIT>""" <EOL> def to_python ( self , value ) : <EOL> try : <EOL> return bson . ObjectId ( value ) <EOL> except bson . errors . InvalidId : <EOL> raise abort ( <NUM_LIT> ) <EOL> def to_url ( self , value ) : <EOL> return str ( value ) <EOL> class Document ( Document ) : <EOL> def get_or_404 ( self , id ) : <EOL> """<STR_LIT>""" <EOL> doc = self . get_from_id ( id ) <EOL> if doc is None : <EOL> abort ( <NUM_LIT> ) <EOL> else : <EOL> return doc <EOL> def find_one_or_404 ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> doc = self . find_one ( * args , ** kwargs ) <EOL> if doc is None : <EOL> abort ( <NUM_LIT> ) <EOL> else : <EOL> return doc <EOL> class MongoKit ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , app = None ) : <EOL> self . registered_documents = [ ] <EOL> if app is not None : <EOL> self . app = app <EOL> self . init_app ( self . app ) <EOL> else : <EOL> self . app = None <EOL> def init_app ( self , app ) : <EOL> """<STR_LIT>""" <EOL> app . config . setdefault ( '<STR_LIT>' , '<STR_LIT:127.0.0.1>' ) <EOL> app . config . setdefault ( '<STR_LIT>' , <NUM_LIT> ) <EOL> app . config . setdefault ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> app . config . setdefault ( '<STR_LIT>' , False ) <EOL> app . config . setdefault ( '<STR_LIT>' , None ) <EOL> app . config . setdefault ( '<STR_LIT>' , None ) <EOL> if hasattr ( app , '<STR_LIT>' ) : <EOL> app . teardown_appcontext ( self . _teardown_request ) <EOL> elif hasattr ( app , '<STR_LIT>' ) : <EOL> app . teardown_request ( self . _teardown_request ) <EOL> else : <EOL> app . after_request ( self . _teardown_request ) <EOL> app . extensions = getattr ( app , '<STR_LIT>' , { } ) <EOL> app . extensions [ '<STR_LIT>' ] = self <EOL> app . url_map . converters [ '<STR_LIT>' ] = BSONObjectIdConverter <EOL> self . app = app <EOL> def register ( self , documents ) : <EOL> """<STR_LIT>""" <EOL> decorator = None <EOL> if not isinstance ( documents , ( list , tuple , set , frozenset ) ) : <EOL> decorator = documents <EOL> documents = [ documents ] <EOL> for document in documents : <EOL> if document not in self . registered_documents : <EOL> self . registered_documents . append ( document ) <EOL> if decorator is None : <EOL> return self . registered_documents <EOL> else : <EOL> return decorator <EOL> def connect ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . app is None : <EOL> raise RuntimeError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> ctx = ctx_stack . top <EOL> mongokit_connection = getattr ( ctx , '<STR_LIT>' , None ) <EOL> if mongokit_connection is None : <EOL> ctx . mongokit_connection = Connection ( <EOL> host = ctx . app . config . get ( '<STR_LIT>' ) , <EOL> port = ctx . app . config . get ( '<STR_LIT>' ) , <EOL> slave_okay = ctx . app . config . get ( '<STR_LIT>' ) , <EOL> tz_aware = ctx . app . config . get ( '<STR_LIT>' , False ) <EOL> ) <EOL> ctx . mongokit_connection . register ( self . registered_documents ) <EOL> mongokit_database = getattr ( ctx , '<STR_LIT>' , None ) <EOL> if mongokit_database is None : <EOL> ctx . mongokit_database = Database ( <EOL> ctx . mongokit_connection , <EOL> ctx . app . config . get ( '<STR_LIT>' ) <EOL> ) <EOL> if ctx . app . config . get ( '<STR_LIT>' ) is not None : <EOL> try : <EOL> auth_success = ctx . mongokit_database . authenticate ( <EOL> ctx . app . config . get ( '<STR_LIT>' ) , <EOL> ctx . app . config . get ( '<STR_LIT>' ) <EOL> ) <EOL> except OperationFailure : <EOL> auth_success = False <EOL> if not auth_success : <EOL> raise AuthenticationIncorrect ( '<STR_LIT>' ) <EOL> @ property <EOL> def connected ( self ) : <EOL> """<STR_LIT>""" <EOL> ctx = ctx_stack . top <EOL> return getattr ( ctx , '<STR_LIT>' , None ) is not None <EOL> def disconnect ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . connected : <EOL> ctx = ctx_stack . top <EOL> ctx . mongokit_connection . disconnect ( ) <EOL> del ctx . mongokit_connection <EOL> del ctx . mongokit_database <EOL> def _teardown_request ( self , response ) : <EOL> self . disconnect ( ) <EOL> return response <EOL> def __getattr__ ( self , name , ** kwargs ) : <EOL> if not self . connected : <EOL> self . connect ( ) <EOL> mongokit_database = getattr ( ctx_stack . top , "<STR_LIT>" ) <EOL> return getattr ( mongokit_database , name ) <EOL> def __getitem__ ( self , name ) : <EOL> if not self . connected : <EOL> self . connect ( ) <EOL> mongokit_database = getattr ( ctx_stack . top , "<STR_LIT>" ) <EOL> return mongokit_database [ name ] </s>
