Mercurial > hg > index.fcgi > www > www-1
diff pics2/pics_app.py @ 52:f5c2934a1e3f
add pics2
author | paulo |
---|---|
date | Tue, 14 May 2013 23:36:18 -0700 |
parents | |
children | 0482bffd7d7f |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/pics2/pics_app.py Tue May 14 23:36:18 2013 -0700 1.3 @@ -0,0 +1,72 @@ 1.4 +import os 1.5 +import re 1.6 +import glob 1.7 +import traceback 1.8 + 1.9 +import html 1.10 + 1.11 + 1.12 +_APPROOTPATH = "/pics" 1.13 +_GET_PICSDIR_RE = re.compile(r"^.*" + _APPROOTPATH) 1.14 + 1.15 +def _get_pics_dir(environ): 1.16 + path_info = environ.get("PATH_INFO", '') 1.17 + return _GET_PICSDIR_RE.sub('.', path_info) 1.18 + 1.19 + 1.20 +def _get_pics_url(environ, dirpath): 1.21 + script_name = environ.get("SCRIPT_NAME", '') 1.22 + return os.path.normpath(os.path.join(os.path.dirname(script_name), dirpath)) 1.23 + 1.24 + 1.25 +def main(environ): 1.26 + title = '(None)' 1.27 + is_index = False 1.28 + 1.29 + d = _get_pics_dir(environ) 1.30 + 1.31 + if os.path.exists(os.path.join(d, "_picsroot")): 1.32 + title = os.path.basename(d) 1.33 + elif d == '/': 1.34 + title = "pics index" 1.35 + is_index = True 1.36 + else: 1.37 + raise IOError("_picsroot not found in directory: " + d) 1.38 + 1.39 + html_root = html.HTML("html") 1.40 + 1.41 + html_header = html_root.header 1.42 + html_header.title(title) 1.43 + 1.44 + html_body = html_root.body 1.45 + html_body.h1(title) 1.46 + 1.47 + if not is_index: 1.48 + num_jpgs = len(glob.glob(os.path.join(d, "thumbs", "*.jpg"))) 1.49 + for i in range(num_jpgs): 1.50 + thumbnail_path = os.path.join(d, "thumbs", "%d.jpg" % i) 1.51 + thumbnail_url = _get_pics_url(environ, thumbnail_path) 1.52 + html_body.img(src=thumbnail_url) 1.53 + 1.54 + return unicode(html_root).encode("utf-8") 1.55 + 1.56 + 1.57 +def app(environ, start_response): 1.58 + response_code = "500 Internal Server Error" 1.59 + response_type = "text/plain; charset=UTF-8" 1.60 + 1.61 + try: 1.62 + response_body = main(environ) 1.63 + response_code = "200 OK" 1.64 + response_type = "text/html; charset=UTF-8" 1.65 + except: 1.66 + response_body = traceback.format_exc() 1.67 + 1.68 + response_headers = [ 1.69 + ("Content-Type", response_type), 1.70 + ("Content-Length", str(len(response_body))), 1.71 + ] 1.72 + 1.73 + start_response(response_code, response_headers) 1.74 + 1.75 + return [response_body]