<s> from __future__ import ( absolute_import , division , print_function , <EOL> unicode_literals ) <EOL> from contextlib import contextmanager <EOL> from redis import StrictRedis <EOL> from . compat . connections import patch_connection <EOL> from . local import LocalStack , release_local <EOL> class NoRedisConnectionException ( Exception ) : <EOL> pass <EOL> @ contextmanager <EOL> def Connection ( connection = None ) : <EOL> if connection is None : <EOL> connection = StrictRedis ( ) <EOL> push_connection ( connection ) <EOL> try : <EOL> yield <EOL> finally : <EOL> popped = pop_connection ( ) <EOL> assert popped == connection , '<STR_LIT>' '<STR_LIT>' <EOL> def push_connection ( redis ) : <EOL> """<STR_LIT>""" <EOL> _connection_stack . push ( patch_connection ( redis ) ) <EOL> def pop_connection ( ) : <EOL> """<STR_LIT>""" <EOL> return _connection_stack . pop ( ) <EOL> def use_connection ( redis = None ) : <EOL> """<STR_LIT>""" <EOL> assert len ( _connection_stack ) <= <NUM_LIT:1> , '<STR_LIT>' <EOL> release_local ( _connection_stack ) <EOL> if redis is None : <EOL> redis = StrictRedis ( ) <EOL> push_connection ( redis ) <EOL> def get_current_connection ( ) : <EOL> """<STR_LIT>""" <EOL> return _connection_stack . top <EOL> def resolve_connection ( connection = None ) : <EOL> """<STR_LIT>""" <EOL> if connection is not None : <EOL> return patch_connection ( connection ) <EOL> connection = get_current_connection ( ) <EOL> if connection is None : <EOL> raise NoRedisConnectionException ( '<STR_LIT>' ) <EOL> return connection <EOL> _connection_stack = LocalStack ( ) <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] </s>
