Extract command line section into its own submodule
This commit is contained in:
parent
77a7b147ab
commit
9f375207d9
1
.gitignore
vendored
1
.gitignore
vendored
@ -5,3 +5,4 @@ __pycache__
|
|||||||
*.pyd
|
*.pyd
|
||||||
*.sqlite
|
*.sqlite
|
||||||
venv
|
venv
|
||||||
|
config.ini
|
||||||
|
65
scribeengine/config/__init__.py
Normal file
65
scribeengine/config/__init__.py
Normal file
@ -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
|
@ -1,10 +1,10 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- 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 #
|
# 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 #
|
# 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
|
The :mod:`~scribeengine.config` module contains helper classes for config handling
|
||||||
"""
|
"""
|
||||||
|
from argparse import ArgumentParser
|
||||||
from configparser import ConfigParser
|
from configparser import ConfigParser
|
||||||
|
|
||||||
BOOLEAN_VALUES = ['yes', 'true', 'on', 'no', 'false', 'off']
|
|
||||||
|
|
||||||
|
def parse_args():
|
||||||
def _fix_special_cases(config):
|
parser = ArgumentParser()
|
||||||
"""
|
parser.add_argument('-t', '--db-type', metavar="TYPE",
|
||||||
Deal with special cases
|
help='The type of database to use. Options are "sqlite", "mysql" or "postgres". '
|
||||||
"""
|
'Defaults to "sqlite"')
|
||||||
if 'THEME_PATHS' in config:
|
parser.add_argument('-d', '--db-name', metavar='NAME',
|
||||||
config['THEME_PATHS'] = [p.strip() for p in config['THEME_PATHS'].split(',') if p.strip()]
|
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',
|
||||||
def read_config_from_file(filename):
|
help='The hostname or IP address of the database server. Defaults to "localhost"')
|
||||||
"""
|
parser.add_argument('-p', '--db-port', metavar='PORT', type=int,
|
||||||
Read the Flask configuration from a config file
|
help='The database server port. Defaults to 3306 for "mysql" and 5432 for "postgres"')
|
||||||
"""
|
parser.add_argument('-c', '--config', metavar='FILENAME',
|
||||||
flask_config = {}
|
help='The filename to write the configuration to. Defaults to "config.ini"')
|
||||||
config = ConfigParser()
|
parser.add_argument('-n', '--non-interactive', action='store_true',
|
||||||
config.read(filename)
|
help='Run non-interactively. Defaults will be used for arguments not supplied')
|
||||||
for section in config.sections():
|
return parser.parse_args()
|
||||||
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 write_config_file(config, filename):
|
def write_config_file(config, filename):
|
||||||
@ -122,13 +98,14 @@ def ask_db_questions():
|
|||||||
db_name=db_name)
|
db_name=db_name)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def run_cmd():
|
||||||
"""
|
"""
|
||||||
Ask the user a set of questions, and then write them to a config file.
|
Ask the user a set of questions, and then write them to a config file.
|
||||||
"""
|
"""
|
||||||
|
args = parse_args()
|
||||||
config = {
|
config = {
|
||||||
'sqlalchemy': {
|
'sqlalchemy': {
|
||||||
'database_uri': ask_db_questions()
|
'database_uri': ask_db_questions()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
write_config_file(config, 'config.ini')
|
write_config_file(config, args.config or 'config.ini')
|
Reference in New Issue
Block a user