# -*- 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 ALLOWED_CHARS = ascii_letters + digits 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, str(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(args): """ 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 args.get('db_type'): args['db_type'] = 'sqlite' if not args.get('db_name'): if args['db_type'] == 'sqlite': args['db_name'] = 'scribeengine.sqlite' args['db_name'] = 'scribeengine' if not args.get('db_user'): args['db_user'] = 'scribeegine' if not args.get('db_password'): args['db_password'] = _generate_secret(40) if not args.get('db_host'): args['db_host'] = 'localhost' if not args.get('db_port'): if args['db_type'] == 'mysql': args['db_port'] = 3306 elif args['db_type'] == 'postgres': args['db_port'] = 5432 else: args['db_port'] = 0 if args['db_type'] == 'sqlite': return '{db_type}:///{db_name}'.format(**args) return '{db_type}://{db_user}:{db_password}@{db_host}:{db_port}/{db_name}'.format(**args) def ask_db_questions(args): """ 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 args.get('db_type'): args['db_type'] = prompt('Database type', ['sqlite', 'mysql', 'postgres'], 'sqlite').lower() if not args.get('db_name'): if args.get('db_type') == 'sqlite': args['db_name'] = prompt('Database filename', default='scribeengine.sqlite') args['db_name'] = prompt('Database name', default='scribeengine') if not args.get('db_user'): args['db_user'] = prompt('Database user', default='scribeengine') if not args.get('db_password'): args['db_password'] = prompt('Database password', default=_generate_secret(40)) if not args.get('db_host'): args['db_host'] = prompt('Database host', default='localhost') if not args.get('db_port'): if args.get('db_type') in ['mysql', 'mariadb']: args['db_port'] = prompt('Database port', default=3306, type_=int) elif args.get('db_type') == 'postgres': args['db_port'] = prompt('Database port', default=5432, type_=int) else: args['db_port'] = prompt('Database port', default=0, type_=int) return args def run_cmd(): """ Ask the user a set of questions, and then write them to a config file. """ args = vars(parse_args()) if not args.pop('non_interactive', False): ask_db_questions(args) config_file = args.pop('config') or 'config.ini' db_url = build_db_url(args) config = { 'flask': { 'secret_key': _generate_secret() }, 'sqlalchemy': { 'database_uri': db_url, 'track_modifications': False }, 'mail': { }, 'paths': { 'theme_path': '', 'uploads_path': '' } } write_config_file(config, config_file)