From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) by finch.gentoo.org (Postfix) with ESMTP id AB49013888F for ; Fri, 16 Oct 2015 18:49:17 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 37BC1E07FD; Fri, 16 Oct 2015 18:49:17 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id C7F9CE07FD for ; Fri, 16 Oct 2015 18:49:16 +0000 (UTC) Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id C6C89340893 for ; Fri, 16 Oct 2015 18:49:09 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id 843AA10AA for ; Fri, 16 Oct 2015 18:49:07 +0000 (UTC) From: "Devan Franchini" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Devan Franchini" Message-ID: <1445019571.d1c17e2c30f47ef5ac8450f269824976605b58a0.twitch153@gentoo> Subject: [gentoo-commits] proj/layman:master commit in: layman/ X-VCS-Repository: proj/layman X-VCS-Files: layman/dbbase.py X-VCS-Directories: layman/ X-VCS-Committer: twitch153 X-VCS-Committer-Name: Devan Franchini X-VCS-Revision: d1c17e2c30f47ef5ac8450f269824976605b58a0 X-VCS-Branch: master Date: Fri, 16 Oct 2015 18:49:07 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Archives-Salt: 18c56071-6f5f-4c11-8ccf-24e9cdc42664 X-Archives-Hash: 90449910ab14abd5dd4486f426715aec commit: d1c17e2c30f47ef5ac8450f269824976605b58a0 Author: Devan Franchini gentoo org> AuthorDate: Fri Oct 16 18:19:27 2015 +0000 Commit: Devan Franchini gentoo org> CommitDate: Fri Oct 16 18:19:31 2015 +0000 URL: https://gitweb.gentoo.org/proj/layman.git/commit/?id=d1c17e2c dbbase.py: Creates internal function to get database module controller This internal function centralizes the functionality of getting the database module controller while also adding in checks to gracefully handle invalid module names. layman/dbbase.py | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/layman/dbbase.py b/layman/dbbase.py index dc089d8..ebcd7a4 100644 --- a/layman/dbbase.py +++ b/layman/dbbase.py @@ -36,7 +36,7 @@ import os import os.path import sys -from layman.module import Modules +from layman.module import Modules, InvalidModuleName from layman.overlays.overlay import Overlay @@ -155,6 +155,26 @@ class DbBase(object): raise NotImplementedError(msg) + def _get_dbctl(self, db_type): + ''' + Returns database module controller for class or dies trying. + ''' + try: + db_ctl = self.mod_ctl.get_class(db_type)(self.config, + self.overlays, + self.paths, + self.ignore, + self.ignore_init_read_errors) + except InvalidModuleName: + msg = 'DbBase._get_dbctl() error:\nDatabase module name '\ + '"%(name)s" is invalid or not found.\nPlease set db_type '\ + 'variable to proper value to continue.'\ + % {'name': db_type.replace('_db', '')} + self.output.die(msg) + + return db_ctl + + def add_new(self, xml=None, origin=None, from_dict=None): ''' Reads xml text and dictionary definitions and adds @@ -188,12 +208,7 @@ class DbBase(object): if 'cache' in path and '.xml' in path: db_type = 'xml_db' - db_ctl = self.mod_ctl.get_class(db_type)(self.config, - self.overlays, - self.paths, - self.ignore, - self.ignore_init_read_errors) - + db_ctl = self._get_dbctl(db_type) db_ctl.read_db(path, text=text) @@ -206,12 +221,7 @@ class DbBase(object): if migrate_type: db_type = migrate_type + '_db' - db_ctl = self.mod_ctl.get_class(db_type)(self.config, - self.overlays, - self.paths, - self.ignore, - self.ignore_init_read_errors) - + db_ctl = self._get_dbctl(db_type) db_ctl.write(path, remove=remove) @@ -219,12 +229,7 @@ class DbBase(object): ''' Remove an overlay from the database. ''' - db_ctl = self.mod_ctl.get_class(self.db_type)(self.config, - self.overlays, - self.paths, - self.ignore, - self.ignore_init_read_errors) - + db_ctl = self._get_dbctl(db_type) db_ctl.remove(overlay, path)