# -*- 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
from sqlalchemy.orm import sessionmaker, scoped_session

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

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


tamp_product_engine = create_engine(
    'mysql+pymysql://{user}:{password}@{host}:{port}/{db}?charset={charset}'.format(
        db=config[env]['MySQL']['tamp_product_db'],
        host=config[env]['MySQL']['host'],
        port=config[env]['MySQL']['port'],
        user=config[env]['MySQL']['user'],
        password=config[env]['MySQL']['password'],
        charset="utf8"),
    echo=True
)
tamp_order_engine = create_engine(
    'mysql+pymysql://{user}:{password}@{host}:{port}/{db}?charset={charset}'.format(
        db=config[env]['MySQL']['tamp_order_db'],
        host=config[env]['MySQL']['host'],
        port=config[env]['MySQL']['port'],
        user=config[env]['MySQL']['user'],
        password=config[env]['MySQL']['password'],
        charset="utf8"),
    echo=True
)

tamp_user_engine = create_engine(
    'mysql+pymysql://{user}:{password}@{host}:{port}/{db}?charset={charset}'.format(
        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"
    ),
    echo=True
)
tamp_product_session = scoped_session(sessionmaker(bind=tamp_product_engine))()
tamp_order_session = scoped_session(sessionmaker(bind=tamp_order_engine))()
tamp_user_session = scoped_session(sessionmaker(bind=tamp_user_engine))()

# redis = redis.StrictRedis(
#     host=config[env]['redis']['host'],
#     port=config[env]['redis']['port'],
#     db=config[env]['redis']['db']
# )

logging.basicConfig(level=logging.INFO,
                    filename=work_dir + config[env]['log']['filename'],
                    filemode=config[env]['log']['filemode'],
                    format=config[env]['log']['format'],
                    datefmt=config[env]['log']['datefmt']
                    )

