From 9f375207d9f7f9d3529b8f94a806dc9ab191e01b Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Sun, 30 May 2021 00:24:03 -0700 Subject: [PATCH] Extract command line section into its own submodule --- .gitignore | 1 + scribeengine/config/__init__.py | 65 +++++++++++++++++++++ scribeengine/{config.py => config/cmd.py} | 71 ++++++++--------------- 3 files changed, 90 insertions(+), 47 deletions(-) create mode 100644 scribeengine/config/__init__.py rename scribeengine/{config.py => config/cmd.py} (69%) diff --git a/.gitignore b/.gitignore index 0185f33..749b740 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ __pycache__ *.pyd *.sqlite venv +config.ini diff --git a/scribeengine/config/__init__.py b/scribeengine/config/__init__.py new file mode 100644 index 0000000..8b33d32 --- /dev/null +++ b/scribeengine/config/__init__.py @@ -0,0 +1,65 @@ +# -*- 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 configparser import ConfigParser + +from scribeengine.config.cmd import run_cmd + +BOOLEAN_VALUES = ['yes', 'true', 'on', 'no', 'false', 'off'] +LIST_OPTIONS = ['THEME_PATHS', 'UPLOAD_PATHS'] + + +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): + # 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() + # 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) + elif key in LIST_OPTIONS: + value = [p.strip() for p in string_value.split(',') if p.strip()] + else: + value = string_value + # Save this into our flask config dictionary + flask_config[key] = value + return flask_config diff --git a/scribeengine/config.py b/scribeengine/config/cmd.py similarity index 69% rename from scribeengine/config.py rename to scribeengine/config/cmd.py index bc30766..c7392b8 100644 --- a/scribeengine/config.py +++ b/scribeengine/config/cmd.py @@ -1,10 +1,10 @@ # -*- coding: utf-8 -*- -# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4 +# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4 ############################################################################### -# ScribeEngine - Open Source Blog Software # +# ScribeEngine - Open Source Content Management System # # --------------------------------------------------------------------------- # -# Copyright (c) 2010-2017 Raoul Snyman # +# 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 # @@ -22,51 +22,27 @@ """ The :mod:`~scribeengine.config` module contains helper classes for config handling """ +from argparse import ArgumentParser 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 +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): @@ -122,13 +98,14 @@ def ask_db_questions(): db_name=db_name) -def main(): +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, 'config.ini') + write_config_file(config, args.config or 'config.ini')