Files
MSState-Library-ETD/app/configuration.py
T
Aaron Kimbrell ec28417fef Database setup
Started working on Db models (not functional right now)
Schemas will need to be added to more as we go, but it's functional
Updated the configuration for the other extensions that we are using
Fixed the DB connection scheme in the config
2019-03-21 14:17:35 -05:00

116 lines
3.6 KiB
Python

import configparser
import os
from distutils.version import LooseVersion
class AppConfiguration(object):
"""Configuration Version"""
CONFIG_VERSION = '2.0'
def __init__(self):
"""Creates/Parses the config.ini"""
# create/read the config.ini
self.config_parser = self.reload_config()
self.__update_config()
# setup configuration
self.mode = self.config_parser.get('FLASK', 'mode')
# setup flask configuration
self.flask_config = self.__FLASK_CONFIGS.get(self.mode, self.__DefaultFlaskConfig)
self.flask_config.SECRET_KEY = self.__FLASK_CONFIGS.get('secret_key', 'SECRET_KEY_NOT_SET')
def _create_config(self):
"""Creates a default configuration (*.ini) file
Returns:
config (ConfigParser): the config.ini parser
"""
config = configparser.ConfigParser()
# create the config
config['CONFIG'] = {
'version': self.CONFIG_VERSION
}
config['SQLALCHEMY'] = {
'SQLALCHEMY_DATABASE_URI': 'postgresql://user:password@host:port/database',
'SQLALCHEMY_TRACK_MODIFICATIONS': 'False',
}
config['FLASK'] = {
'mode': 'default',
'secret_key': 'SECRET_KEY_NOT_SET'
}
config['FLASK-MAIL'] = {
'MAIL_SERVER': 'smtp.gmail.com',
'MAIL_PORT': '587',
'MAIL_USE_SSL': 'False',
'MAIL_USE_TLS': 'True',
'MAIL_USERNAME': 'yourname@gmail.com',
'MAIL_PASSWORD': 'password',
}
config['FLASK-USER'] = {
'USER_APP_NAME': 'MSState Library ETD',
'USER_EMAIL_SENDER_NAME': 'MSState',
'USER_EMAIL_SENDER_EMAIL': 'yourname@gmail.com',
}
# write the config.
config_path = os.path.dirname(os.path.abspath(__file__))
config_path = os.path.join(os.path.dirname(config_path), 'config.ini')
with open(config_path, 'w') as configfile:
config.write(configfile)
return config
def reload_config(self):
"""Create/read the config.ini into ConfigParser
Returns:
the ConfigParser from config.ini
"""
if not os.path.exists('config.ini'):
print('creating new config...')
config = self._create_config()
else:
print('loading config...')
config = configparser.ConfigParser()
config.read('config.ini')
return config
def get(self, *args):
"""Returns ConfigParser.get(*args)"""
return self.config_parser.get(*args)
def __update_config(self):
"""Checks if config.ini needs an update"""
if LooseVersion(self.config_parser.get('CONFIG', 'version')) < LooseVersion(self.CONFIG_VERSION):
print('updating config...')
os.rename('config.ini', 'config.ini.backup')
self.config_parser = self.reload_config()
class __DefaultFlaskConfig(object):
"""Default/Production Flask Configuration"""
DEBUG = False
TESTING = False
SQLALCHEMY_TRACK_MODIFICATIONS = False
class __DevelopmentFlaskConfig(__DefaultFlaskConfig):
"""Development/Debug Configuration"""
DEBUG = True
ASSETS_DEBUG = True
SQLALCHEMY_ECHO = True
class __TestingFlaskConfig(__DefaultFlaskConfig):
"""Testing Configuration"""
TESTING = True
# config map
__FLASK_CONFIGS = {
'dev': __DevelopmentFlaskConfig,
'prod': __DefaultFlaskConfig,
'test': __TestingFlaskConfig,
'default': __DevelopmentFlaskConfig
}