summaryrefslogtreecommitdiff
path: root/portato/db/__init__.py
diff options
context:
space:
mode:
authorRené 'Necoro' Neumann <necoro@necoro.net>2010-03-16 02:55:26 +0100
committerRené 'Necoro' Neumann <necoro@necoro.net>2010-03-16 03:02:57 +0100
commit0ed08eba908d3b0ca8fcc282c387c03f95250147 (patch)
tree9c4f09749f59a181de77e6e580e76a099aa33e1e /portato/db/__init__.py
parentdde6c85455fdcfd933c18c65a2c038c14093f525 (diff)
downloadportato-0ed08eba908d3b0ca8fcc282c387c03f95250147.tar.gz
portato-0ed08eba908d3b0ca8fcc282c387c03f95250147.tar.bz2
portato-0ed08eba908d3b0ca8fcc282c387c03f95250147.zip
Rewrote the database initialization to be a class instead of a function
Diffstat (limited to 'portato/db/__init__.py')
-rw-r--r--portato/db/__init__.py94
1 files changed, 47 insertions, 47 deletions
diff --git a/portato/db/__init__.py b/portato/db/__init__.py
index 21f72ce..850a84e 100644
--- a/portato/db/__init__.py
+++ b/portato/db/__init__.py
@@ -12,66 +12,66 @@
from __future__ import absolute_import
+from . import database as db
+from .exceptions import UnknownDatabaseTypeError, DatabaseInstantiationError
from ..session import Session, SectionDict
from ..helper import debug, warning, error
-class UnknownDatabaseTypeError (Exception):
- pass
-
-_SESSION = None
-_TYPE = None
-_DEFAULT = "dict"
-_DATABASE = None
-
types = {
"sql": (_("SQLite"), _("Uses an SQLite-database to store package information.\nMay take longer to generate at the first time, but has advantages if portato is re-started with an unchanged portage tree. Additionally it allows to use fast SQL expressions for fetching the data.")),
"dict": (_("Hashmap"), _("Uses an in-memory hashmap to store package information.\nHas been used since at least version 0.3.3, but all information has to be regenerated on each startup.")),
"eixsql" : (_("eix + SQLite"), _("Similar to SQLite, but now uses the eix database to get the package information.\nThis should be much faster on startup, but requires that your eix database is always up-to-date.\nAdditionally, this is the only database allowing searching in descriptions."))
}
-def Database(type = None):
- global _SESSION, _TYPE, _DATABASE
+class Database(db.Database):
+ DEFAULT = "dict"
- if type is None:
- if _DATABASE is None:
- warning("No database type specified! Falling back to default.")
- return Database(_DEFAULT)
- else:
- return _DATABASE
+ def __new__ (cls, type = None):
+ if not '_the_instance' in cls.__dict__:
+ dbcls = cls._generate(type)
+ cls._the_instance = dbcls(cls._get_session())
+ elif type is not None:
+ raise DatabaseInstantiationError("Database instantiation called with 'type' argument multiple times.")
+ return cls._the_instance
- if _SESSION is None:
- _SESSION = Session("db.cfg", name = "DB")
- _SESSION.load()
+ @classmethod
+ def _generate(cls, type):
- _TYPE = type
+ if type is None:
+ warning("No database type specified! Falling back to default.")
+ type = cls.DEFAULT
+
+ cls.DB_TYPE = type
- if type == "sql":
- debug("Using SQLDatabase")
- try:
- from .sql import SQLDatabase
- except ImportError:
- warning(_("Cannot load %s."), "SQLDatabase")
- _DATABASE = Database("dict")
- else:
- _DATABASE = SQLDatabase(SectionDict(_SESSION, type))
+ if type == "sql":
+ debug("Using SQLDatabase")
+ try:
+ from .sql import SQLDatabase
+ except ImportError:
+ warning(_("Cannot load %s."), "SQLDatabase")
+ return cls._generate("dict")
+ else:
+ return SQLDatabase
- elif type == "dict":
- debug("Using HashDatabase")
- from .hash import HashDatabase
- _DATABASE = HashDatabase(SectionDict(_SESSION, type))
-
- elif type == "eixsql":
- debug("Using EixSQLDatabase")
- try:
- from .eix_sql import EixSQLDatabase
- except ImportError:
- warning(_("Cannot load %s."), "EixSQLDatabase.")
- _DATABASE = Database("sql")
- else:
- _DATABASE = EixSQLDatabase(SectionDict(_SESSION, type))
+ elif type == "dict":
+ debug("Using HashDatabase")
+ from .hash import HashDatabase
+ return HashDatabase
+
+ elif type == "eixsql":
+ debug("Using EixSQLDatabase")
+ try:
+ from .eix_sql import EixSQLDatabase
+ except ImportError:
+ warning(_("Cannot load %s."), "EixSQLDatabase.")
+ return cls._generate("sql")
+ else:
+ return EixSQLDatabase
- else:
- error(_("Unknown database type: %s"), type)
- raise UnknownDatabaseTypeError, type
+ else:
+ error(_("Unknown database type: %s"), type)
+ raise UnknownDatabaseTypeError, type
- return _DATABASE
+ @classmethod
+ def _get_session(cls):
+ return SectionDict(Session("db.cfg", name = "DB"), cls.DB_TYPE)