From: Drew Fisher Date: Sun, 2 Oct 2022 03:28:51 +0000 (-0700) Subject: Rewrite in python3 without web.py X-Git-Url: https://git.zarvox.org/shortlog/month?a=commitdiff_plain;h=af35de78d1c7db97c00fb16560888082779550a5;p=shortlog.git Rewrite in python3 without web.py --- diff --git a/shortlog.py b/shortlog.py index f7b62b3..0a41f3f 100755 --- a/shortlog.py +++ b/shortlog.py @@ -1,11 +1,9 @@ -#!/usr/bin/env python2 - -import web -import os +#!/usr/bin/python3 +from collections import namedtuple import datetime +import mimetypes +import os import re -import smtplib -import hashlib import sys from wsgiref.handlers import CGIHandler @@ -16,8 +14,7 @@ import markdoku entriesdir = os.path.join(pwd, "entries") commentsdir = os.path.join(pwd, "comments") templatesdir = os.path.join(pwd, "templates") - -web.config.debug = True +staticsdir = os.path.join(pwd, "static") monthnames = {"01": "January", "02": "February", @@ -32,29 +29,52 @@ monthnames = {"01": "January", "11": "November", "12": "December"} -urls = ( - '/', 'pastweek', - '/week', 'pastweek' , - '/month', 'pastmonth' , - '/year', 'pastyear' , - '/feed', 'feed' , - '/(\d\d\d\d)/(\d\d)/(\d\d)', 'day' , - '/(\d\d\d\d)-(\d\d)-(\d\d)', 'day' , - '/(\d\d\d\d)/(\d\d)', 'month' , - '/(\d\d\d\d)', 'year', - '', 'redir', - ) - -## This is the way to do it in web.py 0.3+ -app = web.application(urls, globals()) - -## Workaround for autoreloading and sessions when using web.config.debug = True -## Note: I don't actually use sessions -#if web.config.get("_session") is None: -# session = web.session.Session(app, web.session.DiskStore('sessions'), initializer={'count': 0}) -# web.config._session = session -#else: -# session = web.config._session +STATUS_CODES = { + 200: "OK", + 302: "Found", + 404: "Not Found", + 500: "Internal Server Error", +} + +class Response: + def __init__(self, status, headers, body): + self.status = status + self.headers = headers + self.body = body + + def send(self, start_response): + status_str = STATUS_CODES[self.status] + resp_line = f"{self.status} {status_str}" + start_response(resp_line, self.headers) + return self.body + +class Route: + def __init__(self, path_re, fn, methods=["GET"], prefix_match=False): + self.path_re = re.compile(path_re) + self.fn = fn + self.methods = methods + self.prefix_match = prefix_match + + def handle(self, match, env): + return self.fn(match, env) + +class Router: + def __init__(self, routes): + self.routes = routes + + def match(self, env): + for route in self.routes: + if env["REQUEST_METHOD"] not in route.methods: + continue + if route.prefix_match: + m = route.path_re.match(env["PATH_INFO"]) + else: + m = route.path_re.fullmatch(env["PATH_INFO"]) + if m: + return (route, m) + return (None, None) + +# Helper functions # read the paragraphs from a file def readFile(filepath): @@ -76,6 +96,10 @@ def loadComment(filepath): #timestamp = datetime.datetime.fromtimestamp(os.stat(filepath).st_mtime).isoformat() return (nameline, emailline, websiteline, commentbody, timestamp, gravatar) +def most_recent_entries_filelist(count): + files = sorted(os.listdir(entriesdir)) + return files[-count:] + def getCommentFiles(date): # Returns a list of paths to comment files associated with date filelist = [x for x in os.listdir(commentsdir) if re.match(date, x)] return filelist @@ -97,7 +121,6 @@ def makeParas(lines): #return map( lambda x: x if "
    " in x or "