view pics2/pics_app.py @ 61:16bcb4ae6985

add laterlinks2/index.fcgi
author paulo
date Fri, 18 Jul 2014 23:03:53 -0700
parents 0249782e231e
children 6318de36e334
line source
1 import os
2 import re
3 import glob
4 import traceback
5 import datetime
6 import random
7 import urlparse
9 import html
11 import logging
12 logging.basicConfig(
13 level=logging.DEBUG,
14 filename="_LOG",
15 format="%(asctime)s %(levelname)-8s %(message)s",
16 )
20 def _is_pics_dir(dirpath):
21 return os.path.exists(os.path.join(dirpath, "_picsroot"))
24 def _get_dir_dt(dirpath):
25 if _is_pics_dir(dirpath):
26 dirpath = os.path.join(dirpath, "_picsroot")
28 return datetime.datetime.fromtimestamp(os.stat(dirpath).st_mtime)
31 def _format_dt(dt):
32 return dt.strftime("%Y-%m-%d")
35 def _parse_path_info(path_info):
36 ppi = path_info.split(os.sep)
37 if len(ppi) > 1 and ppi[-1] == '':
38 del ppi[-1]
40 return ppi
43 def _numeric_pad_basename(path, maxdigits=20):
44 return os.path.basename(path).zfill(maxdigits)
47 def _get_images(d):
48 exts = [".jpg", ".webm"]
50 thumb_fns = glob.glob(os.path.join(d, "thumbs", "*.jpg"))
51 thumb_fns = sorted(thumb_fns, key=_numeric_pad_basename)
52 logging.debug("thumb_fns = %s" % thumb_fns)
54 browse_dir = os.path.join(d, "browse")
55 browse_contents = set(os.listdir(browse_dir))
56 logging.debug("browse_contents = %s" % browse_contents)
58 browse_fns = []
59 for i in thumb_fns:
60 i_basename = os.path.splitext(os.path.basename(i))[0]
61 try:
62 for j in exts:
63 browse_fn_basename = i_basename + j
64 if browse_fn_basename in browse_contents:
65 browse_fns.append(os.path.join(browse_dir, browse_fn_basename))
66 raise StopIteration
67 except StopIteration:
68 pass
69 else:
70 raise RuntimeError("Cannot find browse image for %s" % i)
71 logging.debug("browse_fns = %s" % browse_fns)
73 return zip(thumb_fns, browse_fns)
76 class Main:
77 def _get_pics_url(self, dirpath):
78 script_name = self._environ.get("SCRIPT_NAME", '')
79 return os.path.normpath(os.path.join(os.path.dirname(script_name), dirpath))
82 def _get_app_url(self, dirpath):
83 script_name = self._environ.get("SCRIPT_NAME", '')
84 return os.path.normpath(os.path.join(script_name, dirpath))
87 def _get_standard_html_doc(self, title):
88 root = html.HTML("html")
90 header = root.header
91 header.link(rel="stylesheet", type="text/css", href=self._get_pics_url("index.css"))
92 header.title(title)
94 body = root.body
95 body.h1(title)
97 return (root, header, body)
100 def _go_thumbnail_links_to_browse_imgs_html_body(self, body, t, b, a_args={}, img_args={}):
101 thumb_img_url = self._get_pics_url(t)
102 browse_url = self._get_app_url(b)
104 a = body.a(href=browse_url, **a_args)
105 a.img(src=thumb_img_url, **img_args)
107 body.text(' ')
110 def _go_browse_image_html_body(self, body, img):
111 browse_img_url = self._get_pics_url(img)
113 ext = os.path.splitext(img)[1]
114 if ext == ".webm":
115 body.video(src=browse_img_url, autoplay="true", loop="true")
116 else:
117 body.img(src=browse_img_url)
120 def __init__(self, environ):
121 self._environ = environ
122 self._page_func = None
124 #logging.debug("environ = %s" % (sorted(self._environ.items(), key=lambda x: x[0]),))
125 logging.debug("environ['PATH_INFO'] = %s" % self._environ["PATH_INFO"])
126 logging.debug("environ['SCRIPT_NAME'] = %s" % self._environ["SCRIPT_NAME"])
127 logging.debug("environ['QUERY_STRING'] = %s" % self._environ["QUERY_STRING"])
129 pi = self._environ["PATH_INFO"]
130 ppi = _parse_path_info(pi)
131 logging.debug("ppi = %s" % ppi)
133 if len(ppi) < 1 or ppi[0] != '':
134 raise AssertionError("Parsed path length must start empty: " + pi)
136 if len(ppi) >= 2 and _is_pics_dir(ppi[1]):
137 if len(ppi) == 2:
138 self._page_func = self.page_thumbs
139 elif len(ppi) >= 4 and ppi[2] == "browse" and os.path.exists(os.path.join(*ppi)):
140 self._page_func = self.page_browse
141 elif len(ppi) == 1:
142 self._page_func = self.page_index
144 if self._page_func is None:
145 raise RuntimeError("Cannot find path: " + pi)
148 def page(self):
149 return unicode(self._page_func()).encode("utf-8")
152 def page_index(self):
153 n = 5 # number of thumbnails to display
155 (html_root, html_header, html_body) = self._get_standard_html_doc("Pictures")
157 pics_dirs = []
158 for i in os.listdir('.'):
159 if _is_pics_dir(i):
160 pics_dirs.append((i, _get_dir_dt(i)))
162 pics_dirs.sort(key=lambda x: x[1], reverse=True)
164 for (d, dt) in pics_dirs:
165 html_body.h2.a(d, href=self._get_app_url(d))
166 html_body.h3(_format_dt(dt))
168 imgs = _get_images(d)
169 imgs_idx = [(i, img) for (i, img) in enumerate(imgs)]
171 sampled_imgs_idx = random.sample(imgs_idx, min(len(imgs_idx), n))
172 sampled_imgs_idx.sort(key=lambda x: x[0])
174 html_p = html_body.p
175 for (i, (t, b)) in sampled_imgs_idx:
176 self._go_thumbnail_links_to_browse_imgs_html_body(html_p, t, b)
178 return html_root
181 def page_thumbs(self):
182 ppi = _parse_path_info(self._environ["PATH_INFO"])
183 d = os.path.join(*ppi)
184 (html_root, html_header, html_body) = self._get_standard_html_doc(d)
186 qs = urlparse.parse_qs(self._environ["QUERY_STRING"])
187 from_img = None
188 if "from" in qs:
189 from_img = qs["from"][0]
191 html_p = html_body.p
192 for (t, b) in _get_images(d):
193 if from_img is not None and b == from_img:
194 self._go_thumbnail_links_to_browse_imgs_html_body(html_p, t, b, img_args={"klass":"sel2", "id":"selected"})
195 else:
196 self._go_thumbnail_links_to_browse_imgs_html_body(html_p, t, b)
198 html_body.a("(Other pictures)", href=self._get_app_url(''))
199 return html_root
202 def page_browse(self):
203 ppi = _parse_path_info(self._environ["PATH_INFO"])
204 d = os.path.join(*ppi[:2])
205 imgs = _get_images(d)
206 img = os.path.join(*ppi)
208 # thumbnail preview ribbon
209 w = 7 # must be odd
210 v = w/2
211 imgs_circ = [None] * w
212 x = None
213 n = len(imgs)
214 for (i, (t, b)) in enumerate(imgs):
215 if b == img:
216 x = i + 1
217 imgs_circ[v] = (t, b)
218 for j in range(1, v + 1):
219 if (i + j) < n: imgs_circ[v + j] = imgs[i + j]
220 if (i - j) >= 0: imgs_circ[v - j] = imgs[i - j]
222 break
224 if x is None:
225 raise AssertionError
227 (html_root, html_header, html_body) = self._get_standard_html_doc(u"%s \u2014 %s of %s" % (d, x, len(imgs)))
229 html_header.script('', type="text/javascript", src=self._get_pics_url("np_keys.js"))
231 self._go_browse_image_html_body(html_body.p, img)
233 logging.debug("imgs_circ = %s" % imgs_circ)
235 html_p = html_body.p
236 for (i, img_c) in enumerate(imgs_circ):
237 if img_c is not None:
238 (t, b) = img_c
239 a_args = {}
240 img_args = {}
241 if b == img:
242 a_args = {"id": "up"}
243 img_args = {"klass": "sel"}
244 b = "%s?from=%s#selected" % (d, img)
245 elif i == v + 1:
246 a_args = {"id": "next"}
247 elif i == v - 1:
248 a_args = {"id": "prev"}
250 self._go_thumbnail_links_to_browse_imgs_html_body(html_p, t, b, a_args, img_args)
252 return html_root
255 def app(environ, start_response):
256 response_code = "500 Internal Server Error"
257 response_type = "text/plain; charset=UTF-8"
259 try:
260 response_body = Main(environ).page()
261 response_code = "200 OK"
262 response_type = "text/html; charset=UTF-8"
263 except:
264 response_body = traceback.format_exc()
266 response_headers = [
267 ("Content-Type", response_type),
268 ("Content-Length", str(len(response_body))),
269 ]
271 start_response(response_code, response_headers)
273 return [response_body]