def main(argv): try: opts, args = getopt.getopt(argv, "c:", ["configure=", "help"]) except getopt.GetoptError: usage() sys.exit() conf_path = os.path.join(os.path.abspath("."), 'configs/conf') for opt, arg in opts: if opt == '--help': usage() sys.exit() elif opt in ('-c', '--configure'): conf_path = arg else: usage() exit() def _exit_w_info(info): print('\n%s\n' % info) usage() exit() def _ok_conf(conf): def check_cfg(cfg): cpath = os.path.join(conf, cfg) return ((os.path.exists(cpath) and cpath) or _exit_w_info('missing %s.' % cpath)) return [check_cfg(cfg) for cfg in ('api.yaml', 'logging.yaml')] api_conf, logging_conf = _ok_conf(conf_path) app_conf = {} with codecs.open(logging_conf, 'r', 'utf-8') as logging_file: log_conf_dict = yaml.load(logging_file) logfile_path = os.path.split( log_conf_dict['handlers']['file']['filename'])[0] if not os.path.exists(logfile_path): os.makedirs(logfile_path) logging.config.dictConfig(log_conf_dict) with codecs.open(api_conf, 'r', 'utf-8') as conff: app_conf.update(yaml.load(conff)) _log = logging.getLogger(__name__) # 等logging配置好了再导入 from mysqldal.sql_engine import sql_init from RESTFul_flask.app import app from RESTFul_flask.app import app_init from utils.context import Context context = Context() context.init(app_conf) app_init() sql_init() _log.debug('start from main.py') app.run(host="0.0.0.0", debug=True)
sys.path.append(project_path) # 加入工程环境 from mysqldal.sql_engine import sql_init from bll import basic_interface from mysqldal.models import Idc from mysqldal.tests import fixture from utils.context import Context from utils.tools import get_api_conf # 上下文初始化 conf_path = os.path.join(os.path.abspath("."), '../configs/conf') api_conf = get_api_conf(conf_path) context = Context() context.init(api_conf) sql_init() class TestApiIdcList: def setup(self): self.session = context.Session() self.idc1 = Idc(**fixture.IDC1) self.session.add(self.idc1) self.session.commit() def teardown(self): self.session.delete(self.idc1) def test1(self): condiction_dict = {