commit 9cdb91809828483e69af8359203891dd06287422 Author: 20xd6 <20xd6@airmail.cc> Date: Wed Jun 29 09:00:03 2022 -0400 Initial commit diff --git a/app_log.py b/app_log.py new file mode 100644 index 0000000..1f59ded --- /dev/null +++ b/app_log.py @@ -0,0 +1,23 @@ +import logging +import os +import configparser + +import find_ini + +# Global variables that should maintain consistency across all logging activities. +option_file = find_ini.find_ini() +simple_blog_conf = configparser.ConfigParser() +simple_blog_conf.read(option_file) +if simple_blog_conf['general']['log_file'] != '': + log_file = simple_blog_conf['general']['log_file'] +else: + log_file = os.path.dirname(os.path.realpath(__file__)) + '/.simple-blog.log' +logging.basicConfig(format='%(levelname)s:%(asctime)s:%(message)s', filename=log_file, level=logging.INFO) + + +def info(log_text): + logging.info(log_text) + + +def warning(log_text): + logging.warning(log_text) diff --git a/find_ini.py b/find_ini.py new file mode 100644 index 0000000..593180f --- /dev/null +++ b/find_ini.py @@ -0,0 +1,27 @@ +import configparser +import os + +import sb_config + + +def find_ini(): # Test for the existence of a configuration + conf_found = False + while not conf_found: + try: + option_file = open(os.path.dirname(os.path.realpath(__file__)) + "/.simple-blog.ini", "r") + base_options = option_file.readlines() + if len(base_options) > 0: + option_file_path = os.path.dirname(os.path.realpath(__file__)) + "/.simple-blog.ini" + return option_file_path + else: + sb_config.write_conf("No configuration found in file.", True) + option_file.close() + except IOError: + sb_config.write_conf("No configuration file found.", False) + + +def return_options(): + option_file = find_ini() + simple_blog_conf = configparser.ConfigParser() + simple_blog_conf.read(option_file) + return simple_blog_conf diff --git a/publish_blog.py b/publish_blog.py new file mode 100644 index 0000000..e69de29 diff --git a/sb_config.py b/sb_config.py new file mode 100644 index 0000000..6321e99 --- /dev/null +++ b/sb_config.py @@ -0,0 +1,126 @@ +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.") diff --git a/simple_blog.py b/simple_blog.py new file mode 100755 index 0000000..c060078 --- /dev/null +++ b/simple_blog.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python3 +# The CMS portion of simple_blog a self-hosted blogging platform. +# Version 0.1 +# 03/12/2022 + +# imports +import configparser +import argparse +import sys + +import find_ini +import publish_blog +import sb_config + +# Start of 'main' +script_name = "simple_blog" +script_version = "0.1" +help_epilog = "" +find_ini.find_ini() +cmd_option_set = argparse.ArgumentParser(description='Publish your thoughts with simple_blog.', epilog=help_epilog) +cmd_option_set.add_argument('--conf', '-C', dest='run_conf', action='store_true', help='Run the self configuration ' + 'routine.') +cmd_option_set.add_argument('--report-conf', '-R', dest='report_config', action='store_true', help='Reports the ' + 'current ' + 'configuration.') +cmd_option_set.add_argument('--publish', '-p', dest='publish', action='store_true', help='Publishes an article. If no ' + 'article is explicitly ' + 'provided ' + ' (-a) it will parse the ' + 'current directory for ' + 'Markdown ' + 'files.') +cmd_option_set.add_argument('--article', '-a', dest='article_path', type=str, + help='Path to the Markdown file to be published.') +cmd_option_set.add_argument('--version', '-v', dest='print_version', action='store_true', + help='Print the current version number.') +cmd_options = cmd_option_set.parse_args() + +option_file = find_ini.find_ini() +simple_blog_conf = configparser.ConfigParser() +simple_blog_conf.read(option_file) +# Testing for command line flags. +if cmd_options.run_conf: + sb_config.write_conf("Configuring", True) + sys.exit(0) +elif cmd_options.report_config: + sb_config.report_conf(simple_blog_conf) + sys.exit(0) +elif cmd_options.publish: + if cmd_options.article_path is not None: + publish_blog.publish_path(cmd_options.article_path ) + else: + publish_blog.publish_parse() +elif cmd_options.print_version: + print("Script version: " + script_version) + sys.exit(0) diff --git a/tagging.py b/tagging.py new file mode 100644 index 0000000..6505d0e --- /dev/null +++ b/tagging.py @@ -0,0 +1,7 @@ +import csv + + +def get_tags(): + user_tags = input("Please enter tags for the article separated by commas: ") + csv_reader = csv.reader(user_tags) + article_tags = [tuple(row) for row in csv_reader]