def _connect_db(self):
<EOL>db_args = {}<EOL>db_args['<STR_LIT:host>'] = self._cfg.get('<STR_LIT>', '<STR_LIT>')<EOL>db_args['<STR_LIT>'] = self._cfg.get('<STR_LIT>', '<STR_LIT>')<EOL>db_args['<STR_LIT:user>'] = self._cfg.get('<STR_LIT>', '<STR_LIT>')<EOL>db_args['<STR_LIT:password>'] = self._cfg.get('<STR_LIT>', '<STR_LIT>')<EOL>db_args['<STR_LIT>'] = self._cfg.get('<STR_LIT>', '<STR_LIT>')<EOL>db_args['<STR_LIT:port>'] = self._cfg.get('<STR_LIT>', '<STR_LIT>')<EOL>if db_args['<STR_LIT:host>'] is not None and db_args['<STR_LIT:host>'] == '<STR_LIT>':<EOL><INDENT>db_args['<STR_LIT:host>'] = None<EOL><DEDENT>for key in db_args.copy():<EOL><INDENT>if db_args[key] is None:<EOL><INDENT>del(db_args[key])<EOL><DEDENT><DEDENT>while True:<EOL><INDENT>try:<EOL><INDENT>self._con_pg = psycopg2.connect(**db_args)<EOL>self._con_pg.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)<EOL>self._curs_pg = self._con_pg.cursor(cursor_factory=psycopg2.extras.DictCursor)<EOL>self._register_inet()<EOL>psycopg2.extras.register_hstore(self._con_pg, globally=True, unicode=True)<EOL><DEDENT>except psycopg2.Error as exc:<EOL><INDENT>if re.search("<STR_LIT>", unicode(exc)):<EOL><INDENT>raise NipapDatabaseNonExistentError("<STR_LIT>" % db_args['<STR_LIT>'])<EOL><DEDENT>if re.search("<STR_LIT>", unicode(exc)):<EOL><INDENT>if self._auto_install_db:<EOL><INDENT>self._db_install(db_args['<STR_LIT>'])<EOL>continue<EOL><DEDENT>raise NipapDatabaseMissingExtensionError("<STR_LIT>")<EOL><DEDENT>self._logger.error("<STR_LIT>" % exc)<EOL>raise NipapError("<STR_LIT>")<EOL><DEDENT>except psycopg2.Warning as warn:<EOL><INDENT>self._logger.warning('<STR_LIT>' % warn)<EOL><DEDENT>try:<EOL><INDENT>current_db_version = self._get_db_version()<EOL><DEDENT>except NipapDatabaseNoVersionError as exc:<EOL><INDENT>if self._auto_install_db:<EOL><INDENT>self._db_install(db_args['<STR_LIT>'])<EOL>continue<EOL><DEDENT>raise exc<EOL><DEDENT>except NipapError as exc:<EOL><INDENT>self._logger.error(unicode(exc))<EOL>raise exc<EOL><DEDENT>if current_db_version != nipap.__db_version__:<EOL><INDENT>if self._auto_upgrade_db:<EOL><INDENT>self._db_upgrade(db_args['<STR_LIT>'])<EOL>continue<EOL><DEDENT>raise NipapDatabaseWrongVersionError("<STR_LIT>" % (nipap.__db_version__, current_db_version))<EOL><DEDENT>break<EOL><DEDENT>
Open database connection