From 816fbaf42407fbbb8466c0d08d64fc11f605e5b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20=27Necoro=27=20Neumann?= Date: Wed, 28 Jan 2009 13:13:56 +0100 Subject: Use the new database class layout --- portato/db/__init__.py | 14 ++++++++++++++ portato/db/database.py | 17 +++++++++++++++++ portato/db/dict.py | 16 +++++----------- portato/db/sql.py | 16 ++++------------ portato/gui/windows/main.py | 3 ++- 5 files changed, 42 insertions(+), 24 deletions(-) diff --git a/portato/db/__init__.py b/portato/db/__init__.py index 5787c56..122940a 100644 --- a/portato/db/__init__.py +++ b/portato/db/__init__.py @@ -9,3 +9,17 @@ # There is NO WARRANTY, to the extent permitted by law. # # Written by René 'Necoro' Neumann + +from __future__ import absolute_import + +from ..constants import USE_SQL +from ..helper import debug + +if USE_SQL: + debug("Using SQLDatabase") + from .sql import SQLDatabase + Database = SQLDatabase +else: + debug("Using DictDatabase") + from .dict import DictDatabase + Database = DictDatabase diff --git a/portato/db/database.py b/portato/db/database.py index 7d8e378..941c3a3 100644 --- a/portato/db/database.py +++ b/portato/db/database.py @@ -12,6 +12,9 @@ from __future__ import absolute_import, with_statement +from threading import RLock +from functools import wraps + class PkgData (object): __slots__ = ("cat", "pkg", "inst") @@ -33,6 +36,20 @@ class Database (object): ALL = _("ALL") + def __init__ (self): + self._lock = RLock() + + @staticmethod + def lock (f): + @wraps(f) + def wrapper (self, *args, **kwargs): + with self._lock: + r = f(self, *args, **kwargs) + + return r + + return wrapper + def populate (self, category = None): """Populates the database. diff --git a/portato/db/dict.py b/portato/db/dict.py index 435cdd9..5c5ca49 100644 --- a/portato/db/dict.py +++ b/portato/db/dict.py @@ -14,7 +14,7 @@ from __future__ import absolute_import, with_statement import re from collections import defaultdict -from functools import wraps +from threading import RLock from ..backend import system from .database import Database, PkgData @@ -22,21 +22,15 @@ from .database import Database, PkgData class DictDatabase (Database): """An internal database which holds a simple dictionary cat -> [package_list].""" + lock = Database.lock + def __init__ (self): """Constructor.""" + Database.__init__(self) + self.__initialize() - self._lock = RLock() self.populate() - def lock (f): - @wraps(f) - def wrapper (self, *args, **kwargs): - with self._lock: - r = f(self, *args, **kwargs) - return r - - return wrapper - def __initialize (self): self._db = defaultdict(list) self.inst_cats = set([self.ALL]) diff --git a/portato/db/sql.py b/portato/db/sql.py index c80fd91..e7be91e 100644 --- a/portato/db/sql.py +++ b/portato/db/sql.py @@ -22,8 +22,8 @@ import hashlib import os from functools import wraps -from threading import RLock +from ..constants import SESSION_DIR from ..helper import info, error, debug from ..backend import system from .database import Database, PkgData @@ -31,11 +31,13 @@ from .database import Database, PkgData class SQLDatabase (Database): FORBIDDEN = (".bzr", ".svn", ".git", "CVS", ".hg", "_darcs") + lock = Database.lock def __init__ (self): """Constructor.""" + Database.__init__(self) + self._restrict = "" - self._lock = RLock() pkgdb = os.path.join(SESSION_DIR, "package.db") pkgdb_existed = os.path.exists(pkgdb) @@ -130,16 +132,6 @@ class SQLDatabase (Database): return changed - def lock (f): - @wraps(f) - def wrapper (self, *args, **kwargs): - with self._lock: - r = f(self, *args, **kwargs) - - return r - - return wrapper - def con (f): @wraps(f) def wrapper (*args, **kwargs): diff --git a/portato/gui/windows/main.py b/portato/gui/windows/main.py index 0882c2a..b8d3c17 100644 --- a/portato/gui/windows/main.py +++ b/portato/gui/windows/main.py @@ -26,11 +26,12 @@ from ...backend import flags, system # must be the first to avoid circular deps from ... import get_listener, plugin from ...helper import debug, warning, error, info, unique_array from ...session import Session +from ...db import Database from ...constants import CONFIG_LOCATION, VERSION, APP_ICON, ICON_DIR from ...backend.exceptions import PackageNotFoundException, BlockedException # more GUI stuff -from ..utils import Database, Config, GtkThread, get_color +from ..utils import Config, GtkThread, get_color from ..queue import EmergeQueue from ..session import SESSION_VERSION, SessionException, OldSessionException, NewSessionException from ..wrapper import GtkTree, GtkConsole -- cgit v1.2.3