<s> """<STR_LIT>""" <EOL> import logging <EOL> from functools import partial <EOL> from sqlalchemy import event <EOL> from sqlalchemy . pool import manage , QueuePool <EOL> from psycopg2 import InterfaceError , ProgrammingError , OperationalError <EOL> from django . conf import settings <EOL> from django . db . backends . postgresql_psycopg2 . base import * <EOL> from django . db . backends . postgresql_psycopg2 . base import DatabaseWrapper as Psycopg2DatabaseWrapper <EOL> from django . db . backends . postgresql_psycopg2 . base import CursorWrapper as DjangoCursorWrapper <EOL> from django . db . backends . postgresql_psycopg2 . creation import DatabaseCreation as Psycopg2DatabaseCreation <EOL> class DatabaseOperations ( DatabaseOperations ) : <EOL> def lookup_cast ( self , lookup_type ) : <EOL> if lookup_type in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return "<STR_LIT>" <EOL> else : <EOL> return super ( DatabaseOperations , self ) . lookup_cast ( lookup_type ) <EOL> """<STR_LIT>""" <EOL> POOL_SETTINGS = '<STR_LIT>' <EOL> pool_args = getattr ( settings , POOL_SETTINGS , { } ) <EOL> db_pool = manage ( Database , ** pool_args ) <EOL> log = logging . getLogger ( '<STR_LIT>' ) <EOL> def _log ( message , * args ) : <EOL> log . debug ( message ) <EOL> if settings . DEBUG : <EOL> event . listen ( QueuePool , '<STR_LIT>' , partial ( _log , '<STR_LIT>' ) ) <EOL> event . listen ( QueuePool , '<STR_LIT>' , partial ( _log , '<STR_LIT>' ) ) <EOL> event . listen ( QueuePool , '<STR_LIT>' , partial ( _log , '<STR_LIT>' ) ) <EOL> def is_disconnect ( e , connection , cursor ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( e , OperationalError ) : <EOL> return '<STR_LIT>' in str ( e ) or '<STR_LIT>' in str ( e ) or '<STR_LIT>' in str ( e ) or '<STR_LIT>' in str ( e ) <EOL> elif isinstance ( e , InterfaceError ) : <EOL> return '<STR_LIT>' in str ( e ) or '<STR_LIT>' in str ( e ) <EOL> elif isinstance ( e , ProgrammingError ) : <EOL> return "<STR_LIT>" in str ( e ) <EOL> else : <EOL> return False <EOL> class CursorWrapper ( DjangoCursorWrapper ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , cursor , connection ) : <EOL> self . cursor = cursor <EOL> self . connection = connection <EOL> def execute ( self , query , args = None ) : <EOL> try : <EOL> return self . cursor . execute ( query , args ) <EOL> except Database . IntegrityError , e : <EOL> raise utils . IntegrityError , utils . IntegrityError ( <EOL> * tuple ( e ) ) , sys . exc_info ( ) [ <NUM_LIT:2> ] <EOL> except Database . DatabaseError , e : <EOL> if is_disconnect ( e , self . connection . connection , self . cursor ) : <EOL> log . error ( "<STR_LIT>" ) <EOL> self . connection . invalidate ( ) <EOL> raise utils . DatabaseError , utils . DatabaseError ( <EOL> * tuple ( e ) ) , sys . exc_info ( ) [ <NUM_LIT:2> ] <EOL> def executemany ( self , query , args ) : <EOL> try : <EOL> return self . cursor . executemany ( query , args ) <EOL> except Database . IntegrityError , e : <EOL> raise utils . IntegrityError , utils . IntegrityError ( <EOL> * tuple ( e ) ) , sys . exc_info ( ) [ <NUM_LIT:2> ] <EOL> except Database . DatabaseError , e : <EOL> if is_disconnect ( e , self . connection . connection , self . cursor ) : <EOL> log . error ( "<STR_LIT>" ) <EOL> self . connection . invalidate ( ) <EOL> raise utils . DatabaseError , utils . DatabaseError ( <EOL> * tuple ( e ) ) , sys . exc_info ( ) [ <NUM_LIT:2> ] <EOL> class DatabaseCreation ( Psycopg2DatabaseCreation ) : <EOL> def destroy_test_db ( self , * args , ** kw ) : <EOL> """<STR_LIT>""" <EOL> self . connection . _dispose ( ) <EOL> super ( DatabaseCreation , self ) . destroy_test_db ( * args , ** kw ) <EOL> class DatabaseWrapper ( Psycopg2DatabaseWrapper ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( DatabaseWrapper , self ) . __init__ ( * args , ** kwargs ) <EOL> self . creation = DatabaseCreation ( self ) <EOL> self . operators [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . operators [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . ops = DatabaseOperations ( self ) <EOL> def _cursor ( self ) : <EOL> if self . connection is None or self . connection . is_valid is False : <EOL> self . connection = db_pool . connect ( ** self . _get_conn_params ( ) ) <EOL> self . connection . set_client_encoding ( '<STR_LIT>' ) <EOL> tz = '<STR_LIT>' if settings . USE_TZ else self . settings_dict . get ( <EOL> '<STR_LIT>' ) <EOL> if tz : <EOL> try : <EOL> get_parameter_status = self . connection . get_parameter_status <EOL> except AttributeError : <EOL> conn_tz = None <EOL> else : <EOL> conn_tz = get_parameter_status ( '<STR_LIT>' ) <EOL> if conn_tz != tz : <EOL> self . connection . set_isolation_level ( <EOL> psycopg2 . extensions . ISOLATION_LEVEL_AUTOCOMMIT ) <EOL> self . connection . cursor ( ) . execute ( <EOL> self . ops . set_time_zone_sql ( ) , [ tz ] ) <EOL> self . connection . set_isolation_level ( self . isolation_level ) <EOL> self . _get_pg_version ( ) <EOL> connection_created . send ( sender = self . __class__ , connection = self ) <EOL> cursor = self . connection . cursor ( ) <EOL> cursor . tzinfo_factory = utc_tzinfo_factory if settings . USE_TZ else None <EOL> return CursorWrapper ( cursor , self . connection ) <EOL> def _commit ( self ) : <EOL> if self . connection is not None and self . connection . is_valid : <EOL> return self . connection . commit ( ) <EOL> def _rollback ( self ) : <EOL> if self . connection is not None and self . connection . is_valid : <EOL> return self . connection . rollback ( ) <EOL> def _dispose ( self ) : <EOL> """<STR_LIT>""" <EOL> self . close ( ) <EOL> conn_params = self . _get_conn_params ( ) <EOL> key = db_pool . _serialize ( ** conn_params ) <EOL> try : <EOL> pool = db_pool . pools [ key ] <EOL> except KeyError : <EOL> pass <EOL> else : <EOL> pool . dispose ( ) <EOL> del db_pool . pools [ key ] <EOL> def _get_conn_params ( self ) : <EOL> settings_dict = self . settings_dict <EOL> if not settings_dict [ '<STR_LIT>' ] : <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> raise ImproperlyConfigured ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> conn_params = { <EOL> '<STR_LIT>' : settings_dict [ '<STR_LIT>' ] , <EOL> } <EOL> conn_params . update ( settings_dict [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in conn_params : <EOL> del conn_params [ '<STR_LIT>' ] <EOL> if settings_dict [ '<STR_LIT>' ] : <EOL> conn_params [ '<STR_LIT:user>' ] = settings_dict [ '<STR_LIT>' ] <EOL> if settings_dict [ '<STR_LIT>' ] : <EOL> conn_params [ '<STR_LIT:password>' ] = settings_dict [ '<STR_LIT>' ] <EOL> if settings_dict [ '<STR_LIT>' ] : <EOL> conn_params [ '<STR_LIT:host>' ] = settings_dict [ '<STR_LIT>' ] <EOL> if settings_dict [ '<STR_LIT>' ] : <EOL> conn_params [ '<STR_LIT:port>' ] = settings_dict [ '<STR_LIT>' ] <EOL> return conn_params </s>
