config_loader.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import json
  2. from flask_cors import CORS
  3. import os
  4. import logging
  5. from engine.basic_queries import basicQueries
  6. def config(flask_app):
  7. env_label = os.environ.get('APP_ENVIRONMENT')
  8. # Local development
  9. if env_label=='local_development':
  10. configLocal(flask_app)
  11. # Any different environments needed, including production
  12. #elif:
  13. #
  14. # Default
  15. else:
  16. configLocal(flask_app)
  17. def configLocal(flask_app):
  18. # Read configuration file
  19. flask_app.config.from_file(os.path.dirname(os.path.abspath(__file__)) + "/basic_config.json", load=json.load)
  20. # This will enable CORS for all routes
  21. CORS(flask_app)
  22. # CORS enabling is useful to prevent annoying CORS error when developing locally
  23. # Config DB access
  24. try:
  25. dataConfigObject = flask_app.config['DATA_CONFIG']
  26. dbPath = dataConfigObject['dbPath']
  27. flask_app.config['DATA_CONFIG']['dbPath'] = os.path.join(flask_app.root_path, os.pardir, dbPath)
  28. if dataConfigObject['dynamic_occ_tables']:
  29. # Find the list of occurrences tables dynamically
  30. flask_app.config['DATA_CONFIG']['listOcc'] = get_occ_tables(flask_app.config['DATA_CONFIG'])
  31. else:
  32. if dataConfigObject.get('listOcc') is None:
  33. raise Exception
  34. except:
  35. raise Exception('Error in initializing data configuration')
  36. # Config logging
  37. if flask_app.config['LOGGER_CONFIG'].get('level')=='info':
  38. logging_level = logging.INFO
  39. else:
  40. logging_level = logging.DEBUG
  41. logging.basicConfig(filename = flask_app.config['LOGGER_CONFIG']['filename'],
  42. filemode = 'w',
  43. format = '%(asctime)s %(message)s',
  44. level=logging_level)
  45. def get_occ_tables(dataConfig):
  46. handler = basicQueries(dataConfig)
  47. all_tables = handler.queryHandler.query({'queryType': 'occ_tables'})
  48. occ_tables = [table['name'] for table in all_tables if table['name'].startswith('Occ')]
  49. return occ_tables