simple_blog_cms/sb_config.py
2022-06-29 09:00:03 -04:00

127 lines
5.2 KiB
Python

import os
import configparser
# Prompt for writing of the conf file
def write_conf(err_message, append):
print(err_message)
simple_blog_conf = configparser.ConfigParser()
option_file = os.path.dirname(os.path.realpath(__file__)) + "/.simple-blog.ini"
if append:
conf_changed(simple_blog_conf, option_file)
elif not append:
conf_from_scratch(simple_blog_conf, option_file)
# This configuration section is for updating or changing the configuration.
def conf_changed(simple_blog_conf, option_file):
simple_blog_conf.read(option_file)
report_conf(simple_blog_conf)
done_with_conf = False
while not done_with_conf:
print("1. Update site name.\n" +
"2. Update web root.\n" +
"3. Update sub-folder.\n" +
"4. Update SSL use.\n" +
"5. Report current config.\n" +
"6. Quit configuration.")
conf_action = input("What do you want to change: ")
if conf_action.lower() == 'q':
done_with_conf = True
elif conf_action == '1':
update_site_name(simple_blog_conf, option_file)
elif conf_action == '2':
update_webroot(option_file)
elif conf_action == '3':
update_sub_folder(option_file)
elif conf_action == '4':
update_ssl_use(simple_blog_conf, option_file)
elif conf_action == '5':
report_conf(simple_blog_conf)
elif conf_action == '6':
done_with_conf = True
else:
print("Input not understood.")
simple_blog_conf.read(option_file)
def conf_from_scratch(simple_blog_conf, option_file):
site_name = input("What is the site's name: ")
web_root = str(input("Path of new web-root\nPress enter for default of (/var/www/html/): ") or "/var/www/html")
sub_folder = str(input("Sub-folder for the blog\nPress ENTER to accept the default: ") or "blog")
use_ssl = str(input("Site uses SSL(Y/n): ") or "y")
if use_ssl.lower() == "y":
use_ssl = "True"
else:
use_ssl = "False"
log_file_path = str(input("Full path to keep CMS logs\nPress ENTER to accept the default: ") or os.path.dirname(
os.path.realpath(__file__)) + '/.simple-blog.log')
simple_blog_conf['general'] = {'site_name': site_name, 'web_root': web_root,
'sub_folder': sub_folder, 'use_ssl': use_ssl, 'log_file': log_file_path}
with open(option_file, 'w') as ini_file:
simple_blog_conf.write(ini_file)
# Updates the site name
def update_site_name(simple_blog_conf, option_file):
site_name = input("What is the name of the site: ")
simple_blog_conf['general']['site_name'] = site_name
with open(option_file, 'w') as ini_file:
simple_blog_conf.write(ini_file)
def update_webroot(option_file):
import app_log
default = "/var/www/html/"
simple_blog_conf = configparser.ConfigParser()
simple_blog_conf.read(option_file)
current_webroot = simple_blog_conf['general']['web_root']
print("Currently configured web-root: %s", current_webroot)
current_webroot = str(input("Path of new web-root\nPress enter for default of (" + default + "): ") or default)
simple_blog_conf['general']['web_root'] = current_webroot
with open(option_file, 'w') as ini_file:
simple_blog_conf.write(ini_file)
app_log.info("The web-root has been set to " + current_webroot)
def update_sub_folder(option_file):
import app_log
default = "/blog"
simple_blog_conf = configparser.ConfigParser()
simple_blog_conf.read(option_file)
current_sub_folder = simple_blog_conf['general']['sub_folder']
print("Currently configured web-root: %s", current_sub_folder)
current_sub_folder = str(input("Path of new web-root\nPress enter for default of (" + default + "): ") or default)
simple_blog_conf['general']['sub_folder'] = current_sub_folder
with open(option_file, 'w') as ini_file:
simple_blog_conf.write(ini_file)
app_log.info("The sub-folder has been set to " + current_sub_folder)
def update_ssl_use(simple_blog_conf, option_file):
import app_log
use_ssl = str(input("Site uses SSL(Y/n): ") or "y")
if use_ssl.lower() == "y":
use_ssl = "True"
else:
use_ssl = "False"
simple_blog_conf['general']['use_ssl'] = use_ssl
with open(option_file, 'w') as ini_file:
simple_blog_conf.write(ini_file)
app_log.info("Site uses SSL set to " + use_ssl)
# Print a report of the current configuration.
def report_conf(simple_blog_conf):
if 'general' in simple_blog_conf:
if 'site_name' in simple_blog_conf['general']:
print("The web root path is: " + simple_blog_conf['general']['site_name'])
if 'web_root' in simple_blog_conf['general']:
print("Your configured web-root is: {}".format(simple_blog_conf['general']['web_root']))
if 'sub_folder' in simple_blog_conf['general']:
print("Your configured sub-folder is: {}".format(simple_blog_conf['general']['sub_folder']))
if 'use_ssl' in simple_blog_conf['general']:
print("SSL use is: {}".format(simple_blog_conf['general']['use_ssl']))
else:
print("No configuration found.")