summaryrefslogtreecommitdiff
path: root/portato
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
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')
-rw-r--r--portato/db/__init__.py94
-rw-r--r--portato/db/database.py2
-rw-r--r--portato/db/exceptions.py24
-rw-r--r--portato/db/sql.py2
-rw-r--r--portato/gui/exception_handling.py4
5 files changed, 76 insertions, 50 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)
diff --git a/portato/db/database.py b/portato/db/database.py
index 6e92d79..c679d06 100644
--- a/portato/db/database.py
+++ b/portato/db/database.py
@@ -16,6 +16,8 @@ from threading import RLock
from functools import wraps
from ..helper import warning
+from .exceptions import UnsupportedSearchTypeError
+
class UnsupportedSearchTypeError(Exception):
pass
diff --git a/portato/db/exceptions.py b/portato/db/exceptions.py
new file mode 100644
index 0000000..8a6e424
--- /dev/null
+++ b/portato/db/exceptions.py
@@ -0,0 +1,24 @@
+# -*- coding: utf-8 -*-
+#
+# File: portato/db/exceptions.py
+# This file is part of the Portato-Project, a graphical portage-frontend.
+#
+# Copyright (C) 2006-2010 René 'Necoro' Neumann
+# This is free software. You may redistribute copies of it under the terms of
+# the GNU General Public License version 2.
+# There is NO WARRANTY, to the extent permitted by law.
+#
+# Written by René 'Necoro' Neumann <necoro@necoro.net>
+
+class DatabaseError (Exception):
+ pass
+
+class UnknownDatabaseTypeError (DatabaseError):
+ pass
+
+class DatabaseInstantiationError (DatabaseError):
+ pass
+
+class UnsupportedSearchTypeError(DatabaseError):
+ pass
+
diff --git a/portato/db/sql.py b/portato/db/sql.py
index ea4ce91..f5dc257 100644
--- a/portato/db/sql.py
+++ b/portato/db/sql.py
@@ -120,7 +120,7 @@ class SQLDatabase (Database):
os.remove(dbpath)
db_existed = False
- self.session["pickle"] = True # no need for a certain value
+ self.session["pickle"] = "1" # no need for a certain value
if db_existed:
debug("portdirs.db already existant")
diff --git a/portato/gui/exception_handling.py b/portato/gui/exception_handling.py
index dbafa7e..d9b133c 100644
--- a/portato/gui/exception_handling.py
+++ b/portato/gui/exception_handling.py
@@ -99,7 +99,7 @@ def convert (version):
def get_version_infos():
from ..constants import VERSION, REVISION
from ..backend import system
- from ..db import _TYPE as db_type
+ from ..db import Database
if REVISION:
VERSION = "%s (git: %s)" % (VERSION, REVISION)
@@ -109,7 +109,7 @@ def get_version_infos():
"System: %s" % " ".join(get_runsystem()),
"Python version: %s" % sys.version,
"Used backend: %s" % system.get_version(),
- "Used database type: %s" % db_type,
+ "Used database type: %s" % Database.DB_TYPE,
"pygtk: %s (using GTK+: %s)" % (convert(gtk.pygtk_version), convert(gtk.gtk_version)),
"pygobject: %s (using GLib: %s)" % (convert(gobject.pygobject_version), convert(gobject.glib_version))))