author | Alberto Bertogli
<albertito@blitiri.com.ar> 2022-08-20 19:25:26 UTC |
committer | Alberto Bertogli
<albertito@blitiri.com.ar> 2022-08-20 19:30:49 UTC |
parent | 703adea706a72e28337769c4e8cf905c0a4e975c |
blitiri.cgi | +52 | -54 |
diff --git a/blitiri.cgi b/blitiri.cgi index fcfd37e..3ca329f 100755 --- a/blitiri.cgi +++ b/blitiri.cgi @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 #coding: utf8 # blitiri - A single-file blog engine. @@ -87,7 +87,7 @@ import time import datetime import calendar import zlib -import urllib +import urllib.request, urllib.parse, urllib.error import cgi import re from docutils.core import publish_parts @@ -532,7 +532,7 @@ def validate_rst(rst, secure = True): try: rst_to_html(rst, secure) return None - except SystemMessage, e: + except SystemMessage as e: desc = e.args[0].encode('utf-8') # the error string desc = desc[9:] # remove "<string>:" line = int(desc[:desc.find(':')] or 0) # get the line number @@ -801,7 +801,7 @@ class CommentDB (object): # if comments were enabled after the article was added, we # will need to create the directory if not os.path.exists(self.path): - os.mkdir(self.path, 0777) + os.mkdir(self.path, 0o777) self.comments = [] self.load(article) @@ -978,11 +978,10 @@ class Article (object): def get_tags_links(self): l = [] - tags = list(self.tags) - tags.sort() + tags = sorted(self.tags) for t in tags: l.append('<a class="tag" href="%s/tag/%s">%s</a>' % \ - (blog_url, urllib.quote(t), sanitize(t) )) + (blog_url, urllib.parse.quote(t), sanitize(t) )) return ', '.join(l) @@ -1067,8 +1066,7 @@ class ArticleDB (object): return ml def get_tag_links(self): - tl = list(self.acttags) - tl.sort() + tl = sorted(self.acttags) return [ '<a href="%s/tag/%s">%s</a>' % (blog_url, sanitize(t), sanitize(t)) for t in tl ] @@ -1077,49 +1075,49 @@ class ArticleDB (object): # def render_comments(article, template, form_data): - print '<a name="comments" />' + print('<a name="comments" />') for c in article.comments: if c is None: continue - print template.get_comment_header(c) - print c.to_html() - print template.get_comment_footer(c) + print(template.get_comment_header(c)) + print(c.to_html()) + print(template.get_comment_footer(c)) if not form_data: form_data = CommentFormData() form_data.action = blog_url + '/comment/' + article.uuid + '#comment' captcha = captcha_method(article) - print template.get_comment_form(article, form_data, captcha.puzzle) + print(template.get_comment_form(article, form_data, captcha.puzzle)) def render_html(articles, db, actyear = None, show_comments = False, redirect = None, form_data = None): if redirect: - print 'Status: 303 See Other\r\n', - print 'Location: %s\r\n' % redirect, - print 'Content-type: text/html; charset=utf-8\r\n', - print '\r\n', + print('Status: 303 See Other\r\n', end=' ') + print('Location: %s\r\n' % redirect, end=' ') + print('Content-type: text/html; charset=utf-8\r\n', end=' ') + print('\r\n', end=' ') template = Templates(templates_path, db, actyear) - print template.get_main_header() + print(template.get_main_header()) for a in articles: - print template.get_article_header(a) - print a.to_html() - print template.get_article_footer(a) + print(template.get_article_header(a)) + print(a.to_html()) + print(template.get_article_footer(a)) if show_comments: render_comments(a, template, form_data) - print template.get_main_footer() + print(template.get_main_footer()) def render_artlist(articles, db, actyear = None): template = Templates(templates_path, db, actyear) - print 'Content-type: text/html; charset=utf-8\n' - print template.get_main_header() - print '<h2>Articles</h2>' + print('Content-type: text/html; charset=utf-8\n') + print(template.get_main_header()) + print('<h2>Articles</h2>') for a in articles: - print '<li><a href="%(url)s/post/%(uuid)s">%(title)s</a></li>' \ + print('<li><a href="%(url)s/post/%(uuid)s">%(title)s</a></li>' \ % { 'url': blog_url, 'uuid': a.uuid, 'title': a.title, 'author': a.author, - } - print template.get_main_footer() + }) + print(template.get_main_footer()) def render_atom(articles): if len(articles) > 0: @@ -1127,8 +1125,8 @@ def render_atom(articles): else: updated = datetime.datetime.now().isoformat() - print 'Content-type: application/atom+xml; charset=utf-8\n' - print """<?xml version="1.0" encoding="utf-8"?> + print('Content-type: application/atom+xml; charset=utf-8\n') + print("""<?xml version="1.0" encoding="utf-8"?> <feed xmlns="http://www.w3.org/2005/Atom"> <title>%(title)s</title> @@ -1141,7 +1139,7 @@ def render_atom(articles): 'title': title, 'url': full_url, 'updated': updated, - } + }) for a in articles: vars = a.to_vars() @@ -1149,7 +1147,7 @@ def render_atom(articles): 'url': full_url, 'contents': a.to_html(), } ) - print """ + print(""" <entry> <title>%(arttitle)s</title> <author><name>%(author)s</name></author> @@ -1164,13 +1162,13 @@ def render_atom(articles): </div> </content> </entry> - """ % vars - print "</feed>" + """ % vars) + print("</feed>") def render_style(): - print 'Content-type: text/css\r\n\r\n', - print default_css + print('Content-type: text/css\r\n\r\n', end=' ') + print(default_css) # Get a dictionary with sort() arguments (key and reverse) by parsing the sort # specification format: @@ -1220,7 +1218,7 @@ def handle_cgi(): artlist = False comment = False - if os.environ.has_key('PATH_INFO'): + if 'PATH_INFO' in os.environ: path_info = os.environ['PATH_INFO'] style = path_info == '/style' atom = path_info == '/atom' @@ -1246,7 +1244,7 @@ def handle_cgi(): uuid = uuid.replace('/', '') elif post_preview: art_path = path_info.replace('/preview/post/', '') - art_path = urllib.unquote_plus(art_path) + art_path = urllib.parse.unquote_plus(art_path) art_path = os.path.join(data_path, art_path) art_path = os.path.realpath(art_path) common = os.path.commonprefix([data_path, art_path]) @@ -1256,8 +1254,8 @@ def handle_cgi(): elif tag: t = path_info.replace('/tag/', '') t = t.replace('/', '') - t = urllib.unquote_plus(t) - tags = set((t,)) + t = urllib.parse.unquote_plus(t) + tags = {t} elif comment: uuid = path_info.replace('/comment/', '') uuid = uuid.replace('#comment', '') @@ -1339,7 +1337,7 @@ def handle_cgi(): def usage(): - print 'Usage: %s {add|rm|update} article_path' % sys.argv[0] + print('Usage: %s {add|rm|update} article_path' % sys.argv[0]) def handle_cmd(): if len(sys.argv) != 3: @@ -1350,8 +1348,8 @@ def handle_cmd(): art_path = os.path.realpath(sys.argv[2]) if os.path.commonprefix([data_path, art_path]) != data_path: - print "Error: article (%s) must be inside data_path (%s)" % \ - (art_path, data_path) + print("Error: article (%s) must be inside data_path (%s)" % \ + (art_path, data_path)) return 1 art_path = art_path[len(data_path)+1:] @@ -1365,19 +1363,19 @@ def handle_cmd(): datetime.datetime.now()) for a in db.articles: if a == article: - print 'Error: article already exists' + print('Error: article already exists') return 1 db.articles.append(article) db.save() if enable_comments: comment_dir = os.path.join(comments_path, article.uuid) try: - os.mkdir(comment_dir, 0775) - except OSError, e: + os.mkdir(comment_dir, 0o775) + except OSError as e: if e.errno != errno.EEXIST: - print "Error: can't create comments " \ + print("Error: can't create comments " \ "directory %s (%s)" \ - % (comment_dir, e) + % (comment_dir, e)) # otherwise is probably a removed and re-added # article elif cmd == 'rm': @@ -1386,10 +1384,10 @@ def handle_cmd(): if a == article: break else: - print "Error: no such article" + print("Error: no such article") return 1 if enable_comments: - r = raw_input('Remove comments [y/N]? ') + r = input('Remove comments [y/N]? ') db.articles.remove(a) db.save() if enable_comments and r.lower() == 'y': @@ -1400,7 +1398,7 @@ def handle_cmd(): if a == article: break else: - print "Error: no such article" + print("Error: no such article") return 1 a.updated = datetime.datetime.now() db.save() @@ -1411,11 +1409,11 @@ def handle_cmd(): return 0 -if os.environ.has_key('GATEWAY_INTERFACE'): +if 'GATEWAY_INTERFACE' in os.environ: i = datetime.datetime.now() handle_cgi() f = datetime.datetime.now() - print '<!-- render time: %s -->' % (f-i) + print('<!-- render time: %s -->' % (f-i)) else: sys.exit(handle_cmd())