<s> from functools import wraps <EOL> import os <EOL> import pkgutil <EOL> from threading import local <EOL> import warnings <EOL> from django . conf import settings <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . utils . functional import cached_property <EOL> from django . utils . importlib import import_module <EOL> from django . utils . module_loading import import_by_path <EOL> from django . utils . _os import upath <EOL> from django . utils import six <EOL> DEFAULT_DB_ALIAS = '<STR_LIT:default>' <EOL> class Error ( Exception if six . PY3 else StandardError ) : <EOL> pass <EOL> class InterfaceError ( Error ) : <EOL> pass <EOL> class DatabaseError ( Error ) : <EOL> pass <EOL> class DataError ( DatabaseError ) : <EOL> pass <EOL> class OperationalError ( DatabaseError ) : <EOL> pass <EOL> class IntegrityError ( DatabaseError ) : <EOL> pass <EOL> class InternalError ( DatabaseError ) : <EOL> pass <EOL> class ProgrammingError ( DatabaseError ) : <EOL> pass <EOL> class NotSupportedError ( DatabaseError ) : <EOL> pass <EOL> class DatabaseErrorWrapper ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , wrapper ) : <EOL> """<STR_LIT>""" <EOL> self . wrapper = wrapper <EOL> def __enter__ ( self ) : <EOL> pass <EOL> def __exit__ ( self , exc_type , exc_value , traceback ) : <EOL> if exc_type is None : <EOL> return <EOL> for dj_exc_type in ( <EOL> DataError , <EOL> OperationalError , <EOL> IntegrityError , <EOL> InternalError , <EOL> ProgrammingError , <EOL> NotSupportedError , <EOL> DatabaseError , <EOL> InterfaceError , <EOL> Error , <EOL> ) : <EOL> db_exc_type = getattr ( self . wrapper . Database , dj_exc_type . __name__ ) <EOL> if issubclass ( exc_type , db_exc_type ) : <EOL> try : <EOL> args = tuple ( exc_value . args ) <EOL> except AttributeError : <EOL> args = ( exc_value , ) <EOL> dj_exc_value = dj_exc_type ( * args ) <EOL> dj_exc_value . __cause__ = exc_value <EOL> if dj_exc_type not in ( DataError , IntegrityError ) : <EOL> self . wrapper . errors_occurred = True <EOL> six . reraise ( dj_exc_type , dj_exc_value , traceback ) <EOL> def __call__ ( self , func ) : <EOL> def inner ( * args , ** kwargs ) : <EOL> with self : <EOL> return func ( * args , ** kwargs ) <EOL> return inner <EOL> def load_backend ( backend_name ) : <EOL> try : <EOL> return import_module ( '<STR_LIT>' % backend_name ) <EOL> except ImportError as e_user : <EOL> backend_dir = os . path . join ( os . path . dirname ( upath ( __file__ ) ) , '<STR_LIT>' ) <EOL> try : <EOL> builtin_backends = [ <EOL> name for _ , name , ispkg in pkgutil . iter_modules ( [ backend_dir ] ) <EOL> if ispkg and name != '<STR_LIT>' ] <EOL> except EnvironmentError : <EOL> builtin_backends = [ ] <EOL> if backend_name not in [ '<STR_LIT>' % b for b in <EOL> builtin_backends ] : <EOL> backend_reprs = map ( repr , sorted ( builtin_backends ) ) <EOL> error_msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( backend_name , "<STR_LIT:U+002CU+0020>" . join ( backend_reprs ) , e_user ) ) <EOL> raise ImproperlyConfigured ( error_msg ) <EOL> else : <EOL> raise <EOL> class ConnectionDoesNotExist ( Exception ) : <EOL> pass <EOL> class ConnectionHandler ( object ) : <EOL> def __init__ ( self , databases = None ) : <EOL> """<STR_LIT>""" <EOL> self . _databases = databases <EOL> self . _connections = local ( ) <EOL> @ cached_property <EOL> def databases ( self ) : <EOL> if self . _databases is None : <EOL> self . _databases = settings . DATABASES <EOL> if self . _databases == { } : <EOL> self . _databases = { <EOL> DEFAULT_DB_ALIAS : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> } <EOL> if DEFAULT_DB_ALIAS not in self . _databases : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" % DEFAULT_DB_ALIAS ) <EOL> return self . _databases <EOL> def ensure_defaults ( self , alias ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> conn = self . databases [ alias ] <EOL> except KeyError : <EOL> raise ConnectionDoesNotExist ( "<STR_LIT>" % alias ) <EOL> conn . setdefault ( '<STR_LIT>' , False ) <EOL> if settings . TRANSACTIONS_MANAGED : <EOL> warnings . warn ( <EOL> "<STR_LIT>" , <EOL> PendingDeprecationWarning , stacklevel = <NUM_LIT:2> ) <EOL> conn . setdefault ( '<STR_LIT>' , False ) <EOL> conn . setdefault ( '<STR_LIT>' , True ) <EOL> conn . setdefault ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if conn [ '<STR_LIT>' ] == '<STR_LIT>' or not conn [ '<STR_LIT>' ] : <EOL> conn [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> conn . setdefault ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> conn . setdefault ( '<STR_LIT>' , { } ) <EOL> conn . setdefault ( '<STR_LIT>' , '<STR_LIT>' if settings . USE_TZ else settings . TIME_ZONE ) <EOL> for setting in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> conn . setdefault ( setting , '<STR_LIT>' ) <EOL> for setting in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> conn . setdefault ( setting , None ) <EOL> def __getitem__ ( self , alias ) : <EOL> if hasattr ( self . _connections , alias ) : <EOL> return getattr ( self . _connections , alias ) <EOL> self . ensure_defaults ( alias ) <EOL> db = self . databases [ alias ] <EOL> backend = load_backend ( db [ '<STR_LIT>' ] ) <EOL> conn = backend . DatabaseWrapper ( db , alias ) <EOL> setattr ( self . _connections , alias , conn ) <EOL> return conn <EOL> def __setitem__ ( self , key , value ) : <EOL> setattr ( self . _connections , key , value ) <EOL> def __delitem__ ( self , key ) : <EOL> delattr ( self . _connections , key ) <EOL> def __iter__ ( self ) : <EOL> return iter ( self . databases ) <EOL> def all ( self ) : <EOL> return [ self [ alias ] for alias in self ] <EOL> class ConnectionRouter ( object ) : <EOL> def __init__ ( self , routers = None ) : <EOL> """<STR_LIT>""" <EOL> self . _routers = routers <EOL> @ cached_property <EOL> def routers ( self ) : <EOL> if self . _routers is None : <EOL> self . _routers = settings . DATABASE_ROUTERS <EOL> routers = [ ] <EOL> for r in self . _routers : <EOL> if isinstance ( r , six . string_types ) : <EOL> router = import_by_path ( r ) ( ) <EOL> else : <EOL> router = r <EOL> routers . append ( router ) <EOL> return routers <EOL> def _router_func ( action ) : <EOL> def _route_db ( self , model , ** hints ) : <EOL> chosen_db = None <EOL> for router in self . routers : <EOL> try : <EOL> method = getattr ( router , action ) <EOL> except AttributeError : <EOL> pass <EOL> else : <EOL> chosen_db = method ( model , ** hints ) <EOL> if chosen_db : <EOL> return chosen_db <EOL> try : <EOL> return hints [ '<STR_LIT>' ] . _state . db or DEFAULT_DB_ALIAS <EOL> except KeyError : <EOL> return DEFAULT_DB_ALIAS <EOL> return _route_db <EOL> db_for_read = _router_func ( '<STR_LIT>' ) <EOL> db_for_write = _router_func ( '<STR_LIT>' ) <EOL> def allow_relation ( self , obj1 , obj2 , ** hints ) : <EOL> for router in self . routers : <EOL> try : <EOL> method = router . allow_relation <EOL> except AttributeError : <EOL> pass <EOL> else : <EOL> allow = method ( obj1 , obj2 , ** hints ) <EOL> if allow is not None : <EOL> return allow <EOL> return obj1 . _state . db == obj2 . _state . db <EOL> def allow_syncdb ( self , db , model ) : <EOL> for router in self . routers : <EOL> try : <EOL> method = router . allow_syncdb <EOL> except AttributeError : <EOL> pass <EOL> else : <EOL> allow = method ( db , model ) <EOL> if allow is not None : <EOL> return allow <EOL> return True </s>
