engine.py 4.26 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# -*- encoding: utf-8 -*-
# -----------------------------------------------------------------------------
# @File Name  : app.py
# @Time       : 2020/11/18 下午12:45
# @Author     : X. Peng
# @Email      : acepengxiong@163.com
# @Software   : PyCharm
# -----------------------------------------------------------------------------


import logging
import redis
import os
import sys
import yaml
from sqlalchemy import create_engine
赵杰's avatar
赵杰 committed
17
from sqlalchemy.orm import sessionmaker, scoped_session
18 19 20 21 22 23 24

env = sys.argv[-1]
work_dir = os.getcwd()
CFG_FILEPATH = work_dir + '/app/config/config.yaml'

config = yaml.load(open(CFG_FILEPATH, 'r'), Loader=yaml.FullLoader)

25 26
template_folder = config[env]['oss']['imgs_url_prefix']
pdf_folder = config[env]['oss']['pdf_url_prefix']
27 28

tamp_product_engine = create_engine(
29
    'mysql+mysqldb://{user}:{password}@{host}:{port}/{db}?charset={charset}'.format(
30
        db=config[env]['MySQL']['tamp_product_db'],
31 32 33 34
        host=config[env]['MySQL']['host'],
        port=config[env]['MySQL']['port'],
        user=config[env]['MySQL']['user'],
        password=config[env]['MySQL']['password'],
35 36 37 38 39 40
        charset="utf8"
    ),
    max_overflow=0,  # 超过连接池大小外最多创建的连接
    pool_size=100,  # 连接池大小
    pool_timeout=5,
    pool_recycle=600
41
)
42
tamp_order_engine = create_engine(
43
    'mysql+mysqldb://{user}:{password}@{host}:{port}/{db}?charset={charset}'.format(
44
        db=config[env]['MySQL']['tamp_order_db'],
45 46 47 48
        host=config[env]['MySQL']['host'],
        port=config[env]['MySQL']['port'],
        user=config[env]['MySQL']['user'],
        password=config[env]['MySQL']['password'],
49 50 51 52 53 54
        charset="utf8"
    ),
    max_overflow=0,  # 超过连接池大小外最多创建的连接
    pool_size=100,  # 连接池大小
    pool_timeout=5,
    pool_recycle=600
55
)
56 57

tamp_user_engine = create_engine(
58
    'mysql+mysqldb://{user}:{password}@{host}:{port}/{db}?charset={charset}'.format(
59 60 61 62 63 64
        db=config[env]['MySQL']['tamp_user_db'],
        host=config[env]['MySQL']['host'],
        port=config[env]['MySQL']['port'],
        user=config[env]['MySQL']['user'],
        password=config[env]['MySQL']['password'],
        charset="utf8"
65 66 67 68 69
    ),
    max_overflow=0,  # 超过连接池大小外最多创建的连接
    pool_size=100,  # 连接池大小
    pool_timeout=5,
    pool_recycle=600
70
)
李宗熹's avatar
李宗熹 committed
71 72

tamp_fund_engine = create_engine(
73
    'mysql+mysqldb://{user}:{password}@{host}:{port}/{db}?charset={charset}'.format(
李宗熹's avatar
李宗熹 committed
74 75 76 77 78 79
        db=config[env]['MySQL']['tamp_fund_db'],
        host=config[env]['MySQL']['host'],
        port=config[env]['MySQL']['port'],
        user=config[env]['MySQL']['user'],
        password=config[env]['MySQL']['password'],
        charset="utf8"
pengxiong's avatar
pengxiong committed
80 81 82 83 84
    ),
    max_overflow=0,  # 超过连接池大小外最多创建的连接
    pool_size=100,  # 连接池大小
    pool_timeout=5,
    pool_recycle=600
wang zhengwei's avatar
wang zhengwei committed
85
)
86

赵杰's avatar
赵杰 committed
87 88 89 90 91 92 93 94 95 96 97 98
tamp_diagnose_app_engine = create_engine(
    'mysql+mysqldb://{user}:{password}@{host}:{port}/{db}?charset={charset}'.format(
        db=config[env]['MySQL']['tamp_diagnose_app_db'],
        host=config[env]['MySQL']['host'],
        port=config[env]['MySQL']['port'],
        user=config[env]['MySQL']['user'],
        password=config[env]['MySQL']['password'],
        charset="utf8"
    )
)


pengxiong's avatar
pengxiong committed
99 100 101
redis = redis.StrictRedis(
    host=config[env]['redis']['host'],
    port=config[env]['redis']['port'],
pengxiong's avatar
pengxiong committed
102 103
    db=config[env]['redis']['db'],
    password=config[env]['redis']['password']
pengxiong's avatar
pengxiong committed
104
)
105 106

logging.basicConfig(level=logging.INFO,
pengxiong's avatar
pengxiong committed
107 108
                    filename=work_dir + config[env]['log']['filename'],
                    filemode=config[env]['log']['filemode'],
109 110 111 112
                    format=config[env]['log']['format'],
                    datefmt=config[env]['log']['datefmt']
                    )

李宗熹's avatar
李宗熹 committed
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
class TAMP_SQL(object):
    """[sqlalchemy 封装]

    Args:
        object ([type]): [description]

    Returns:
        [type]: [description]
    """

    def __init__(self, db_engine):
        # 创建DBSession类型:
        self.DBSession = scoped_session(sessionmaker(bind=db_engine))
        self.session = self.DBSession()

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        try:
            self.session.commit()
        except:
            self.session.rollback()
        finally:
            self.session.close()