~ubuntu-branches/ubuntu/lucid/zeitgeist/lucid

« back to all changes in this revision

Viewing changes to _zeitgeist/engine/__init__.py

  • Committer: Bazaar Package Importer
  • Author(s): Siegfried-Angel Gevatter Pujals
  • Date: 2009-08-17 00:12:51 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090817001251-lfpvqpxxf9bxefzu
Tags: 0.2.1-0ubuntu1
* New upstream release.
   - Update dependencies.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -.- coding: utf-8 -.-
 
2
 
 
3
# Zeitgeist
 
4
#
 
5
# Copyright © 2009 Markus Korn <thekorn@gmx.de>
 
6
# Copyright © 2009 Siegfried-Angel Gevatter Pujals <rainct@ubuntu.com>
 
7
#
 
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.
 
12
#
 
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.
 
17
#
 
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/>.
 
20
 
 
21
import os
 
22
import logging
 
23
from xdg import BaseDirectory
 
24
 
 
25
logging.basicConfig(level=logging.DEBUG)
 
26
log = logging.getLogger("zeitgeist.engine")
 
27
 
 
28
DB_PATH = os.path.join(BaseDirectory.save_data_path("zeitgeist"),
 
29
        "database.sqlite")
 
30
 
 
31
AVAILABLE_ENGINES = ["querymancer", "storm"]
 
32
ENGINE_FALLBACK = AVAILABLE_ENGINES[0]
 
33
 
 
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":
 
39
                value = None
 
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
 
43
 
 
44
_engine = None
 
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
 
54
        """     
 
55
        global _engine
 
56
        if engine_type is None:
 
57
                engine_type = ENGINE_FALLBACK
 
58
        engine_type = engine_type.lower()               
 
59
        
 
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:
 
64
                        raise RuntimeError(
 
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))
 
68
                        )
 
69
                return _engine
 
70
        try:
 
71
                log.debug("Creating engine '%s'" % engine_type)
 
72
                engine_cls = __import__(
 
73
                        "_zeitgeist.engine.%s_engine" %engine_type,
 
74
                        globals(), locals(), ["ZeitgeistEngine",], -1
 
75
                )
 
76
        except ImportError, err:
 
77
                logging.exception("Could not load engine implementation for %r" % engine_type)
 
78
                raise
 
79
        _engine = engine_cls.ZeitgeistEngine()
 
80
        return _engine
 
81
 
 
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())
 
86
        return _engine