From 0410f6e90b80d4aa8427ea3c398fe70e1977043c Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Wed, 13 Jul 2022 01:19:57 +0300 Subject: Add missing db_utils module --- qc_app/db_utils.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 qc_app/db_utils.py (limited to 'qc_app/db_utils.py') diff --git a/qc_app/db_utils.py b/qc_app/db_utils.py new file mode 100644 index 0000000..95b0057 --- /dev/null +++ b/qc_app/db_utils.py @@ -0,0 +1,20 @@ +"""module contains all db related stuff""" +from typing import Tuple + +from urllib.parse import urlparse +import MySQLdb as mdb +from flask import current_app as app + +def parse_db_url() -> Tuple: + """ + Parse SQL_URI configuration variable. + """ + parsed_db = urlparse(app.config["SQL_URI"]) + return (parsed_db.hostname, parsed_db.username, + parsed_db.password, parsed_db.path[1:]) + + +def database_connection() -> mdb.Connection: + """function to create db connector""" + host, user, passwd, db_name = parse_db_url() + return mdb.connect(host, user, passwd, db_name) -- cgit v1.2.3