comparison 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
comparison
equal deleted inserted replaced
0:877117903642 1:6a94c3b87174
3 import glob 3 import glob
4 import traceback 4 import traceback
5 5
6 import html 6 import html
7 7
8 import logging
9 logging.basicConfig(
10 level=logging.DEBUG,
11 filename="_LOG",
12 format="%(asctime)s %(levelname)-8s %(message)s",
13 )
8 14
9 _APPROOTPATH = "/pics"
10 _GET_PICSDIR_RE = re.compile(r"^.*" + _APPROOTPATH)
11 15
12 def _get_pics_dir(environ):
13 path_info = environ.get("PATH_INFO", '')
14 return _GET_PICSDIR_RE.sub('.', path_info)
15
16 16
17 def _get_pics_url(environ, dirpath): 17 def _get_pics_url(environ, dirpath):
18 script_name = environ.get("SCRIPT_NAME", '') 18 script_name = environ.get("SCRIPT_NAME", '')
19 return os.path.normpath(os.path.join(os.path.dirname(script_name), dirpath)) 19 return os.path.normpath(os.path.join(os.path.dirname(script_name), dirpath))
20 20
21 21
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))
25
26
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]
31
32 return ppi
33
34
35 def _get_standard_html_doc(title):
36 root = html.HTML("html")
37
38 header = root.header
39 header.title(title)
40
41 body = root.body
42 body.h1(title)
43
44 return (root, header, body)
45
46
47 def _numeric_pad_basename(path, maxdigits=20):
48 return os.path.basename(path).zfill(maxdigits)
49
50
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)
55
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)
58
59 return zip(thumb_fns, browse_fns)
60
61
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)
66
67
22 def main(environ): 68 def main(environ):
23 title = '(None)' 69 page_func = None
24 is_index = False
25 70
26 d = _get_pics_dir(environ) 71 logging.debug("environ['PATH_INFO'] = %s" % environ["PATH_INFO"])
72 logging.debug("environ['SCRIPT_NAME'] = %s" % environ["SCRIPT_NAME"])
27 73
28 if os.path.exists(os.path.join(d, "_picsroot")): 74 pi = environ["PATH_INFO"]
29 title = os.path.basename(d) 75 ppi = _parse_path_info(pi)
30 elif d == '/': 76 logging.debug("ppi = %s" % ppi)
31 title = "pics index" 77
32 is_index = True 78 if len(ppi) < 1 or ppi[0] != '':
33 else: 79 raise AssertionError("Parsed path length must start empty: " + pi)
34 raise IOError("_picsroot not found in directory: " + d) 80
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
88
89 if page_func is None:
90 raise RuntimeError("Cannot find path: " + pi)
35 91
36 html_root = html.HTML("html") 92 return unicode(page_func(environ)).encode("utf-8")
93
37 94
38 html_header = html_root.header 95 def page_index(environ):
39 html_header.title(title) 96 (html_root, html_header, html_body) = _get_standard_html_doc("pics index")
40 97
41 html_body = html_root.body 98 return html_root
42 html_body.h1(title)
43 99
44 if not is_index:
45 num_jpgs = len(glob.glob(os.path.join(d, "thumbs", "*.jpg")))
46 for i in range(num_jpgs):
47 thumbnail_path = os.path.join(d, "thumbs", "%d.jpg" % i)
48 thumbnail_url = _get_pics_url(environ, thumbnail_path)
49 html_body.img(src=thumbnail_url)
50 100
51 return unicode(html_root).encode("utf-8") 101 def page_thumbs(environ):
52 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)
105
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)
109
110 return html_root
111
112
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)
118
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]
132
133 break
134
135 if x is None:
136 raise AssertionError
137
138 (html_root, html_header, html_body) = _get_standard_html_doc(u"%s \u2014 %s of %s" % (d, x, len(imgs)))
139
140 browse_img_url = _get_pics_url(environ, img)
141 html_body.p.img(src=browse_img_url)
142
143 logging.debug("imgs_circ = %s" % imgs_circ)
144
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)
150
151 return html_root
152
53 153
54 def app(environ, start_response): 154 def app(environ, start_response):
55 response_code = "500 Internal Server Error" 155 response_code = "500 Internal Server Error"
56 response_type = "text/plain; charset=UTF-8" 156 response_type = "text/plain; charset=UTF-8"
57 157