# -*- 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 from random import SystemRandom from string import ascii_letters, digits, punctuation ALLOWED_CHARS = ascii_letters + digits + punctuation def _generate_secret(length=32): """ Generate a secret """ return ''.join(SystemRandom().choice(ALLOWED_CHARS) for _ in range(length)) 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 build_db_url(db_type=None, db_name=None, db_user=None, db_password=None, db_host=None, db_port=None): """ Build an SQLAlchemy connection string using the values given, using defaults for omitted values :param db_type: The type of database, "sqlite", "mysql", "postgres" :param db_name: The name of the database :param db_user: The user to connect to the database :param db_password: The password used to authenticate the user :param db_host: The hostname or IP address of the database server :param db_port: The port of the database server ; :return: An SQLAlchemy connection string """ if not db_type: db_type = 'sqlite' if not db_name: if db_type == 'sqlite': return 'sqlite:///scribeengine.sqlite' db_name = 'scribeengine' if not db_user: db_user = 'scribeegine' if not db_password: db_password = _generate_secret(40) if not db_host: db_host = 'localhost' if not db_port: if db_type == 'mysql': db_port = 3306 elif db_type == 'postgres': db_port = 5432 else: db_port = 0 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 ask_db_questions(db_type=None, db_name=None, db_user=None, db_password=None, db_host=None, db_port=None): """ Ask questions about the database in order to build a SQLAlchemy connection string :param db_type: The type of database, "sqlite", "mysql", "postgres" :param db_name: The name of the database :param db_user: The user to connect to the database :param db_password: The password used to authenticate the user :param db_host: The hostname or IP address of the database server :param db_port: The port of the database server :return: An SQLAlchemy connection string """ if not db_type: db_type = prompt('Database type', ['sqlite', 'mysql', 'postgres'], 'sqlite').lower() if not db_name: 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') if not db_user: db_user = prompt('Database user', default='scribeengine') if not db_password: db_password = prompt('Database password', default=_generate_secret(40)) if not db_host: db_host = prompt('Database host', default='localhost') if not db_port: 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() if args.non_interactive: db_url = build_db_url(args.db_type, args.db_name, args.db_user, args.db_password, args.db_host, args.db_port) else: db_url = ask_db_questions(args.db_type, args.db_name, args.db_user, args.db_password, args.db_host, args.db_port) config = { 'flask': { 'secret_key': _generate_secret() }, 'sqlalchemy': { 'database_uri': db_url, 'track_modifications': False }, 'mail': { }, 'paths': { 'theme_path', 'uploads_path' } } write_config_file(config, args.config or 'config.ini')