annotate index_app.py @ 61:16bcb4ae6985

add laterlinks2/index.fcgi
author paulo
date Fri, 18 Jul 2014 23:03:53 -0700
parents 637a4470363b
children 259a484f691b
rev   line source
paulo@48 1 import os
paulo@48 2 import stat
paulo@48 3 import datetime
paulo@48 4
paulo@48 5 import html
paulo@48 6
paulo@48 7
paulo@55 8 def read_indexroot(indexroot_fn):
paulo@55 9 ret = ''
paulo@55 10 with open(indexroot_fn) as f:
paulo@55 11 try:
paulo@55 12 ret = f.next().strip()
paulo@55 13 except StopIteration:
paulo@55 14 pass
paulo@55 15 return ret
paulo@48 16
paulo@48 17
paulo@48 18 def main(environ):
paulo@48 19 dirs = []
paulo@48 20
paulo@48 21 for i in os.listdir("."):
paulo@55 22 indexroot_fn = os.path.join(i, "_indexroot")
paulo@55 23 if i[0] != '.' and os.path.isdir(i) and os.path.exists(indexroot_fn):
paulo@48 24 statobj = os.stat(i)
paulo@55 25 if not (statobj.st_mode & stat.S_IXOTH) and (statobj.st_mode & stat.S_IROTH):
paulo@55 26 continue
paulo@55 27 indexroot_statobj = os.stat(indexroot_fn)
paulo@55 28 mtime = datetime.datetime.fromtimestamp(indexroot_statobj.st_mtime)
paulo@55 29 indexroot = read_indexroot(indexroot_fn)
paulo@55 30 dirs.append((i, mtime, indexroot))
paulo@48 31
paulo@48 32 root = html.HTML("html")
paulo@55 33 title = "-[pauloang.com]-"
paulo@48 34
paulo@48 35 header = root.header
paulo@48 36 header.title(title)
paulo@48 37 header.link(rel="stylesheet", type="text/css", href="index.css")
paulo@48 38
paulo@55 39 body = root.body(klass="body")
paulo@55 40 body.h1(title, escape=False)
paulo@48 41
paulo@55 42 for (i, mtime, indexroot) in sorted(dirs, key=lambda x: x[1], reverse=True):
paulo@55 43 body.a(i, href=os.path.join(i, indexroot))
paulo@55 44 body.br
paulo@48 45
paulo@55 46 body.div(klass="email").p.a("pbba13@gmail.com", href="mailto:pbba13@gmail.com")
paulo@55 47
paulo@48 48 return unicode(root).encode("utf-8")
paulo@48 49
paulo@48 50
paulo@48 51 def app(environ, start_response):
paulo@48 52 response_body = main(environ)
paulo@48 53 response_headers = [
paulo@48 54 ("Content-Type", "text/html; charset=UTF-8"),
paulo@48 55 ("Content-Length", str(len(response_body))),
paulo@48 56 ]
paulo@48 57 start_response("200 OK", response_headers)
paulo@48 58
paulo@48 59 return [response_body]