Mercurial > hg > index.fcgi > www > www-1
comparison pics2/pics_app.py @ 52:f5c2934a1e3f
add pics2
author | paulo |
---|---|
date | Tue, 14 May 2013 23:36:18 -0700 |
parents | |
children | 0482bffd7d7f |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:877117903642 |
---|---|
1 import os | |
2 import re | |
3 import glob | |
4 import traceback | |
5 | |
6 import html | |
7 | |
8 | |
9 _APPROOTPATH = "/pics" | |
10 _GET_PICSDIR_RE = re.compile(r"^.*" + _APPROOTPATH) | |
11 | |
12 def _get_pics_dir(environ): | |
13 path_info = environ.get("PATH_INFO", '') | |
14 return _GET_PICSDIR_RE.sub('.', path_info) | |
15 | |
16 | |
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)) | |
20 | |
21 | |
22 def main(environ): | |
23 title = '(None)' | |
24 is_index = False | |
25 | |
26 d = _get_pics_dir(environ) | |
27 | |
28 if os.path.exists(os.path.join(d, "_picsroot")): | |
29 title = os.path.basename(d) | |
30 elif d == '/': | |
31 title = "pics index" | |
32 is_index = True | |
33 else: | |
34 raise IOError("_picsroot not found in directory: " + d) | |
35 | |
36 html_root = html.HTML("html") | |
37 | |
38 html_header = html_root.header | |
39 html_header.title(title) | |
40 | |
41 html_body = html_root.body | |
42 html_body.h1(title) | |
43 | |
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 | |
51 return unicode(html_root).encode("utf-8") | |
52 | |
53 | |
54 def app(environ, start_response): | |
55 response_code = "500 Internal Server Error" | |
56 response_type = "text/plain; charset=UTF-8" | |
57 | |
58 try: | |
59 response_body = main(environ) | |
60 response_code = "200 OK" | |
61 response_type = "text/html; charset=UTF-8" | |
62 except: | |
63 response_body = traceback.format_exc() | |
64 | |
65 response_headers = [ | |
66 ("Content-Type", response_type), | |
67 ("Content-Length", str(len(response_body))), | |
68 ] | |
69 | |
70 start_response(response_code, response_headers) | |
71 | |
72 return [response_body] |