Rewritten config parser code

This commit is contained in:
Stefan Ritter 2010-12-14 16:16:12 +01:00
parent b76a97c26a
commit bc6a0f176a

View File

@ -18,7 +18,7 @@ import sys
import time import time
import locale import locale
import re import re
from ConfigParser import ConfigParser import ConfigParser
from cgi import FieldStorage from cgi import FieldStorage
from smtplib import SMTP from smtplib import SMTP
from hashlib import md5 from hashlib import md5
@ -41,6 +41,7 @@ def errorpage(string):
document_header("xhtml-strict") document_header("xhtml-strict")
print "<head><title>Error!</title></head>" print "<head><title>Error!</title></head>"
print "<body>" print "<body>"
print "<h1>Error!</h1><br /><br />"
print tab + string print tab + string
print "</body>" print "</body>"
print "</html>" print "</html>"
@ -66,9 +67,8 @@ def document_header(string):
print "<?xml version=\"1.0\" encoding=\"utf-8\"?>" print "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
print "<rss version=\"2.0\">" print "<rss version=\"2.0\">"
# Parse configuration # Parse configuration
configuration = ConfigParser() configuration = ConfigParser.SafeConfigParser()
for config in ["../blogthonrc", "../.blogthonrc", "configuration"]: for config in ["../blogthonrc", "../.blogthonrc", "configuration"]:
if os.path.exists(config): if os.path.exists(config):
@ -83,60 +83,40 @@ if not config:
try: try:
blog_title = configuration.get("personal", "blog_title") blog_title = configuration.get("personal", "blog_title")
except:
errorpage("\"blog_title\" is missing in configuration!")
try:
blog_subtitle = configuration.get("personal", "blog_subtitle") blog_subtitle = configuration.get("personal", "blog_subtitle")
except:
errorpage("\"blog_subtitle\" is missing in configuration!")
try:
blog_url = configuration.get("personal", "blog_url") blog_url = configuration.get("personal", "blog_url")
except:
errorpage("\"blog_url\" is missing in configuration!")
try:
keywords = configuration.get("personal", "keywords") keywords = configuration.get("personal", "keywords")
except:
errorpage("\"keywords\" is missing in configuration!")
try:
entries_dir = configuration.get("personal", "entries_dir") entries_dir = configuration.get("personal", "entries_dir")
except: entries_suffix = configuration.get("personal", "entries_suffix")
errorpage("\"entries_dir\" is missing in configuration!") staticpages_dir = configuration.get("personal", "staticpages_dir")
plugins_dir = configuration.get("personal", "plugins_dir")
style = configuration.get("look", "style")
language = configuration.get("look", "language")
entries_per_page = configuration.getint("look", "entries_per_page")
monthlist = configuration.get("look", "monthlist")
staticpages = configuration.get("look", "staticpages")
linklist = configuration.get("look", "linklist")
permalinks = configuration.get("look", "permalinks")
comments = configuration.get("look", "comments")
newest_first = configuration.get("look", "newest_first")
new_comment_mail = configuration.get("smtp", "new_comment_mail")
mail_to = configuration.get("smtp", "mail_to")
smtp_host = configuration.get("smtp", "smtp_host")
feed_preview = configuration.get("feed", "feed_preview")
except ConfigParser.Error as error:
errorpage(str(error))
if not os.path.exists(entries_dir): if not os.path.exists(entries_dir):
errorpage("\"entries_dir\" does not exist!") errorpage("Directory \"" + entries_dir + "\" does not exist!")
try:
entries_suffix = configuration.get("personal", "entries_suffix")
except:
errorpage("\"entries_suffix\" is missing in configuration!")
try:
staticpages_dir = configuration.get("personal", "staticpages_dir")
except:
errorpage("\"staticpages_dir\" is missing in configuration!")
if not os.path.exists(staticpages_dir): if not os.path.exists(staticpages_dir):
errorpage("\"staticpages_dir\" does not exist!") errorpage("Directory \"" + staticpages_dir + "\" does not exist!")
try:
plugins_dir = configuration.get("personal", "plugins_dir")
except:
errorpage("\"plugins_dir\" is missing in configuration!")
if not os.path.exists(plugins_dir): if not os.path.exists(plugins_dir):
errorpage("\"plugins_dir\" does not exist!") errorpage("Directory \"" + plugins_dir + "\" does not exist!")
try: if not os.path.exists("linklist"):
style = configuration.get("look", "style") errorpage("File \"linklist\" does not exist!")
except:
errorpage("\"style\" is missing in configuration!")
try:
language = configuration.get("look", "language")
except:
errorpage("\"language\" is missing in configuration!")
if language == "de": if language == "de":
blog_locale = ("Seiten", "Monate", "Links", "Keine Kommentare", "Kommentare", "Alle Einträge anzeigen...", "Name", "Text", "Absenden") blog_locale = ("Seiten", "Monate", "Links", "Keine Kommentare", "Kommentare", "Alle Einträge anzeigen...", "Name", "Text", "Absenden")
@ -162,63 +142,6 @@ else:
else: else:
locale.setlocale(locale.LC_TIME, None) locale.setlocale(locale.LC_TIME, None)
try:
entries_per_page = configuration.getint("look", "entries_per_page")
except:
errorpage("\"entries_per_page\" is missing in configuration!")
try:
monthlist = configuration.get("look", "monthlist")
except:
errorpage("\"monthlist\" is missing in configuration!")
try:
staticpages = configuration.get("look", "staticpages")
except:
errorpage("\"staticpages\" is missing in configuration!")
try:
linklist = configuration.get("look", "linklist")
except:
errorpage("\"linklist\" is missing in configuration!")
if not os.path.exists("linklist"):
errorpage("\"linklist\" does not exist!")
try:
permalinks = configuration.get("look", "permalinks")
except:
errorpage("\"permalinks\" is missing in configuration!")
try:
comments = configuration.get("look", "comments")
except:
errorpage("\"comments\" is missing in configuration!")
try:
newest_first = configuration.get("look", "newest_first")
except:
errorpage("\"newest_first\" is missing in configuration!")
try:
new_comment_mail = configuration.get("smtp", "new_comment_mail")
except:
errorpage("\"new_comment_mail\" is missing in configuration!")
try:
mail_to = configuration.get("smtp", "mail_to")
except:
errorpage("\"mail_to\" is missing in configuration!")
try:
smtp_host = configuration.get("smtp", "smtp_host")
except:
errorpage("\"smtp_host\" is missing in configuration!")
try:
feed_preview = configuration.get("feed", "feed_preview")
except:
errorpage("\"feed_preview\" is missing or empty in configuration!")
# Read POST Variables # Read POST Variables
action = FieldStorage() action = FieldStorage()
month_display = action.getvalue("m") month_display = action.getvalue("m")