summaryrefslogtreecommitdiffstats
path: root/kittystore/__init__.py
blob: ac61abc5f10eead5e0e78b750a8c8def5d84074b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# -*- coding: utf-8 -*-

"""
Module entry point: call get_store() to instanciate a KittyStore
implementation.

Copyright (C) 2012 Aurelien Bompard
Author: Aurelien Bompard <abompard@fedoraproject.org>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.
See http://www.gnu.org/copyleft/gpl.html  for the full text of the
license.
"""

__all__ = ("get_store", "create_store", "MessageNotFound",
           "SchemaUpgradeNeeded")


from kittystore.search import SearchEngine
from kittystore.caching import register_events


def _check_settings(settings):
    required_keys = ("KITTYSTORE_URL", "KITTYSTORE_SEARCH_INDEX",
                     "MAILMAN_REST_SERVER", "MAILMAN_API_USER",
                     "MAILMAN_API_PASS")
    for req_key in required_keys:
        try:
            getattr(settings, req_key)
        except AttributeError:
            raise AttributeError("The settings file is missing the \"%s\" key" % req_key)
    if settings.KITTYSTORE_URL.startswith("mongo://"):
        raise NotImplementedError

def _get_search_index(settings):
    search_index_path = settings.KITTYSTORE_SEARCH_INDEX
    if search_index_path is None:
        return None
    return SearchEngine(search_index_path)

def get_store(settings, debug=None, auto_create=False):
    """Factory for a KittyStore subclass"""
    _check_settings(settings)
    if debug is None:
        debug = getattr(settings, "KITTYSTORE_DEBUG", False)

    search_index = _get_search_index(settings)

    from kittystore.storm import get_storm_store
    store = get_storm_store(settings, search_index, debug, auto_create)

    if search_index is not None and search_index.needs_upgrade():
        if auto_create:
            search_index.upgrade(store)
        else:
            store.close()
            raise SchemaUpgradeNeeded()

    register_events()

    return store


def create_store(settings, debug=None):
    """Factory for a KittyStore subclass"""
    _check_settings(settings)
    if debug is None:
        debug = getattr(settings, "KITTYSTORE_DEBUG", False)

    search_index = _get_search_index(settings)
    from kittystore.storm import get_storm_store, create_storm_store
    create_storm_store(settings, debug)
    store = get_storm_store(settings, search_index, debug)
    if search_index is not None and search_index.needs_upgrade():
        search_index.upgrade(store)
    return store


class SchemaUpgradeNeeded(Exception):
    """Raised when there are pending patches"""


class MessageNotFound(Exception):
    pass