Mercurial > hg > index.fcgi > www > www-1
view 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 source
1 import os
2 import re
3 import glob
4 import traceback
6 import html
8 import logging
9 logging.basicConfig(
10 level=logging.DEBUG,
11 filename="_LOG",
12 format="%(asctime)s %(levelname)-8s %(message)s",
13 )
17 def _get_pics_url(environ, dirpath):
18 script_name = environ.get("SCRIPT_NAME", '')
19 return os.path.normpath(os.path.join(os.path.dirname(script_name), dirpath))
22 def _get_app_url(environ, dirpath):
23 script_name = environ.get("SCRIPT_NAME", '')
24 return os.path.normpath(os.path.join(script_name, dirpath))
27 def _parse_path_info(path_info):
28 ppi = path_info.split(os.sep)
29 if len(ppi) > 1 and ppi[-1] == '':
30 del ppi[-1]
32 return ppi
35 def _get_standard_html_doc(title):
36 root = html.HTML("html")
38 header = root.header
39 header.title(title)
41 body = root.body
42 body.h1(title)
44 return (root, header, body)
47 def _numeric_pad_basename(path, maxdigits=20):
48 return os.path.basename(path).zfill(maxdigits)
51 def _get_images(d):
52 thumb_fns = glob.glob(os.path.join(d, "thumbs", "*.jpg"))
53 thumb_fns = sorted(thumb_fns, key=_numeric_pad_basename)
54 logging.debug("thumb_fns = %s" % thumb_fns)
56 browse_fns = [os.path.join(d, "browse", os.path.basename(i)) for i in thumb_fns]
57 logging.debug("browse_fns = %s" % browse_fns)
59 return zip(thumb_fns, browse_fns)
62 def _go_thumbnail_links_to_browse_imgs_html_body(environ, body, t, b):
63 thumb_img_url = _get_pics_url(environ, t)
64 browse_url = _get_app_url(environ, b)
65 body.a(href=browse_url).img(src=thumb_img_url)
68 def main(environ):
69 page_func = None
71 logging.debug("environ['PATH_INFO'] = %s" % environ["PATH_INFO"])
72 logging.debug("environ['SCRIPT_NAME'] = %s" % environ["SCRIPT_NAME"])
74 pi = environ["PATH_INFO"]
75 ppi = _parse_path_info(pi)
76 logging.debug("ppi = %s" % ppi)
78 if len(ppi) < 1 or ppi[0] != '':
79 raise AssertionError("Parsed path length must start empty: " + pi)
81 if len(ppi) >= 2 and os.path.exists(os.path.join(ppi[1], "_picsroot")):
82 if len(ppi) == 2:
83 page_func = page_thumbs
84 elif len(ppi) >= 4 and ppi[2] == "browse" and os.path.exists(os.path.join(*ppi)):
85 page_func = page_browse
86 elif len(ppi) == 1:
87 page_func = page_index
89 if page_func is None:
90 raise RuntimeError("Cannot find path: " + pi)
92 return unicode(page_func(environ)).encode("utf-8")
95 def page_index(environ):
96 (html_root, html_header, html_body) = _get_standard_html_doc("pics index")
98 return html_root
101 def page_thumbs(environ):
102 ppi = _parse_path_info(environ["PATH_INFO"])
103 d = os.path.join(*ppi)
104 (html_root, html_header, html_body) = _get_standard_html_doc(d)
106 html_p = html_body.p
107 for (t, b) in _get_images(d):
108 _go_thumbnail_links_to_browse_imgs_html_body(environ, html_p, t, b)
110 return html_root
113 def page_browse(environ):
114 ppi = _parse_path_info(environ["PATH_INFO"])
115 d = os.path.join(*ppi[:2])
116 imgs = _get_images(d)
117 img = os.path.join(*ppi)
119 # thumbnail preview ribbon
120 w = 7 # must be odd
121 v = w/2
122 imgs_circ = [None] * w
123 x = None
124 n = len(imgs)
125 for (i, (t, b)) in enumerate(imgs):
126 if b == img:
127 x = i + 1
128 imgs_circ[v] = (t, b)
129 for j in range(1, v + 1):
130 if (i + j) < n: imgs_circ[v + j] = imgs[i + j]
131 if (i - j) >= 0: imgs_circ[v - j] = imgs[i - j]
133 break
135 if x is None:
136 raise AssertionError
138 (html_root, html_header, html_body) = _get_standard_html_doc(u"%s \u2014 %s of %s" % (d, x, len(imgs)))
140 browse_img_url = _get_pics_url(environ, img)
141 html_body.p.img(src=browse_img_url)
143 logging.debug("imgs_circ = %s" % imgs_circ)
145 html_p_ribbon = html_body.p
146 for i in imgs_circ:
147 if i is not None:
148 (t, b) = i
149 _go_thumbnail_links_to_browse_imgs_html_body(environ, html_p_ribbon, t, b)
151 return html_root
154 def app(environ, start_response):
155 response_code = "500 Internal Server Error"
156 response_type = "text/plain; charset=UTF-8"
158 try:
159 response_body = main(environ)
160 response_code = "200 OK"
161 response_type = "text/html; charset=UTF-8"
162 except:
163 response_body = traceback.format_exc()
165 response_headers = [
166 ("Content-Type", response_type),
167 ("Content-Length", str(len(response_body))),
168 ]
170 start_response(response_code, response_headers)
172 return [response_body]