~justin-fathomdb/nova/justinsb-openstack-api-volumes

« back to all changes in this revision

Viewing changes to vendor/carrot/backends/__init__.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
 
 
3
Working with Backends.
 
4
 
 
5
"""
 
6
import sys
 
7
 
 
8
from carrot.utils import rpartition
 
9
 
 
10
DEFAULT_BACKEND = "carrot.backends.pyamqplib.Backend"
 
11
 
 
12
BACKEND_ALIASES = {
 
13
    "amqp": "carrot.backends.pyamqplib.Backend",
 
14
    "amqplib": "carrot.backends.pyamqplib.Backend",
 
15
    "stomp": "carrot.backends.pystomp.Backend",
 
16
    "stompy": "carrot.backends.pystomp.Backend",
 
17
    "memory": "carrot.backends.queue.Backend",
 
18
    "mem": "carrot.backends.queue.Backend",
 
19
    "pika": "carrot.backends.pikachu.AsyncoreBackend",
 
20
    "pikachu": "carrot.backends.pikachu.AsyncoreBackend",
 
21
    "syncpika": "carrot.backends.pikachu.SyncBackend",
 
22
}
 
23
 
 
24
_backend_cache = {}
 
25
 
 
26
 
 
27
def resolve_backend(backend=None):
 
28
    backend = BACKEND_ALIASES.get(backend, backend)
 
29
    backend_module_name, _, backend_cls_name = rpartition(backend, ".")
 
30
    return backend_module_name, backend_cls_name
 
31
 
 
32
 
 
33
def _get_backend_cls(backend=None):
 
34
    backend_module_name, backend_cls_name = resolve_backend(backend)
 
35
    __import__(backend_module_name)
 
36
    backend_module = sys.modules[backend_module_name]
 
37
    return getattr(backend_module, backend_cls_name)
 
38
 
 
39
 
 
40
def get_backend_cls(backend=None):
 
41
    """Get backend class by name.
 
42
 
 
43
    The backend string is the full path to a backend class, e.g.::
 
44
 
 
45
        "carrot.backends.pyamqplib.Backend"
 
46
 
 
47
    If the name does not include "``.``" (is not fully qualified),
 
48
    the alias table will be consulted.
 
49
 
 
50
    """
 
51
    backend = backend or DEFAULT_BACKEND
 
52
    if backend not in _backend_cache:
 
53
        _backend_cache[backend] = _get_backend_cls(backend)
 
54
    return _backend_cache[backend]