# HG changeset patch # User paulo # Date 1363077797 25200 # Node ID 50de1845520fa54d23c1258a254e12f36695c625 # Parent 315afeb47e52015a4f1c9fdda086fd2ae379f0ec add first draft of index_app.py diff -r 315afeb47e52 -r 50de1845520f index_app.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/index_app.py Tue Mar 12 01:43:17 2013 -0700 @@ -0,0 +1,64 @@ +import os +import stat +import datetime + +import html + + +def _get_cwd(environ): + ROOTPATH = "pauloang.com" + path_info = environ.get("PATH_INFO", '') + return [ROOTPATH] + [i for i in path_info.split('/') if len(i) > 0] + + +def _get_breadcrumb(cwd): + h = html.HTML() + for (i, d) in enumerate(cwd[:-1]): + h.a(d, href='/' + '/'.join(cwd[1:i+1])) + h.text('/') + h.text(cwd[-1]) + return h + + +def main(environ): + cwd = _get_cwd(environ) + dirs = [] + + for i in os.listdir("."): + if i[0] != '.' and os.path.isdir(i): + statobj = os.stat(i) + mtime = datetime.datetime.fromtimestamp(statobj.st_mtime) + modeok = (statobj.st_mode & stat.S_IXOTH) and (statobj.st_mode & stat.S_IROTH) + dirs.append((i, modeok, mtime)) + + root = html.HTML("html") + TITLE_FMT = "-[%s]-" + title = TITLE_FMT % '/'.join(cwd) + + header = root.header + header.title(title) + header.link(rel="stylesheet", type="text/css", href="index.css") + + body = root.body + body.h1(TITLE_FMT % str(_get_breadcrumb(cwd)), escape=False) + + body_table = body.table + + for (i, modeok, mtime) in sorted(dirs, key=lambda x: x[2], reverse=True): + if modeok: + r = body_table.tr + r.td.div(mtime.isoformat(), klass="index_date") + r.td.a(i, href=i) + + return unicode(root).encode("utf-8") + + +def app(environ, start_response): + response_body = main(environ) + response_headers = [ + ("Content-Type", "text/html; charset=UTF-8"), + ("Content-Length", str(len(response_body))), + ] + start_response("200 OK", response_headers) + + return [response_body] diff -r 315afeb47e52 -r 50de1845520f index_test_server.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/index_test_server.py Tue Mar 12 01:43:17 2013 -0700 @@ -0,0 +1,14 @@ +import wsgiref.simple_server +import SocketServer + +import index_app + + +class ThreadingWSGIServer(SocketServer.ThreadingMixIn, wsgiref.simple_server.WSGIServer): + pass + + +if __name__ == "__main__": + httpd = ThreadingWSGIServer(('', 8000), wsgiref.simple_server.WSGIRequestHandler) + httpd.set_app(index_app.app) + httpd.serve_forever()