aboutsummaryrefslogtreecommitdiff
path: root/dbs.py
blob: b32afa317b2e471a2dd14fa61a506a74873fc630 (plain) (blame)
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
import psycopg2
import redis as _redis

redis = None
psql = None

def redis_connected():
    global redis
    if redis is None:
        return False
    else:
        try:
            return redis.ping()
        except Exception:
            return False

def psql_connected():
    global psql
    return psql is not None and psql.closed == 0

def connect_redis(args):
    global redis

    redis_config = {
            "host": args.redis_host,
            "port": args.redis_port,
            "db": args.redis_database,
            }
    if redis_config["host"].startswith("/"):
        redis_config["unix_socket_path"] = redis_config.pop("host")
        del(redis_config["port"])
    del(args.redis_host)
    del(args.redis_port)
    del(args.redis_database)

    if redis is None:
        redis = _redis.Redis(**redis_config)

def connect_psql(args):
    global psql
    pg_config = {
            "host": args.db_host,
            "port": args.db_port,
            "user": args.db_user,
            "password": args.db_password,
            "database": args.db_database,
            }
    del(args.db_host)
    del(args.db_port)
    del(args.db_user)
    del(args.db_password)
    del(args.db_database)

    if psql is None:
        psql = psycopg2.connect(**pg_config)