dsn.py 716 Bytes
Newer Older
Weizhi Cui's avatar
Weizhi Cui committed
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
# @Version     : Python 3.11.4
# @Software    : Sublime Text 4
# @Author      : StudentCWZ
# @Email       : StudentCWZ@outlook.com
# @Date        : 2023/10/29 00:36
# @File        : dsn.py
# @Description :
"""

from flask import Flask


def dsn(app: Flask) -> str:
    """
    Initialize the MySQL dsn extension

    :param app: flask.Flask application instance
    :return: dsn
    """
    mysql_cfg = app.config.get('MySQL')
    if mysql_cfg is None:
        raise KeyError('Key MySQL error')
    return (f'mysql+pymysql://{app.config.MySQL.USER}:'
            f'{app.config.MySQL.PASSWORD}@{app.config.MySQL.HOST}:{app.config.MySQL.PORT}/{app.config.MySQL.DB}')