112 lines
5.6 KiB
Python
112 lines
5.6 KiB
Python
# -*- coding: utf-8 -*-
|
|
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
|
|
|
|
###############################################################################
|
|
# ScribeEngine - Open Source Content Management System #
|
|
# --------------------------------------------------------------------------- #
|
|
# Copyright (c) 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 argparse import ArgumentParser
|
|
from configparser import ConfigParser
|
|
|
|
|
|
def parse_args():
|
|
parser = ArgumentParser()
|
|
parser.add_argument('-t', '--db-type', metavar="TYPE",
|
|
help='The type of database to use. Options are "sqlite", "mysql" or "postgres". '
|
|
'Defaults to "sqlite"')
|
|
parser.add_argument('-d', '--db-name', metavar='NAME',
|
|
help='The name of the database. For "sqlite" this will be the filename. '
|
|
'Defaults to "scribeengine" or "scribeengine.sqlite"')
|
|
parser.add_argument('-H', '--db-host', metavar='HOSTNAME',
|
|
help='The hostname or IP address of the database server. Defaults to "localhost"')
|
|
parser.add_argument('-p', '--db-port', metavar='PORT', type=int,
|
|
help='The database server port. Defaults to 3306 for "mysql" and 5432 for "postgres"')
|
|
parser.add_argument('-c', '--config', metavar='FILENAME',
|
|
help='The filename to write the configuration to. Defaults to "config.ini"')
|
|
parser.add_argument('-n', '--non-interactive', action='store_true',
|
|
help='Run non-interactively. Defaults will be used for arguments not supplied')
|
|
return parser.parse_args()
|
|
|
|
|
|
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 run_cmd():
|
|
"""
|
|
Ask the user a set of questions, and then write them to a config file.
|
|
"""
|
|
args = parse_args()
|
|
config = {
|
|
'sqlalchemy': {
|
|
'database_uri': ask_db_questions()
|
|
}
|
|
}
|
|
write_config_file(config, args.config or 'config.ini')
|