diff pics2/pics_app.py @ 53:0482bffd7d7f

update pics2/pics_app.py with browse page
author paulo
date Sat, 01 Jun 2013 20:43:00 -0700
parents f5c2934a1e3f
children 496714f2fd8c
line diff
     1.1 --- a/pics2/pics_app.py	Tue May 14 23:36:18 2013 -0700
     1.2 +++ b/pics2/pics_app.py	Sat Jun 01 20:43:00 2013 -0700
     1.3 @@ -5,51 +5,151 @@
     1.4  
     1.5  import html
     1.6  
     1.7 +import logging
     1.8 +logging.basicConfig(
     1.9 +	level=logging.DEBUG,
    1.10 +	filename="_LOG",
    1.11 +	format="%(asctime)s %(levelname)-8s %(message)s",
    1.12 +)
    1.13  
    1.14 -_APPROOTPATH = "/pics"
    1.15 -_GET_PICSDIR_RE = re.compile(r"^.*" + _APPROOTPATH)
    1.16  
    1.17 -def _get_pics_dir(environ):
    1.18 -	path_info = environ.get("PATH_INFO", '')
    1.19 -	return _GET_PICSDIR_RE.sub('.', path_info)
    1.20 -	
    1.21  
    1.22  def _get_pics_url(environ, dirpath):
    1.23  	script_name = environ.get("SCRIPT_NAME", '')
    1.24  	return os.path.normpath(os.path.join(os.path.dirname(script_name), dirpath))
    1.25  
    1.26  
    1.27 +def _get_app_url(environ, dirpath):
    1.28 +	script_name = environ.get("SCRIPT_NAME", '')
    1.29 +	return os.path.normpath(os.path.join(script_name, dirpath))
    1.30 +
    1.31 +
    1.32 +def _parse_path_info(path_info):
    1.33 +	ppi = path_info.split(os.sep)
    1.34 +	if len(ppi) > 1 and ppi[-1] == '':
    1.35 +		del ppi[-1]
    1.36 +		
    1.37 +	return ppi
    1.38 +
    1.39 +
    1.40 +def _get_standard_html_doc(title):
    1.41 +	root = html.HTML("html")
    1.42 +
    1.43 +	header = root.header
    1.44 +	header.title(title)
    1.45 +
    1.46 +	body = root.body
    1.47 +	body.h1(title)
    1.48 +
    1.49 +	return (root, header, body)
    1.50 +
    1.51 +
    1.52 +def _numeric_pad_basename(path, maxdigits=20):
    1.53 +	return os.path.basename(path).zfill(maxdigits)
    1.54 +
    1.55 +
    1.56 +def _get_images(d):
    1.57 +	thumb_fns = glob.glob(os.path.join(d, "thumbs", "*.jpg"))
    1.58 +	thumb_fns = sorted(thumb_fns, key=_numeric_pad_basename)
    1.59 +	logging.debug("thumb_fns = %s" % thumb_fns)
    1.60 +
    1.61 +	browse_fns = [os.path.join(d, "browse", os.path.basename(i)) for i in thumb_fns]
    1.62 +	logging.debug("browse_fns = %s" % browse_fns)
    1.63 +
    1.64 +	return zip(thumb_fns, browse_fns)
    1.65 +
    1.66 +
    1.67 +def _go_thumbnail_links_to_browse_imgs_html_body(environ, body, t, b):
    1.68 +	thumb_img_url = _get_pics_url(environ, t)
    1.69 +	browse_url = _get_app_url(environ, b)
    1.70 +	body.a(href=browse_url).img(src=thumb_img_url)
    1.71 +
    1.72 +
    1.73  def main(environ):
    1.74 -	title = '(None)'
    1.75 -	is_index = False
    1.76 +	page_func = None
    1.77  
    1.78 -	d = _get_pics_dir(environ)
    1.79 +	logging.debug("environ['PATH_INFO'] = %s" % environ["PATH_INFO"])
    1.80 +	logging.debug("environ['SCRIPT_NAME'] = %s" % environ["SCRIPT_NAME"])
    1.81  
    1.82 -	if os.path.exists(os.path.join(d, "_picsroot")):
    1.83 -		title = os.path.basename(d)
    1.84 -	elif d == '/':
    1.85 -		title = "pics index"
    1.86 -		is_index = True
    1.87 -	else:
    1.88 -		raise IOError("_picsroot not found in directory: " + d)
    1.89 +	pi = environ["PATH_INFO"]
    1.90 +	ppi = _parse_path_info(pi)
    1.91 +	logging.debug("ppi = %s" % ppi)
    1.92 +
    1.93 +	if len(ppi) < 1 or ppi[0] != '':
    1.94 +		raise AssertionError("Parsed path length must start empty: " + pi)
    1.95 +
    1.96 +	if len(ppi) >= 2 and os.path.exists(os.path.join(ppi[1], "_picsroot")):
    1.97 +		if len(ppi) == 2:
    1.98 +			page_func = page_thumbs
    1.99 +		elif len(ppi) >= 4 and ppi[2] == "browse" and os.path.exists(os.path.join(*ppi)):
   1.100 +			page_func = page_browse
   1.101 +	elif len(ppi) == 1:
   1.102 +		page_func = page_index
   1.103 +
   1.104 +	if page_func is None:
   1.105 +		raise RuntimeError("Cannot find path: " + pi)
   1.106  	
   1.107 -	html_root = html.HTML("html")
   1.108 +	return unicode(page_func(environ)).encode("utf-8")
   1.109 +	
   1.110  
   1.111 -	html_header = html_root.header
   1.112 -	html_header.title(title)
   1.113 +def page_index(environ):
   1.114 +	(html_root, html_header, html_body) = _get_standard_html_doc("pics index")
   1.115  
   1.116 -	html_body = html_root.body
   1.117 -	html_body.h1(title)
   1.118 +	return html_root
   1.119  
   1.120 -	if not is_index:
   1.121 -		num_jpgs = len(glob.glob(os.path.join(d, "thumbs", "*.jpg")))
   1.122 -		for i in range(num_jpgs):
   1.123 -			thumbnail_path = os.path.join(d, "thumbs", "%d.jpg" % i)
   1.124 -			thumbnail_url = _get_pics_url(environ, thumbnail_path)
   1.125 -			html_body.img(src=thumbnail_url)
   1.126  
   1.127 -	return unicode(html_root).encode("utf-8")
   1.128 -	
   1.129 +def page_thumbs(environ):
   1.130 +	ppi = _parse_path_info(environ["PATH_INFO"])
   1.131 +	d = os.path.join(*ppi)
   1.132 +	(html_root, html_header, html_body) = _get_standard_html_doc(d)
   1.133 +
   1.134 +	html_p = html_body.p
   1.135 +	for (t, b) in _get_images(d):
   1.136 +		_go_thumbnail_links_to_browse_imgs_html_body(environ, html_p, t, b)
   1.137 +
   1.138 +	return html_root
   1.139 +
   1.140 +
   1.141 +def page_browse(environ):
   1.142 +	ppi = _parse_path_info(environ["PATH_INFO"])
   1.143 +	d = os.path.join(*ppi[:2])
   1.144 +	imgs = _get_images(d)
   1.145 +	img = os.path.join(*ppi)
   1.146 +
   1.147 +	# thumbnail preview ribbon
   1.148 +	w = 7 # must be odd
   1.149 +	v = w/2
   1.150 +	imgs_circ = [None] * w
   1.151 +	x = None
   1.152 +	n = len(imgs)
   1.153 +	for (i, (t, b)) in enumerate(imgs):
   1.154 +		if b == img:
   1.155 +			x = i + 1
   1.156 +			imgs_circ[v] = (t, b)
   1.157 +			for j in range(1, v + 1):
   1.158 +				if (i + j) < n: imgs_circ[v + j] = imgs[i + j]
   1.159 +				if (i - j) >= 0: imgs_circ[v - j] = imgs[i - j]
   1.160 +					
   1.161 +			break
   1.162 +
   1.163 +	if x is None:
   1.164 +		raise AssertionError
   1.165 +
   1.166 +	(html_root, html_header, html_body) = _get_standard_html_doc(u"%s \u2014 %s of %s" % (d, x, len(imgs)))
   1.167 +
   1.168 +	browse_img_url = _get_pics_url(environ, img)
   1.169 +	html_body.p.img(src=browse_img_url)
   1.170 +
   1.171 +	logging.debug("imgs_circ = %s" % imgs_circ)
   1.172 +
   1.173 +	html_p_ribbon = html_body.p
   1.174 +	for i in imgs_circ:
   1.175 +		if i is not None:
   1.176 +			(t, b) = i
   1.177 +			_go_thumbnail_links_to_browse_imgs_html_body(environ, html_p_ribbon, t, b)
   1.178 +
   1.179 +	return html_root
   1.180 +
   1.181  
   1.182  def app(environ, start_response):
   1.183  	response_code = "500 Internal Server Error"