<s> import re <EOL> from mongoengine import connect <EOL> from marrow . package . loader import load <EOL> from marrow . util . compat import native <EOL> from web . core . compat import items <EOL> log = __import__ ( '<STR_LIT>' ) . getLogger ( __name__ ) <EOL> _safe_uri_replace = re . compile ( r'<STR_LIT>' ) <EOL> class MongoEngineExtension : <EOL> __slots__ = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> provides = [ '<STR_LIT>' ] <EOL> def __init__ ( self , uri , ** config ) : <EOL> self . uri = uri <EOL> prefix = config . pop ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> log . info ( "<STR_LIT>" , _safe_uri_replace . sub ( r'<STR_LIT>' , uri ) ) <EOL> connection = self . connection = dict ( tz_aware = True ) <EOL> scheme , parts = uri . split ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> parts , self . db = parts . split ( '<STR_LIT:/>' , <NUM_LIT:1> ) <EOL> auth , host = parts . split ( '<STR_LIT:@>' , <NUM_LIT:1> ) if '<STR_LIT:@>' in parts else ( None , parts ) <EOL> if scheme != '<STR_LIT>' : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> connection [ '<STR_LIT:host>' ] , connection [ '<STR_LIT:port>' ] = host . split ( '<STR_LIT::>' ) if '<STR_LIT::>' in host else ( host , '<STR_LIT>' ) <EOL> connection [ '<STR_LIT:port>' ] = int ( connection [ '<STR_LIT:port>' ] ) <EOL> if auth : <EOL> connection [ '<STR_LIT:username>' ] , _ , connection [ '<STR_LIT:password>' ] = auth . partition ( '<STR_LIT::>' ) <EOL> for k , v in items ( config ) : <EOL> pfx , _ , k = k . rpartition ( '<STR_LIT:.>' ) <EOL> if pfx != prefix or k in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : continue <EOL> connection [ k ] = int ( v ) if v . isdigit ( ) else v <EOL> self . cb = config . get ( '<STR_LIT>' , None ) <EOL> def start ( self , context ) : <EOL> db , connection = self . db , self . connection <EOL> csafe = connection . copy ( ) <EOL> csafe . pop ( '<STR_LIT:password>' , None ) <EOL> log . debug ( "<STR_LIT>" , db , csafe ) <EOL> context . mongoengine = connect ( db , ** connection ) <EOL> cb = self . cb <EOL> if cb is not None : <EOL> cb = load ( cb ) if isinstance ( cb , native ) else cb <EOL> if hasattr ( cb , '<STR_LIT>' ) : <EOL> cb ( ) </s>
