diff options
-rw-r--r-- | qc_app/db_utils.py | 20 |
1 files changed, 20 insertions, 0 deletions
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) |