paulo@52: import os paulo@52: import re paulo@52: import glob paulo@52: import traceback paulo@52: paulo@52: import html paulo@52: paulo@52: paulo@52: _APPROOTPATH = "/pics" paulo@52: _GET_PICSDIR_RE = re.compile(r"^.*" + _APPROOTPATH) paulo@52: paulo@52: def _get_pics_dir(environ): paulo@52: path_info = environ.get("PATH_INFO", '') paulo@52: return _GET_PICSDIR_RE.sub('.', path_info) paulo@52: paulo@52: paulo@52: def _get_pics_url(environ, dirpath): paulo@52: script_name = environ.get("SCRIPT_NAME", '') paulo@52: return os.path.normpath(os.path.join(os.path.dirname(script_name), dirpath)) paulo@52: paulo@52: paulo@52: def main(environ): paulo@52: title = '(None)' paulo@52: is_index = False paulo@52: paulo@52: d = _get_pics_dir(environ) paulo@52: paulo@52: if os.path.exists(os.path.join(d, "_picsroot")): paulo@52: title = os.path.basename(d) paulo@52: elif d == '/': paulo@52: title = "pics index" paulo@52: is_index = True paulo@52: else: paulo@52: raise IOError("_picsroot not found in directory: " + d) paulo@52: paulo@52: html_root = html.HTML("html") paulo@52: paulo@52: html_header = html_root.header paulo@52: html_header.title(title) paulo@52: paulo@52: html_body = html_root.body paulo@52: html_body.h1(title) paulo@52: paulo@52: if not is_index: paulo@52: num_jpgs = len(glob.glob(os.path.join(d, "thumbs", "*.jpg"))) paulo@52: for i in range(num_jpgs): paulo@52: thumbnail_path = os.path.join(d, "thumbs", "%d.jpg" % i) paulo@52: thumbnail_url = _get_pics_url(environ, thumbnail_path) paulo@52: html_body.img(src=thumbnail_url) paulo@52: paulo@52: return unicode(html_root).encode("utf-8") paulo@52: paulo@52: paulo@52: def app(environ, start_response): paulo@52: response_code = "500 Internal Server Error" paulo@52: response_type = "text/plain; charset=UTF-8" paulo@52: paulo@52: try: paulo@52: response_body = main(environ) paulo@52: response_code = "200 OK" paulo@52: response_type = "text/html; charset=UTF-8" paulo@52: except: paulo@52: response_body = traceback.format_exc() paulo@52: paulo@52: response_headers = [ paulo@52: ("Content-Type", response_type), paulo@52: ("Content-Length", str(len(response_body))), paulo@52: ] paulo@52: paulo@52: start_response(response_code, response_headers) paulo@52: paulo@52: return [response_body]