aboutsummaryrefslogtreecommitdiff
path: root/gn3/db_utils.py
blob: 3b72d2876fa98de1dff183d1279c5f6d6bfc1dfb (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
"""module contains all db related stuff"""
from typing import Tuple
from urllib.parse import urlparse
import MySQLdb as mdb
from gn3.settings import SQL_URI


def parse_db_url() -> Tuple:
    """function to parse SQL_URI env variable note:there\
    is a default value for SQL_URI so a tuple result is\
    always expected"""
    parsed_db = urlparse(SQL_URI)
    return (parsed_db.hostname, parsed_db.username,
            parsed_db.password, parsed_db.path[1:])


def database_connector() -> mdb.Connection:
    """function to create db connector"""
    host, user, passwd, db_name = parse_db_url()
    return mdb.connect(host, user, passwd, db_name)