diff options
author | 2015-10-16 14:19:27 -0400 | |
---|---|---|
committer | 2015-10-16 14:19:31 -0400 | |
commit | d1c17e2c30f47ef5ac8450f269824976605b58a0 (patch) | |
tree | d7bc39dedd5282148ba7d89575d3f420a4cb1c05 | |
parent | setup.py: Makes sqlite database backend option (diff) | |
download | layman-d1c17e2c30f47ef5ac8450f269824976605b58a0.tar.gz layman-d1c17e2c30f47ef5ac8450f269824976605b58a0.tar.bz2 layman-d1c17e2c30f47ef5ac8450f269824976605b58a0.zip |
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.
-rw-r--r-- | layman/dbbase.py | 43 |
1 files 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) |