diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/index_app.py	Tue Mar 12 01:43:17 2013 -0700
     1.3 @@ -0,0 +1,64 @@
     1.4 +import os
     1.5 +import stat
     1.6 +import datetime
     1.7 +
     1.8 +import html
     1.9 +
    1.10 +
    1.11 +def _get_cwd(environ):
    1.12 +	ROOTPATH = "pauloang.com"
    1.13 +	path_info = environ.get("PATH_INFO", '')
    1.14 +	return [ROOTPATH] + [i for i in path_info.split('/') if len(i) > 0]
    1.15 +
    1.16 +
    1.17 +def _get_breadcrumb(cwd):
    1.18 +	h = html.HTML()
    1.19 +	for (i, d) in enumerate(cwd[:-1]):
    1.20 +		h.a(d, href='/' + '/'.join(cwd[1:i+1]))
    1.21 +		h.text('/')
    1.22 +	h.text(cwd[-1])
    1.23 +	return h
    1.24 +
    1.25 +
    1.26 +def main(environ):
    1.27 +	cwd = _get_cwd(environ)
    1.28 +	dirs = []
    1.29 +	
    1.30 +	for i in os.listdir("."):
    1.31 +		if i[0] != '.' and os.path.isdir(i):
    1.32 +			statobj = os.stat(i)
    1.33 +			mtime = datetime.datetime.fromtimestamp(statobj.st_mtime)
    1.34 +			modeok = (statobj.st_mode & stat.S_IXOTH) and (statobj.st_mode & stat.S_IROTH)
    1.35 +			dirs.append((i, modeok, mtime))
    1.36 +	
    1.37 +	root = html.HTML("html")
    1.38 +	TITLE_FMT = "-[%s]-" 
    1.39 +	title = TITLE_FMT % '/'.join(cwd)
    1.40 +	
    1.41 +	header = root.header
    1.42 +	header.title(title)
    1.43 +	header.link(rel="stylesheet", type="text/css", href="index.css")
    1.44 +	
    1.45 +	body = root.body
    1.46 +	body.h1(TITLE_FMT % str(_get_breadcrumb(cwd)), escape=False)
    1.47 +	
    1.48 +	body_table = body.table
    1.49 +	
    1.50 +	for (i, modeok, mtime) in sorted(dirs, key=lambda x: x[2], reverse=True):
    1.51 +		if modeok:
    1.52 +			r = body_table.tr
    1.53 +			r.td.div(mtime.isoformat(), klass="index_date")
    1.54 +			r.td.a(i, href=i)
    1.55 +	
    1.56 +	return unicode(root).encode("utf-8")
    1.57 +
    1.58 +
    1.59 +def app(environ, start_response):
    1.60 +	response_body = main(environ)
    1.61 +	response_headers = [
    1.62 +		("Content-Type", "text/html; charset=UTF-8"),
    1.63 +		("Content-Length", str(len(response_body))),
    1.64 +	]
    1.65 +	start_response("200 OK", response_headers)
    1.66 +
    1.67 +	return [response_body]