From b32a982b81bcd1f1d3c41414e84ed5cb505d15e8 Mon Sep 17 00:00:00 2001 From: Stefan Ritter Date: Thu, 16 Dec 2010 11:11:41 +0100 Subject: Code cleanup: * Fix blog_url if there is no http:// at the beginning or/and no / at the end * Use %s for string concatenation (improving performance) --- blogthon.cgi | 114 +++++++++++++++++++++++++++++++---------------------------- 1 file changed, 60 insertions(+), 54 deletions(-) (limited to 'blogthon.cgi') diff --git a/blogthon.cgi b/blogthon.cgi index 56a9335..bcb0db1 100755 --- a/blogthon.cgi +++ b/blogthon.cgi @@ -106,14 +106,20 @@ try: except ConfigParser.Error, error: errorpage(str(error)) +if not re.match("^http:\/\/.*$", blog_url): + blog_url = "http://" + blog_url + +if not re.match("^.*\/$", blog_url): + blog_url = blog_url + "/" + if not os.path.exists(entries_dir): - errorpage("Directory \"" + entries_dir + "\" does not exist!") + errorpage("Directory \"%s\" does not exist!" % entries_dir) if not os.path.exists(staticpages_dir): - errorpage("Directory \"" + staticpages_dir + "\" does not exist!") + errorpage("Directory \"%s\" does not exist!" % staticpages_dir) if not os.path.exists(plugins_dir): - errorpage("Directory \"" + plugins_dir + "\" does not exist!") + errorpage("Directory \"%s\" does not exist!" % plugins_dir) if not os.path.exists("linklist"): errorpage("File \"linklist\" does not exist!") @@ -206,7 +212,7 @@ if cname and ctext and ctitle: content = open(comments_file, "w") content.close() except: - errorpage("\"" + entries_dir + "\" isn\'t writable!") + errorpage("\"%s\" isn\'t writable!" % entries_dir) try: content = open(comments_file, "a") @@ -219,7 +225,7 @@ if cname and ctext and ctitle: # Send mail? if not new_comment_mail == "False": - msg = "From: Blogthon\nTo: " + mail_to + "\nSubject: New comment on " + blog_title + "\n\nSomeone wrote a comment to this entry: " + blog_url + "?p=" + ctitle.replace(" ", "-") + msg = "From: Blogthon\nTo: %s\nSubject: New comment on %s\n\nSomeone wrote a comment to this entry: %s?p=%s" % (mail_to, blog_title, blog_url, ctitle.replace(" ", "-")) smtp = SMTP(smtp_host) smtp.starttls() smtp.sendmail(blog_title, mail_to, msg) @@ -267,13 +273,13 @@ if feed_display == "atom": sec = "%(#)02d" % {"#": int(date[5])} document_header("atom") - print "" + print "" % blog_url print tab + "" - print tab*2 + "" + blog_title + "" + print tab*2 + "%s" % blog_title print tab + "" - print tab + "" + blog_title + "" - print tab + "urn:uuid:" + blog_title_md5sum + "" - print tab + "" + str(date[0]) + "-" + month + "-" + day + "T" + hour + ":" + min + ":" + sec + "Z" + print tab + "%s" % blog_title + print tab + "urn:uuid:%s" % blog_title_md5sum + print tab + "%s-%s-%sT%s:%s:%sZ" % (str(date[0]), month, day, hour, min, sec) print "" j = len(entries) if j > 10: j = 10 @@ -282,10 +288,10 @@ if feed_display == "atom": date = entries[i][0] title_md5sum = generate_uuid(title) print tab*2 + "" - print tab*3 + "" + title + "" - print tab*3 + "" - print tab*3 + "urn:uuid:" + title_md5sum + "" - print tab*3 + "" + str(date[0]) + "-" + month + "-" + day + "T" + hour + ":" + min + ":" + sec + "Z" + print tab*3 + "%s" % title + print tab*3 + "" % (blog_url, title) + print tab*3 + "urn:uuid:%s" % title_md5sum + print tab*3 + "%s-%s-%sT%s:%s:%sZ" % (str(date[0]), month, day, hour, min, sec) print tab*3 + "" content = open(str(entries[i][1]), "r") for h in xrange(0, int(feed_preview)): @@ -301,11 +307,11 @@ if feed_display == "atom": elif feed_display == "rss": document_header("rss") print tab + "" - print tab*2 + "" + blog_title + "" - print tab*2 + "" + blog_url + "" - print tab*2 + "" + blog_subtitle + "" + print tab*2 + "%s" % blog_title + print tab*2 + "%s" % blog_url + print tab*2 + "%s" % blog_subtitle date = time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime(time.mktime(entries[0][0]))) - print tab*2 + "" + date + "" + print tab*2 + "%s" % date print "" j = len(entries) if j > 10: j = 10 @@ -313,18 +319,18 @@ elif feed_display == "rss": title = str(entries[i][1]).replace(entries_dir, "", 1).replace("." + entries_suffix, "") date = time.strftime("%a, %d %b %Y %H:%M:%S %z", time.gmtime(time.mktime(entries[i][0]))) print tab*2 + "" - print tab*3 + "" + title + "" - print tab*3 + "" + blog_url + "?p=" + title + "" - print tab*3 + "" + blog_url + "?p=" + title + "" - print tab*3 + "" + date + "" + print tab*3 + "%s" % title + print tab*3 + "%s?p=%s" % (blog_url, title) + print tab*3 + "%s?p=%s" % (blog_url, title) + print tab*3 + "%s" % date content = open(str(entries[i][1]), "r") rss_description= "" for h in xrange(0, int(feed_preview)): line = content.readline().strip() if line: - rss_description = rss_description + line + "
" + rss_description = "%s%s
" % (rss_description, line) content.close() - print tab*3 + "" + print tab*3 + "" % rss_description print tab*2 + "
" print tab + "
" print "" @@ -333,11 +339,11 @@ elif feed_display == "rss": else: document_header("xhtml-strict") print tab + "" - print tab*2 + "" + blog_title + "" + print tab*2 + "%s" % blog_title print tab*2 + "" - print tab*2 + "" - print tab*2 + "" - print tab*2 + "" + print tab*2 + "" % keywords + print tab*2 + "" % blog_title + print tab*2 + "" % (style, style) print tab + "" print tab + "" print "" @@ -350,10 +356,10 @@ else: # Site header print tab*2 + "
" print tab*3 + "
" - print tab*4 + "" + blog_title + "" + print tab*4 + "%s" % blog_title print tab*3 + "
" print tab*3 + "
" - print tab*4 + "" + blog_subtitle + "" + print tab*4 + "%s" % blog_subtitle print tab*3 + "
" print tab*2 + "
" print "" @@ -376,7 +382,7 @@ else: staticpages_list = glob(os.path.join(staticpages_dir, "*")) staticpages_list.sort() print tab*2 + "
" - print tab*3 + "
" + blog_locale[0] + "
" + print tab*3 + "
%s
" % blog_locale[0] print tab*3 + "
" print tab*4 + "
    " for staticpage in staticpages_list: @@ -386,10 +392,10 @@ else: link = header.split(":", 1)[1].strip() else: link = re.sub("\w+?\/", "", staticpage) - link = "?s=" + link + link = "?s=%s" % link file.close() title = re.sub("\w+?\/\d+?-", "", staticpage) - print tab*5 + "
  • " + title + "
  • " + print tab*5 + "
  • %s
  • " % (link, title) print tab*4 + "
" print tab*3 + "
" print tab*3 + "
" @@ -400,14 +406,14 @@ else: if monthlist == "True": olddate = "" print tab*2 + "
" - print tab*3 + "
" + blog_locale[1] + "
" + print tab*3 + "
%s
" % blog_locale[1] print tab*3 + "
" print tab*4 + "
    " for entry in entries: - date = time.strftime("%m%Y", entry[0]) + date = time.strftime("%m%Y", entry[0]) date_display = time.strftime("%h %Y", entry[0]) if not olddate == date: - print tab*5 + "
  • " + date_display + "
  • " + print tab*5 + "
  • %s
  • " % (date, date_display) olddate = date print tab*4 + "
" print tab*3 + "
" @@ -418,7 +424,7 @@ else: # Linklist if linklist == "True": print tab*2 + "
" - print tab*3 + "" + print tab*3 + "" % blog_locale[2] print tab*3 + "