annotate index_app.py @ 49:637a4470363b

adjust index page formatting
author paulo
date Tue, 12 Mar 2013 21:02:29 -0700
parents 50de1845520f
children 0fbe37b56e84
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@48 8 def _get_cwd(environ):
paulo@48 9 ROOTPATH = "pauloang.com"
paulo@48 10 path_info = environ.get("PATH_INFO", '')
paulo@48 11 return [ROOTPATH] + [i for i in path_info.split('/') if len(i) > 0]
paulo@48 12
paulo@48 13
paulo@48 14 def _get_breadcrumb(cwd):
paulo@48 15 h = html.HTML()
paulo@48 16 for (i, d) in enumerate(cwd[:-1]):
paulo@48 17 h.a(d, href='/' + '/'.join(cwd[1:i+1]))
paulo@48 18 h.text('/')
paulo@48 19 h.text(cwd[-1])
paulo@48 20 return h
paulo@48 21
paulo@48 22
paulo@48 23 def main(environ):
paulo@48 24 cwd = _get_cwd(environ)
paulo@48 25 dirs = []
paulo@48 26
paulo@48 27 for i in os.listdir("."):
paulo@48 28 if i[0] != '.' and os.path.isdir(i):
paulo@48 29 statobj = os.stat(i)
paulo@48 30 mtime = datetime.datetime.fromtimestamp(statobj.st_mtime)
paulo@48 31 modeok = (statobj.st_mode & stat.S_IXOTH) and (statobj.st_mode & stat.S_IROTH)
paulo@48 32 dirs.append((i, modeok, mtime))
paulo@48 33
paulo@48 34 root = html.HTML("html")
paulo@48 35 TITLE_FMT = "-[%s]-"
paulo@48 36 title = TITLE_FMT % '/'.join(cwd)
paulo@48 37
paulo@48 38 header = root.header
paulo@48 39 header.title(title)
paulo@48 40 header.link(rel="stylesheet", type="text/css", href="index.css")
paulo@48 41
paulo@48 42 body = root.body
paulo@48 43 body.h1(TITLE_FMT % str(_get_breadcrumb(cwd)), escape=False)
paulo@48 44
paulo@49 45 body_table = body.table(klass="index")
paulo@48 46
paulo@48 47 for (i, modeok, mtime) in sorted(dirs, key=lambda x: x[2], reverse=True):
paulo@48 48 if modeok:
paulo@48 49 r = body_table.tr
paulo@48 50 r.td.div(mtime.isoformat(), klass="index_date")
paulo@48 51 r.td.a(i, href=i)
paulo@48 52
paulo@48 53 return unicode(root).encode("utf-8")
paulo@48 54
paulo@48 55
paulo@48 56 def app(environ, start_response):
paulo@48 57 response_body = main(environ)
paulo@48 58 response_headers = [
paulo@48 59 ("Content-Type", "text/html; charset=UTF-8"),
paulo@48 60 ("Content-Length", str(len(response_body))),
paulo@48 61 ]
paulo@48 62 start_response("200 OK", response_headers)
paulo@48 63
paulo@48 64 return [response_body]