2017-06-09 06:25:41 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# ScribeEngine - Open Source Blog Software #
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
|
|
# Copyright (c) 2010-2017 Raoul Snyman #
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
|
|
# This program is free software; you can redistribute it and/or modify it #
|
|
|
|
# under the terms of the GNU General Public License as published by the Free #
|
|
|
|
# Software Foundation; version 2 of the License. #
|
|
|
|
# #
|
|
|
|
# This program is distributed in the hope that it will be useful, but WITHOUT #
|
|
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
|
|
|
|
# more details. #
|
|
|
|
# #
|
|
|
|
# You should have received a copy of the GNU General Public License along #
|
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc., 59 #
|
|
|
|
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
|
|
|
|
###############################################################################
|
|
|
|
"""
|
|
|
|
The :mod:`~scribeengine.config` module contains helper classes for config handling
|
|
|
|
"""
|
|
|
|
from configparser import ConfigParser
|
|
|
|
|
|
|
|
BOOLEAN_VALUES = ['yes', 'true', 'on', 'no', 'false', 'off']
|
|
|
|
|
|
|
|
|
|
|
|
def _fix_special_cases(config):
|
|
|
|
"""
|
|
|
|
Deal with special cases
|
|
|
|
"""
|
|
|
|
if 'THEME_PATHS' in config:
|
|
|
|
config['THEME_PATHS'] = [p.strip() for p in config['THEME_PATHS'].split(',') if p.strip()]
|
|
|
|
|
|
|
|
|
|
|
|
def read_config_from_file(filename):
|
|
|
|
"""
|
|
|
|
Read the Flask configuration from a config file
|
|
|
|
"""
|
|
|
|
flask_config = {}
|
|
|
|
config = ConfigParser()
|
|
|
|
config.read(filename)
|
|
|
|
for section in config.sections():
|
|
|
|
for option in config.options(section):
|
|
|
|
# Get the value, skip it if it is blank
|
|
|
|
string_value = config.get(section, option)
|
|
|
|
if not string_value:
|
|
|
|
continue
|
|
|
|
# Try to figure out what type it is
|
|
|
|
if string_value.isnumeric() and '.' in string_value:
|
|
|
|
value = config.getfloat(section, option)
|
|
|
|
elif string_value.isnumeric():
|
|
|
|
value = config.getint(section, option)
|
|
|
|
elif string_value.lower() in BOOLEAN_VALUES:
|
|
|
|
value = config.getboolean(section, option)
|
|
|
|
else:
|
|
|
|
value = string_value
|
|
|
|
# Set up the configuration key
|
|
|
|
if section == 'flask':
|
|
|
|
# Options in the flask section don't need FLASK_*
|
|
|
|
key = option.upper()
|
|
|
|
else:
|
|
|
|
key = '{}_{}'.format(section, option).upper()
|
|
|
|
# Save this into our flask config dictionary
|
|
|
|
flask_config[key] = value
|
|
|
|
_fix_special_cases(flask_config)
|
|
|
|
return flask_config
|
2021-05-30 06:22:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
def write_config_file(config, filename):
|
|
|
|
"""
|
|
|
|
Write the generated config to a file
|
|
|
|
"""
|
|
|
|
parser = ConfigParser()
|
|
|
|
for section, options in config.items():
|
|
|
|
if not parser.has_section(section):
|
|
|
|
parser.add_section(section)
|
|
|
|
for option, value in options.items():
|
|
|
|
parser.set(section, option, value)
|
|
|
|
with open(filename, "w") as config_file:
|
|
|
|
parser.write(config_file)
|
|
|
|
|
|
|
|
|
|
|
|
def prompt(question, options=None, default=None, type_=str):
|
|
|
|
"""
|
|
|
|
Prompt the user for an answer, returning the default value if none is given.
|
|
|
|
"""
|
|
|
|
if options:
|
|
|
|
question = '{question} ({options})'.format(question=question, options='/'.join(options))
|
|
|
|
if default:
|
|
|
|
question = '{question} [{default}]'.format(question=question, default=default)
|
|
|
|
answer = input('{question}: '.format(question=question)).strip()
|
|
|
|
if not answer:
|
|
|
|
return default
|
|
|
|
elif not isinstance(answer, type_):
|
|
|
|
answer = type_(answer)
|
|
|
|
return answer
|
|
|
|
|
|
|
|
|
|
|
|
def ask_db_questions():
|
|
|
|
db_type = prompt('Database type', ['sqlite', 'mysql', 'postgres'], 'sqlite').lower()
|
|
|
|
if db_type == 'sqlite':
|
|
|
|
db_name = prompt('Database filename', default='scribeengine.sqlite')
|
|
|
|
return '{db_type}:///{db_name}'.format(db_type=db_type, db_name=db_name)
|
|
|
|
db_name = prompt('Database name', default='scribeengine')
|
|
|
|
db_user = prompt('Database user', default='scribeengine')
|
|
|
|
db_password = prompt('Database password', default='mysupersecretpassword')
|
|
|
|
db_host = prompt('Database host', default='localhost')
|
|
|
|
if db_type in ['mysql', 'mariadb']:
|
|
|
|
db_port = prompt('Database port', default=3306, type_=int)
|
|
|
|
elif db_type == 'postgres':
|
|
|
|
db_port = prompt('Database port', default=5432, type_=int)
|
|
|
|
else:
|
|
|
|
db_port = prompt('Database port', default=0, type_=int)
|
|
|
|
return '{db_type}://{db_user}:{db_password}@{db_host}:{db_port}/{db_name}'.format(db_type=db_type,
|
|
|
|
db_user=db_user,
|
|
|
|
db_password=db_password,
|
|
|
|
db_host=db_host,
|
|
|
|
db_port=db_port,
|
|
|
|
db_name=db_name)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
"""
|
|
|
|
Ask the user a set of questions, and then write them to a config file.
|
|
|
|
"""
|
|
|
|
config = {
|
|
|
|
'sqlalchemy': {
|
|
|
|
'database_uri': ask_db_questions()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
write_config_file(config, 'config.ini')
|