1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- import json
- from flask_cors import CORS
- import os
- import logging
- from engine.basic_queries import basicQueries
- def config(flask_app):
- env_label = os.environ.get('APP_ENVIRONMENT')
-
- # Local development
- if env_label=='local_development':
- configLocal(flask_app)
-
- # Any different environments needed, including production
- #elif:
- #
- # Default
- else:
- configLocal(flask_app)
- def configLocal(flask_app):
- # Read configuration file
- flask_app.config.from_file(os.path.dirname(os.path.abspath(__file__)) + "/basic_config.json", load=json.load)
-
- # This will enable CORS for all routes
- CORS(flask_app)
- # CORS enabling is useful to prevent annoying CORS error when developing locally
- # 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
|