Mercurial > hg > index.fcgi > www > www-1
view index_app.py @ 48:50de1845520f
add first draft of index_app.py
author | paulo |
---|---|
date | Tue, 12 Mar 2013 01:43:17 -0700 |
parents | |
children | 637a4470363b |
line source
1 import os
2 import stat
3 import datetime
5 import html
8 def _get_cwd(environ):
9 ROOTPATH = "pauloang.com"
10 path_info = environ.get("PATH_INFO", '')
11 return [ROOTPATH] + [i for i in path_info.split('/') if len(i) > 0]
14 def _get_breadcrumb(cwd):
15 h = html.HTML()
16 for (i, d) in enumerate(cwd[:-1]):
17 h.a(d, href='/' + '/'.join(cwd[1:i+1]))
18 h.text('/')
19 h.text(cwd[-1])
20 return h
23 def main(environ):
24 cwd = _get_cwd(environ)
25 dirs = []
27 for i in os.listdir("."):
28 if i[0] != '.' and os.path.isdir(i):
29 statobj = os.stat(i)
30 mtime = datetime.datetime.fromtimestamp(statobj.st_mtime)
31 modeok = (statobj.st_mode & stat.S_IXOTH) and (statobj.st_mode & stat.S_IROTH)
32 dirs.append((i, modeok, mtime))
34 root = html.HTML("html")
35 TITLE_FMT = "-[%s]-"
36 title = TITLE_FMT % '/'.join(cwd)
38 header = root.header
39 header.title(title)
40 header.link(rel="stylesheet", type="text/css", href="index.css")
42 body = root.body
43 body.h1(TITLE_FMT % str(_get_breadcrumb(cwd)), escape=False)
45 body_table = body.table
47 for (i, modeok, mtime) in sorted(dirs, key=lambda x: x[2], reverse=True):
48 if modeok:
49 r = body_table.tr
50 r.td.div(mtime.isoformat(), klass="index_date")
51 r.td.a(i, href=i)
53 return unicode(root).encode("utf-8")
56 def app(environ, start_response):
57 response_body = main(environ)
58 response_headers = [
59 ("Content-Type", "text/html; charset=UTF-8"),
60 ("Content-Length", str(len(response_body))),
61 ]
62 start_response("200 OK", response_headers)
64 return [response_body]