This repository has been archived on 2024-11-20. You can view files and clone it, but cannot push or open issues or pull requests.
old-scribeengine/scribeengine/config.py

70 lines
3.2 KiB
Python

# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
###############################################################################
# ScribeEngine - Open Source Blog Software #
# --------------------------------------------------------------------------- #
# Copyright (c) 2010-2017 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
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