import json from flask_cors import CORS import os import logging from engine.basic_queries import basicQueries def config(flask_app): # Get environment label, define conf file name env_label = os.environ.get('APP_ENVIRONMENT') if env_label is not None and env_label!="": confFileName = "config_" + env_label + ".json" # Default else: confFileName = "basic_config.json" # Read configuration file confFileName = os.path.join(os.path.dirname(os.path.abspath(__file__)), confFileName) flask_app.config.from_file(confFileName, load=json.load) doCommonConfig(flask_app) # Local environment if flask_app.config['LOCAL_ENVIRONMENT']: # This will enable CORS for all routes CORS(flask_app) # CORS enabling is useful to prevent annoying CORS error when developing locally def doCommonConfig(flask_app): # Config DB access try: dataConfigObject = flask_app.config['DATA_CONFIG'] dbPath = dataConfigObject['dbPath'] flask_app.config['DATA_CONFIG']['dbPath'] = os.path.join(flask_app.root_path, os.pardir, dbPath) if dataConfigObject['dynamic_occ_tables']: # Find the list of occurrences tables dynamically flask_app.config['DATA_CONFIG']['listOcc'] = get_occ_tables(flask_app.config['DATA_CONFIG']) else: if dataConfigObject.get('listOcc') is None: raise Exception except: raise Exception('Error in initializing data configuration') # Config logging if flask_app.config['LOGGER_CONFIG'].get('level')=='info': logging_level = logging.INFO else: logging_level = logging.DEBUG logging.basicConfig(filename = flask_app.config['LOGGER_CONFIG']['filename'], filemode = 'w', format = '%(asctime)s %(message)s', level=logging_level) def get_occ_tables(dataConfig): handler = basicQueries(dataConfig) all_tables = handler.queryHandler.query({'queryType': 'occ_tables'}) occ_tables = [table['name'] for table in all_tables if table['name'].startswith('Occ')] return occ_tables