diff options
Diffstat (limited to 'portato')
-rw-r--r-- | portato/constants.py | 3 | ||||
-rw-r--r-- | portato/db/__init__.py | 25 |
2 files changed, 19 insertions, 9 deletions
diff --git a/portato/constants.py b/portato/constants.py index 79ad0f2..3d7217f 100644 --- a/portato/constants.py +++ b/portato/constants.py @@ -22,8 +22,6 @@ These should be set during the installation. @type HOME: string @var SU_COMMAND: command to execute to "su" @type SU_COMMAND: string -@var USE_SQL: whether to use the sqlite db -@type USE_SQL: boolean @var CONFIG_DIR: The configuration directory. @type CONFIG_DIR: string @@ -58,7 +56,6 @@ APP = "portato" VERSION = "9999" HOME = os.environ["HOME"] SU_COMMAND = "gksu -D 'Portato'" -USE_SQL = True # config CONFIG_DIR = "/etc/portato/" diff --git a/portato/db/__init__.py b/portato/db/__init__.py index 40bf9ee..d77fc0d 100644 --- a/portato/db/__init__.py +++ b/portato/db/__init__.py @@ -13,22 +13,35 @@ from __future__ import absolute_import from ..session import Session, SectionDict -from ..constants import USE_SQL from ..helper import debug _SESSION = None +_TYPE = None + +def _set_type(t): + global _TYPE + _TYPE = t def Database(): - global _SESSION + global _SESSION, _TYPE if _SESSION is None: _SESSION = Session("db.cfg", name = "DB") + _SESSION.add_handler((["type"], _set_type, lambda: _TYPE), default = ["sql"]) + _SESSION.load() - if USE_SQL: + if _TYPE == "sql": debug("Using SQLDatabase") - from .sql import SQLDatabase - return SQLDatabase(SectionDict(_SESSION, "SQL")) - else: + try: + from .sql import SQLDatabase + except ImportError: + debug("Cannot load SQLDatabase.") + _TYPE = "dict" + return Database() + else: + return SQLDatabase(SectionDict(_SESSION, "SQL")) + + elif _TYPE == "dict": debug("Using DictDatabase") from .dict import DictDatabase return DictDatabase(SectionDict(_SESSION, "dict")) |