1
# -.- coding: utf-8 -.-
5
# Copyright © 2009 Markus Korn <thekorn@gmx.de>
6
# Copyright © 2009 Siegfried-Angel Gevatter Pujals <rainct@ubuntu.com>
8
# This program is free software: you can redistribute it and/or modify
9
# it under the terms of the GNU Lesser General Public License as published by
10
# the Free Software Foundation, either version 3 of the License, or
11
# (at your option) any later version.
13
# This program is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU Lesser General Public License for more details.
18
# You should have received a copy of the GNU Lesser General Public License
19
# along with this program. If not, see <http://www.gnu.org/licenses/>.
23
from xdg import BaseDirectory
25
logging.basicConfig(level=logging.DEBUG)
26
log = logging.getLogger("zeitgeist.engine")
28
DB_PATH = os.path.join(BaseDirectory.save_data_path("zeitgeist"),
31
AVAILABLE_ENGINES = ["querymancer", "storm"]
32
ENGINE_FALLBACK = AVAILABLE_ENGINES[0]
34
def get_engine_type():
35
""" Returns the value of the $ZEITGEIST_ENGINE environment variable or,
36
if it isn't defined, the default engine."""
37
value = os.environ.get("ZEITGEIST_ENGINE")
38
if value == "default":
40
if value and value.lower() not in AVAILABLE_ENGINES:
41
raise RuntimeError("Unknown engine type requested: \"%s\"." % value)
42
return value.lower() if value else ENGINE_FALLBACK
45
def create_engine(engine_type=None):
46
""" Creates an engine instance of the type defined by 'engine_type'.
47
If 'engine_type' is None 'ENGINE_FALLBACK' is used.
48
This function looks at _zeitgeist.engine to find the engine implementation.
49
Each engine implementation has to follow the following conventions:
50
1.) it has to be in _zeitgeist/engine/SOMENAME_engine.py
51
(where SOMENAME defines the type)
52
2.) the name of the class has to be ZeitgeistEngine and the class
53
itself has to be a sublass of _zeitgeist.engine.engine_base.BaseEngine
56
if engine_type is None:
57
engine_type = ENGINE_FALLBACK
58
engine_type = engine_type.lower()
60
# See if we can reuse _engine
61
if _engine and not _engine.is_closed():
62
running_type = _engine.__module__.split(".").pop().lower()
63
if not running_type == engine_type:
65
("There is already a zeitgeist engine running. But this "
66
"engine has another than the requested type "
67
"(requested='%s', running='%s')" %(engine_type, running_type))
71
log.debug("Creating engine '%s'" % engine_type)
72
engine_cls = __import__(
73
"_zeitgeist.engine.%s_engine" %engine_type,
74
globals(), locals(), ["ZeitgeistEngine",], -1
76
except ImportError, err:
77
logging.exception("Could not load engine implementation for %r" % engine_type)
79
_engine = engine_cls.ZeitgeistEngine()
82
def get_default_engine():
83
""" Get the running engine instance or create a new one. """
84
if _engine is None or _engine.is_closed() :
85
return create_engine(engine_type=get_engine_type())