File: __init__.py

package info (click to toggle)
python-carrot 0.10.7-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 508 kB
  • ctags: 550
  • sloc: python: 2,675; makefile: 75
file content (54 lines) | stat: -rw-r--r-- 1,571 bytes parent folder | download | duplicates (3)
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
"""

Working with Backends.

"""
import sys

from carrot.utils import rpartition

DEFAULT_BACKEND = "carrot.backends.pyamqplib.Backend"

BACKEND_ALIASES = {
    "amqp": "carrot.backends.pyamqplib.Backend",
    "amqplib": "carrot.backends.pyamqplib.Backend",
    "stomp": "carrot.backends.pystomp.Backend",
    "stompy": "carrot.backends.pystomp.Backend",
    "memory": "carrot.backends.queue.Backend",
    "mem": "carrot.backends.queue.Backend",
    "pika": "carrot.backends.pikachu.AsyncoreBackend",
    "pikachu": "carrot.backends.pikachu.AsyncoreBackend",
    "syncpika": "carrot.backends.pikachu.SyncBackend",
}

_backend_cache = {}


def resolve_backend(backend=None):
    backend = BACKEND_ALIASES.get(backend, backend)
    backend_module_name, _, backend_cls_name = rpartition(backend, ".")
    return backend_module_name, backend_cls_name


def _get_backend_cls(backend=None):
    backend_module_name, backend_cls_name = resolve_backend(backend)
    __import__(backend_module_name)
    backend_module = sys.modules[backend_module_name]
    return getattr(backend_module, backend_cls_name)


def get_backend_cls(backend=None):
    """Get backend class by name.

    The backend string is the full path to a backend class, e.g.::

        "carrot.backends.pyamqplib.Backend"

    If the name does not include "``.``" (is not fully qualified),
    the alias table will be consulted.

    """
    backend = backend or DEFAULT_BACKEND
    if backend not in _backend_cache:
        _backend_cache[backend] = _get_backend_cls(backend)
    return _backend_cache[backend]