scribeengine/scribeengine/config/__init__.py

70 lines
3.2 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 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
if __name__ == '__main__':
run_cmd()