comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:7a74a69b996a
1 import os
2 import stat
3 import datetime
4
5 import html
6
7
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]
12
13
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
21
22
23 def main(environ):
24 cwd = _get_cwd(environ)
25 dirs = []
26
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))
33
34 root = html.HTML("html")
35 TITLE_FMT = "-[%s]-"
36 title = TITLE_FMT % '/'.join(cwd)
37
38 header = root.header
39 header.title(title)
40 header.link(rel="stylesheet", type="text/css", href="index.css")
41
42 body = root.body
43 body.h1(TITLE_FMT % str(_get_breadcrumb(cwd)), escape=False)
44
45 body_table = body.table
46
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)
52
53 return unicode(root).encode("utf-8")
54
55
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)
63
64 return [response_body]