# HG changeset patch
# User paulo
# Date 1368599778 25200
# Node ID f5c2934a1e3f0cda59dc0d1f963fb883e2de3414
# Parent  bf8c59873212e16b441780db155eba224cc713d0
add pics2

diff -r bf8c59873212 -r f5c2934a1e3f pics2/pics_app.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pics2/pics_app.py	Tue May 14 23:36:18 2013 -0700
@@ -0,0 +1,72 @@
+import os
+import re
+import glob
+import traceback
+
+import html
+
+
+_APPROOTPATH = "/pics"
+_GET_PICSDIR_RE = re.compile(r"^.*" + _APPROOTPATH)
+
+def _get_pics_dir(environ):
+	path_info = environ.get("PATH_INFO", '')
+	return _GET_PICSDIR_RE.sub('.', path_info)
+	
+
+def _get_pics_url(environ, dirpath):
+	script_name = environ.get("SCRIPT_NAME", '')
+	return os.path.normpath(os.path.join(os.path.dirname(script_name), dirpath))
+
+
+def main(environ):
+	title = '(None)'
+	is_index = False
+
+	d = _get_pics_dir(environ)
+
+	if os.path.exists(os.path.join(d, "_picsroot")):
+		title = os.path.basename(d)
+	elif d == '/':
+		title = "pics index"
+		is_index = True
+	else:
+		raise IOError("_picsroot not found in directory: " + d)
+	
+	html_root = html.HTML("html")
+
+	html_header = html_root.header
+	html_header.title(title)
+
+	html_body = html_root.body
+	html_body.h1(title)
+
+	if not is_index:
+		num_jpgs = len(glob.glob(os.path.join(d, "thumbs", "*.jpg")))
+		for i in range(num_jpgs):
+			thumbnail_path = os.path.join(d, "thumbs", "%d.jpg" % i)
+			thumbnail_url = _get_pics_url(environ, thumbnail_path)
+			html_body.img(src=thumbnail_url)
+
+	return unicode(html_root).encode("utf-8")
+	
+
+def app(environ, start_response):
+	response_code = "500 Internal Server Error"
+	response_type = "text/plain; charset=UTF-8"
+
+	try:
+		response_body = main(environ)
+		response_code = "200 OK"
+		response_type = "text/html; charset=UTF-8"
+	except:
+		response_body = traceback.format_exc()
+
+	response_headers = [
+		("Content-Type", response_type),
+		("Content-Length", str(len(response_body))),
+	]
+
+	start_response(response_code, response_headers)
+
+	return [response_body]
diff -r bf8c59873212 -r f5c2934a1e3f pics2/pics_test_server.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pics2/pics_test_server.py	Tue May 14 23:36:18 2013 -0700
@@ -0,0 +1,14 @@
+import wsgiref.simple_server 
+import SocketServer
+
+import pics_app
+
+
+class ThreadingWSGIServer(SocketServer.ThreadingMixIn, wsgiref.simple_server.WSGIServer):
+	pass
+
+
+if __name__ == "__main__":
+	httpd = ThreadingWSGIServer(('', 8000), wsgiref.simple_server.WSGIRequestHandler)
+	httpd.set_app(pics_app.app)
+	httpd.serve_forever()