def _connect_db(self):
        """ Open database connection
        """

        # Get database configuration
        db_args = {}
        db_args['host'] = self._cfg.get('nipapd', 'db_host')
        db_args['database'] = self._cfg.get('nipapd', 'db_name')
        db_args['user'] = self._cfg.get('nipapd', 'db_user')
        db_args['password'] = self._cfg.get('nipapd', 'db_pass')
        db_args['sslmode'] = self._cfg.get('nipapd', 'db_sslmode')
        db_args['port'] = self._cfg.get('nipapd', 'db_port')
        # delete keys that are None, for example if we want to connect over a
        # UNIX socket, the 'host' argument should not be passed into the DSN
        if db_args['host'] is not None and db_args['host'] == '':
            db_args['host'] = None
        for key in db_args.copy():
            if db_args[key] is None:
                del(db_args[key])

        # Create database connection
        while True:
            try:
                self._con_pg = psycopg2.connect(**db_args)
                self._con_pg.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
                self._curs_pg = self._con_pg.cursor(cursor_factory=psycopg2.extras.DictCursor)
                self._register_inet()
                psycopg2.extras.register_hstore(self._con_pg, globally=True, unicode=True)
            except psycopg2.Error as exc:
                if re.search("database.*does not exist", unicode(exc)):
                    raise NipapDatabaseNonExistentError("Database '%s' does not exist" % db_args['database'])
                # no hstore extension, assume empty db (it wouldn't work
                # otherwise) and do auto upgrade?
                if re.search("hstore type not found in the database", unicode(exc)):
                    # automatically install if auto-install is enabled
                    if self._auto_install_db:
                        self._db_install(db_args['database'])
                        continue
                    raise NipapDatabaseMissingExtensionError("hstore extension not found in the database")

                self._logger.error("pgsql: %s" % exc)
                raise NipapError("Backend unable to connect to database")
            except psycopg2.Warning as warn:
                self._logger.warning('pgsql: %s' % warn)

            # check db version
            try:
                current_db_version = self._get_db_version()
            except NipapDatabaseNoVersionError as exc:
                # if there's no db schema version we assume the database is
                # empty...
                if self._auto_install_db:
                    # automatically install schema?
                    self._db_install(db_args['database'])
                    continue
                raise exc
            except NipapError as exc:
                self._logger.error(unicode(exc))
                raise exc

            if current_db_version != nipap.__db_version__:
                if self._auto_upgrade_db:
                    self._db_upgrade(db_args['database'])
                    continue
                raise NipapDatabaseWrongVersionError("NIPAP PostgreSQL database is outdated. Schema version %s is required to run but you are using %s" % (nipap.__db_version__, current_db_version))

            # if we reach this we should be fine and done
            break
def _connect_db ( self ) : db_args = { } db_args [ 'host' ] = self . _cfg . get ( 'nipapd' , 'db_host' ) db_args [ 'database' ] = self . _cfg . get ( 'nipapd' , 'db_name' ) db_args [ 'user' ] = self . _cfg . get ( 'nipapd' , 'db_user' ) db_args [ 'password' ] = self . _cfg . get ( 'nipapd' , 'db_pass' ) db_args [ 'sslmode' ] = self . _cfg . get ( 'nipapd' , 'db_sslmode' ) db_args [ 'port' ] = self . _cfg . get ( 'nipapd' , 'db_port' ) if db_args [ 'host' ] is not None and db_args [ 'host' ] == '' : db_args [ 'host' ] = None for key in db_args . copy ( ) : if db_args [ key ] is None : del ( db_args [ key ] ) while True : try : self . _con_pg = psycopg2 . connect ( ** db_args ) self . _con_pg . set_isolation_level ( psycopg2 . extensions . ISOLATION_LEVEL_AUTOCOMMIT ) self . _curs_pg = self . _con_pg . cursor ( cursor_factory = psycopg2 . extras . DictCursor ) self . _register_inet ( ) psycopg2 . extras . register_hstore ( self . _con_pg , globally = True , unicode = True ) except psycopg2 . Error as exc : if re . search ( "database.*does not exist" , unicode ( exc ) ) : raise NipapDatabaseNonExistentError ( "Database '%s' does not exist" % db_args [ 'database' ] ) if re . search ( "hstore type not found in the database" , unicode ( exc ) ) : if self . _auto_install_db : self . _db_install ( db_args [ 'database' ] ) continue raise NipapDatabaseMissingExtensionError ( "hstore extension not found in the database" ) self . _logger . error ( "pgsql: %s" % exc ) raise NipapError ( "Backend unable to connect to database" ) except psycopg2 . Warning as warn : self . _logger . warning ( 'pgsql: %s' % warn ) try : current_db_version = self . _get_db_version ( ) except NipapDatabaseNoVersionError as exc : if self . _auto_install_db : self . _db_install ( db_args [ 'database' ] ) continue raise exc except NipapError as exc : self . _logger . error ( unicode ( exc ) ) raise exc if current_db_version != nipap . __db_version__ : if self . _auto_upgrade_db : self . _db_upgrade ( db_args [ 'database' ] ) continue raise NipapDatabaseWrongVersionError ( "NIPAP PostgreSQL database is outdated. Schema version %s is required to run but you are using %s" % ( nipap . __db_version__ , current_db_version ) ) break
Open database connection