Completed
Push — master ( f167fe...72f8c6 )
by Roy
01:23 queued 10s
created

connect_message_queue()   D

Complexity

Conditions 9

Size

Total Lines 63

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 9
c 3
b 1
f 0
dl 0
loc 63
rs 4.7173

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
#!/usr/bin/env python
2
# -*- encoding: utf-8 -*-
3
# vim: set et sw=4 ts=4 sts=4 ff=unix fenc=utf8:
4
# Author: Binux<[email protected]>
5
#         http://binux.me
6
# Created on 2015-04-30 21:47:08
7
8
import logging
9
10
try:
11
    from urllib import parse as urlparse
12
except ImportError:
13
    import urlparse
14
15
16
def connect_message_queue(name, url=None, maxsize=0, lazy_limit=True):
17
    """
18
    create connection to message queue
19
20
    name:
21
        name of message queue
22
23
    rabbitmq:
24
        amqp://username:password@host:5672/%2F
25
        see https://www.rabbitmq.com/uri-spec.html
26
    beanstalk:
27
        beanstalk://host:11300/
28
    redis:
29
        redis://host:6379/db
30
        redis://host1:port1,host2:port2,...,hostn:portn (for redis 3.x in cluster mode)
31
    kombu:
32
        kombu+transport://userid:password@hostname:port/virtual_host
33
        see http://kombu.readthedocs.org/en/latest/userguide/connections.html#urls
34
    builtin:
35
        None
36
    """
37
38
    if not url:
39
        from pyspider.libs.multiprocessing_queue import Queue
40
        return Queue(maxsize=maxsize)
41
42
    parsed = urlparse.urlparse(url)
43
    if parsed.scheme == 'amqp':
44
        from .rabbitmq import Queue
45
        return Queue(name, url, maxsize=maxsize, lazy_limit=lazy_limit)
46
    elif parsed.scheme == 'beanstalk':
47
        from .beanstalk import Queue
48
        return Queue(name, host=parsed.netloc, maxsize=maxsize)
49
    elif parsed.scheme == 'redis':
50
        from .redis_queue import Queue
51
        if ',' in parsed.netloc:
52
            """
53
            redis in cluster mode (there is no concept of 'db' in cluster mode)
54
            ex. redis://host1:port1,host2:port2,...,hostn:portn
55
            """
56
            cluster_nodes = []
57
            for netloc in parsed.netloc.split(','):
58
                cluster_nodes.append({'host': netloc.split(':')[0], 'port': int(netloc.split(':')[1])})
59
60
            return Queue(name=name, maxsize=maxsize, lazy_limit=lazy_limit, cluster_nodes=cluster_nodes)
61
62
        else:
63
            db = parsed.path.lstrip('/').split('/')
64
            try:
65
                db = int(db[0])
66
            except:
67
                logging.warning('redis DB must zero-based numeric index, using 0 instead')
68
                db = 0
69
70
            password = parsed.password or None
71
72
            return Queue(name=name, host=parsed.hostname, port=parsed.port, db=db, maxsize=maxsize, password=password, lazy_limit=lazy_limit)
73
    elif url.startswith('kombu+'):
74
        url = url[len('kombu+'):]
75
        from .kombu_queue import Queue
76
        return Queue(name, url, maxsize=maxsize, lazy_limit=lazy_limit)
77
    else:
78
        raise Exception('unknown connection url: %s', url)
79