author | terom |
Thu, 08 Nov 2007 21:25:01 +0000 | |
changeset 8 | 4e71e1b670bc |
parent 7 | 235ae238f694 |
child 10 | e8143d784b77 |
permissions | -rwxr-xr-x |
1 | 1 |
#!/usr/bin/env python2.4 |
2 |
# |
|
3 |
# DeGAL - A pretty simple web image gallery |
|
4 |
# Copyright (C) 2007 Tero Marttila |
|
5 |
# http://marttila.de/~terom/degal/ |
|
6 |
# |
|
7 |
# This program is free software; you can redistribute it and/or modify |
|
8 |
# it under the terms of the GNU General Public License as published by |
|
9 |
# the Free Software Foundation; either version 2 of the License, or |
|
10 |
# (at your option) any later version. |
|
11 |
# |
|
12 |
# This program is distributed in the hope that it will be useful, |
|
13 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15 |
# GNU General Public License for more details. |
|
16 |
# |
|
17 |
# You should have received a copy of the GNU General Public License |
|
18 |
# along with this program; if not, write to the |
|
19 |
# Free Software Foundation, Inc., |
|
20 |
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
21 |
# |
|
22 |
||
23 |
import os.path, os |
|
24 |
import urllib |
|
25 |
import logging |
|
26 |
import string |
|
27 |
from datetime import datetime |
|
28 |
import struct |
|
29 |
import base64 |
|
30 |
import shelve |
|
31 |
||
32 |
import PIL |
|
33 |
import PIL.Image |
|
34 |
||
35 |
import utils |
|
36 |
||
37 |
__version__ = '0.2' |
|
38 |
||
39 |
TEMPLATE_DIR='templates' |
|
40 |
TEMPLATE_EXT='html' |
|
41 |
||
42 |
logging.basicConfig( |
|
43 |
level=logging.INFO, |
|
44 |
format="%(name)8s %(levelname)8s %(lineno)3d %(message)s", |
|
45 |
||
46 |
) |
|
47 |
||
48 |
tpl = logging.getLogger('tpl') |
|
49 |
index = logging.getLogger('index') |
|
50 |
prepare = logging.getLogger('prepare') |
|
51 |
render = logging.getLogger('render') |
|
52 |
||
53 |
tpl.setLevel(logging.WARNING) |
|
54 |
||
55 |
#for l in (tpl, index, prepare, render) : |
|
56 |
# l.setLevel(logging.DEBUG) |
|
57 |
||
58 |
def readFile (path) : |
|
59 |
fo = open(path, 'r') |
|
60 |
data = fo.read() |
|
61 |
fo.close() |
|
62 |
||
63 |
return data |
|
64 |
||
65 |
class Template (object) : |
|
66 |
GLOBALS = dict( |
|
67 |
VERSION=__version__, |
|
68 |
) |
|
69 |
||
70 |
def __init__ (self, name) : |
|
71 |
self.name = name |
|
72 |
self.path = os.path.join(TEMPLATE_DIR, "%s.%s" % (name, TEMPLATE_EXT)) |
|
73 |
||
74 |
tpl.debug("Template %s at %s", name, self.path) |
|
75 |
||
76 |
self.content = readFile(self.path) |
|
77 |
||
78 |
def render (self, **vars) : |
|
79 |
content = self.content |
|
80 |
||
81 |
vars.update(self.GLOBALS) |
|
82 |
||
83 |
for name, value in vars.iteritems() : |
|
84 |
content = content.replace('<!-- %s -->' % name, str(value)) |
|
85 |
||
86 |
return content |
|
87 |
||
88 |
def renderTo (self, path, **vars) : |
|
89 |
tpl.info("Render %s to %s", self.name, path) |
|
90 |
||
91 |
fo = open(path, 'w') |
|
92 |
fo.write(self.render(**vars)) |
|
93 |
fo.close() |
|
94 |
||
95 |
gallery_tpl = Template('gallery') |
|
96 |
image_tpl = Template('image') |
|
97 |
||
98 |
IMAGE_EXTS = ('jpg', 'jpeg', 'png', 'gif', 'bmp') |
|
99 |
||
100 |
THUMB_DIR = 'thumbs' |
|
101 |
PREVIEW_DIR = 'previews' |
|
102 |
TITLE_FILE = 'title.txt' |
|
103 |
||
104 |
THUMB_GEOM = (160, 120) |
|
105 |
PREVIEW_GEOM = (640, 480) |
|
106 |
||
107 |
DEFAULT_TITLE = 'Image gallery' |
|
108 |
||
5
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
109 |
# how many image/page |
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
110 |
IMAGE_COUNT = 50 |
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
111 |
|
1 | 112 |
def isImage (fname) : |
113 |
""" |
|
114 |
Is the given filename likely to be an image file? |
|
115 |
""" |
|
116 |
||
117 |
fname = fname.lower() |
|
118 |
base, ext = os.path.splitext(fname) |
|
119 |
ext = ext.lstrip('.') |
|
120 |
||
121 |
return ext in IMAGE_EXTS |
|
122 |
||
123 |
def link (url, title) : |
|
124 |
""" |
|
125 |
Returns an <a href=""></a> link tag with the given values |
|
126 |
""" |
|
127 |
||
128 |
return "<a href='%s'>%s</a>" % (urllib.quote(url), title) |
|
129 |
||
130 |
def dirUp (count=1) : |
|
131 |
""" |
|
132 |
Returns a relative path to the directly count levels above the current one |
|
133 |
""" |
|
134 |
||
135 |
if not count : |
|
136 |
return '.' |
|
137 |
||
138 |
return os.path.join(*(['..']*count)) |
|
139 |
||
140 |
def readTitleDescr (path) : |
|
141 |
""" |
|
142 |
Read a title.txt or <imgname>.txt file |
|
143 |
""" |
|
144 |
||
145 |
if os.path.exists(path) : |
|
146 |
content = readFile(path) |
|
147 |
||
148 |
if '---' in content : |
|
149 |
title, descr = content.split('---', 1) |
|
150 |
else : |
|
151 |
title, descr = content, '' |
|
152 |
||
153 |
return title.strip(), descr.strip() |
|
154 |
||
155 |
return None, None |
|
156 |
||
157 |
class Folder (object) : |
|
158 |
def __init__ (self, name='.', parent=None) : |
|
159 |
# the directory name |
|
160 |
self.name = name |
|
161 |
||
162 |
# our parent Folder, or None |
|
163 |
self.parent = parent |
|
164 |
||
165 |
# the path to this dir, as a relative path to the root of the image gallery, always starts with . |
|
166 |
if parent and name : |
|
167 |
self.path = parent.pathFor(name) |
|
168 |
else : |
|
169 |
self.path = name |
|
170 |
||
171 |
# the url-path to the index.html file |
|
172 |
self.html_path = self.path |
|
173 |
||
174 |
# dict of fname -> Folder |
|
175 |
self.subdirs = {} |
|
176 |
||
177 |
# dict of fname -> Image |
|
178 |
self.images = {} |
|
179 |
||
180 |
# our human-friendly title |
|
181 |
self.title = None |
|
182 |
||
183 |
# our long-winded description |
|
184 |
self.descr = '' |
|
185 |
||
186 |
# is this folder non-empty? |
|
187 |
self.alive = None |
|
188 |
||
189 |
# self.images.values(), but sorted by filename |
|
190 |
self.sorted_images = [] |
|
191 |
||
192 |
# the ShortURL key to this dir |
|
193 |
self.shorturl_code = None |
|
194 |
||
195 |
# were we filtered out? |
|
196 |
self.filtered = False |
|
197 |
||
198 |
def pathFor (self, *fnames) : |
|
199 |
""" |
|
200 |
Return a root-relative path to the given path inside this dir |
|
201 |
""" |
|
202 |
return os.path.join(self.path, *fnames) |
|
203 |
||
204 |
def index (self, filters=None) : |
|
205 |
""" |
|
206 |
Look for other dirs and images inside this dir. Filters must be either None, |
|
207 |
whereupon all files will be included, or a dict of {filename -> next_filter}. |
|
208 |
If given, only filenames that are present in the dict will be indexed, and in |
|
209 |
the case of dirs, the next_filter will be passed on to that Folder's index |
|
210 |
method. |
|
211 |
""" |
|
212 |
||
213 |
index.info("Indexing %s", self.path) |
|
214 |
||
215 |
if filters : |
|
216 |
self.filtered = True |
|
217 |
||
218 |
# iterate through listdir |
|
219 |
for fname in os.listdir(self.path) : |
|
220 |
# the full filesystem path to it |
|
221 |
fpath = self.pathFor(fname) |
|
222 |
||
223 |
# ignore dotfiles |
|
224 |
if fname.startswith('.') : |
|
225 |
index.debug("Skipping dotfile %s", fname) |
|
226 |
continue |
|
227 |
||
228 |
# apply filters |
|
229 |
if filters : |
|
230 |
if fname in filters : |
|
231 |
next_filter = filters[fname] |
|
232 |
else : |
|
233 |
index.debug("Skip `%s' as we have a filter", fname) |
|
234 |
continue |
|
235 |
else : |
|
236 |
next_filter = None |
|
237 |
||
238 |
# recurse into subdirs, but not thumbs/previews |
|
239 |
if os.path.isdir(fpath) and fname not in (THUMB_DIR, PREVIEW_DIR) : |
|
240 |
index.debug("Found subdir %s", fpath) |
|
241 |
f = self.subdirs[fname] = Folder(fname, self) |
|
242 |
f.index(next_filter) # recursion |
|
243 |
||
244 |
# handle images |
|
245 |
elif os.path.isfile(fpath) and isImage(fname) : |
|
246 |
index.debug("Found image %s", fname) |
|
247 |
self.images[fname] = Image(self, fname) |
|
248 |
||
249 |
# ignore everything else |
|
250 |
else : |
|
251 |
index.debug("Ignoring file %s", fname) |
|
252 |
||
253 |
def prepare (self) : |
|
254 |
""" |
|
255 |
Prepare the dir, i.e. sort+prepare the images, as well as recurse |
|
256 |
into subdirs |
|
257 |
""" |
|
258 |
||
259 |
prepare.info("Preparing dir %s", self.path) |
|
260 |
||
261 |
# is this folder non-empty? |
|
262 |
alive = False |
|
263 |
||
264 |
# only create thumbs/previews dirs if we have images in here |
|
265 |
if self.images : |
|
266 |
# folder is non-empty |
|
267 |
alive = True |
|
268 |
prepare.info("Have %d images", len(self.images)) |
|
269 |
||
270 |
# create the thumb/preview dirs if needed |
|
271 |
for dir in (THUMB_DIR, PREVIEW_DIR) : |
|
272 |
prepare.debug("Checking for existance of %s dir", dir) |
|
273 |
path = self.pathFor(dir) |
|
274 |
||
275 |
if not os.path.isdir(path) : |
|
276 |
prepare.info("Creating dir %s", path) |
|
277 |
os.mkdir(path) |
|
278 |
||
279 |
# sort the images |
|
280 |
fnames = self.images.keys() |
|
281 |
fnames.sort() |
|
282 |
||
283 |
prev = None |
|
284 |
||
285 |
# link them together and prepare them |
|
286 |
for fname in fnames : |
|
287 |
img = self.images[fname] |
|
288 |
||
289 |
prepare.debug("Linking %s <-> %s", prev, img) |
|
290 |
||
291 |
img.prev = prev |
|
292 |
||
293 |
if prev : |
|
294 |
prev.next = img |
|
295 |
||
296 |
prev = img |
|
297 |
||
298 |
img.prepare() |
|
299 |
||
300 |
# add to the sorted images list |
|
301 |
self.sorted_images.append(img) |
|
302 |
||
303 |
# prepare subdirs |
|
304 |
if self.subdirs : |
|
305 |
prepare.info("Have %d subdirs", len(self.subdirs)) |
|
306 |
||
307 |
# just recurse into them, we're alive if one of them is |
|
308 |
for dir in self.subdirs.itervalues() : |
|
309 |
if dir.prepare() : |
|
310 |
alive = True |
|
311 |
||
312 |
# figure out our title |
|
313 |
title_path = self.pathFor(TITLE_FILE) |
|
314 |
||
315 |
title, descr = readTitleDescr(title_path) |
|
316 |
||
317 |
if title : |
|
318 |
prepare.info("Found title/descr") |
|
319 |
self.title = title |
|
320 |
self.descr = descr |
|
321 |
alive = True |
|
322 |
||
323 |
# default title for the root dir |
|
324 |
elif self.name == '.' : |
|
325 |
self.title = 'Index' |
|
326 |
||
327 |
else : |
|
328 |
self.title = self.name |
|
329 |
||
330 |
prepare.debug("Our title is '%s'", self.title) |
|
331 |
||
332 |
# lol ded |
|
333 |
if not alive : |
|
334 |
prepare.info("Dir %s is not alive", self.path) |
|
335 |
||
336 |
self.alive = alive |
|
337 |
||
338 |
return alive |
|
339 |
||
340 |
def getObjInfo (self) : |
|
341 |
""" |
|
342 |
Metadata for shorturls2.db |
|
343 |
""" |
|
5
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
344 |
return 'dir', self.path, 'index' |
1 | 345 |
|
346 |
def linkTag (self) : |
|
347 |
""" |
|
348 |
A text-link to this dir |
|
349 |
""" |
|
350 |
||
351 |
return link(self.path, self.title) |
|
352 |
||
353 |
def breadcrumb (self) : |
|
354 |
""" |
|
355 |
Returns a [(fname, title)] list of this dir's parent dirs |
|
356 |
""" |
|
357 |
||
358 |
f = self |
|
359 |
b = [] |
|
360 |
d = 0 |
|
361 |
||
362 |
while f : |
|
363 |
b.insert(0, (dirUp(d), f.title)) |
|
364 |
||
365 |
d += 1 |
|
366 |
f = f.parent |
|
367 |
||
368 |
return b |
|
369 |
||
370 |
def inRoot (self, *fnames) : |
|
371 |
""" |
|
372 |
Return a relative URL from this dir to the given path in the root dir |
|
373 |
""" |
|
374 |
||
375 |
c = len(self.path.split('/')) - 1 |
|
376 |
||
377 |
return os.path.join(*((['..']*c) + list(fnames))) |
|
5
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
378 |
|
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
379 |
def _page_fname (self, page) : |
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
380 |
assert page >= 0 |
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
381 |
|
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
382 |
if page > 0 : |
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
383 |
return 'index_%d.html' % page |
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
384 |
else : |
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
385 |
return 'index.html' |
1 | 386 |
|
387 |
def render (self) : |
|
388 |
""" |
|
389 |
Render the index.html, Images, and recurse into subdirs |
|
390 |
""" |
|
391 |
||
392 |
# ded folders are skipped |
|
393 |
if not self.alive : |
|
394 |
render.info("Skipping dir %s", self.path) |
|
395 |
return |
|
396 |
||
397 |
# if this dir's contents were filtered out, then we can't render the index.html, as we aren't aware of all the images in here |
|
398 |
if self.filtered : |
|
399 |
render.warning("Dir `%s' contents were filtered, so we won't render the gallery index again", self.path) |
|
400 |
||
401 |
else : |
|
402 |
# sort the subdirs |
|
403 |
subdirs = self.subdirs.items() |
|
404 |
subdirs.sort() |
|
405 |
||
406 |
# generate the <a href=""></a>'s for the subdirs |
|
407 |
subdir_linkTags = [link(f.name, f.title) for fname, f in subdirs if f.alive] |
|
408 |
||
409 |
# stick them into a list |
|
410 |
if subdir_linkTags : |
|
411 |
directories = "<ul>\n\t<li>%s</li>\n</ul>" % "</li>\n\t<li>".join(subdir_linkTags) |
|
412 |
else : |
|
413 |
directories = '' |
|
414 |
||
415 |
render.info("Rendering %s", self.path) |
|
5
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
416 |
|
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
417 |
# paginate! |
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
418 |
images = self.sorted_images |
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
419 |
image_count = len(images) |
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
420 |
pages = [] |
1 | 421 |
|
5
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
422 |
while images : |
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
423 |
pages.append(images[:IMAGE_COUNT]) |
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
424 |
images = images[IMAGE_COUNT:] |
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
425 |
|
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
426 |
pagination_required = len(pages) > 1 |
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
427 |
|
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
428 |
if pagination_required : |
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
429 |
render.info("Index split into %d pages of %d images each", len(pages), IMAGE_COUNT) |
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
430 |
|
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
431 |
for cur_page, page in enumerate(pages) : |
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
432 |
if pagination_required : |
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
433 |
pagination = "<ul>" # <li>Showing Images %d - %d of %d</li>" % (cur_page*IMAGE_COUNT, cur_page*IMAGE_COUNT+len(page), image_count) |
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
434 |
|
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
435 |
if cur_page > 0 : |
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
436 |
pagination += '<li><a href="%s">« Prev</a></li>' % (self._page_fname(cur_page - 1)) |
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
437 |
else : |
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
438 |
pagination += '<li><span>« Prev</span></li>' |
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
439 |
|
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
440 |
for x in xrange(0, len(pages)) : |
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
441 |
if x == cur_page : |
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
442 |
pagination += '<li><strong>%s</strong></li>' % (x + 1) |
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
443 |
else : |
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
444 |
pagination += '<li><a href="%s">%s</a></li>' % (self._page_fname(x), x+1) |
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
445 |
|
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
446 |
if cur_page < len(pages) - 1 : |
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
447 |
pagination += '<li><a href="%s">Next »</a></li>' % (self._page_fname(cur_page + 1)) |
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
448 |
else : |
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
449 |
pagination += '<li><span>Next »</span></li>' |
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
450 |
|
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
451 |
pagination += "</ul>" |
6
d9d1f8e5f384
fix alignment of teh paginator, and make shorturl dirs better (no index.html + pagination)
terom
parents:
5
diff
changeset
|
452 |
shorturl = "%s/%s" % (self.shorturl_code, cur_page+1) |
5
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
453 |
else : |
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
454 |
pagination = '' |
6
d9d1f8e5f384
fix alignment of teh paginator, and make shorturl dirs better (no index.html + pagination)
terom
parents:
5
diff
changeset
|
455 |
shorturl = self.shorturl_code |
5
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
456 |
|
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
457 |
# render to index.html |
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
458 |
gallery_tpl.renderTo(self.pathFor(self._page_fname(cur_page)), |
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
459 |
STYLE_URL=self.inRoot('style.css'), |
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
460 |
BREADCRUMB=" » ".join([link(u, t) for (u, t) in self.breadcrumb()]), |
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
461 |
TITLE=self.title, |
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
462 |
DIRECTORIES=directories, |
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
463 |
PAGINATION=pagination, |
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
464 |
CONTENT="".join([i.thumbImgTag() for i in page]), |
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
465 |
DESCR=self.descr, |
6
d9d1f8e5f384
fix alignment of teh paginator, and make shorturl dirs better (no index.html + pagination)
terom
parents:
5
diff
changeset
|
466 |
SHORTURL=self.inRoot('s', shorturl), |
d9d1f8e5f384
fix alignment of teh paginator, and make shorturl dirs better (no index.html + pagination)
terom
parents:
5
diff
changeset
|
467 |
SHORTURL_CODE=shorturl, |
5
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
468 |
) |
1 | 469 |
|
470 |
# render images |
|
471 |
for img in self.images.itervalues() : |
|
472 |
img.render() |
|
473 |
||
474 |
# recurse into subdirs |
|
475 |
for dir in self.subdirs.itervalues() : |
|
476 |
dir.render() |
|
477 |
||
478 |
class Image (object) : |
|
479 |
def __init__ (self, dir, name) : |
|
480 |
# the image filename, e.g. DSC3948.JPG |
|
481 |
self.name = name |
|
482 |
||
483 |
# the Folder object that we are in |
|
484 |
self.dir = dir |
|
485 |
||
486 |
# the relative path from the root to us |
|
487 |
self.path = dir.pathFor(name) |
|
488 |
||
489 |
# the basename+ext, e.g. DSCR3948, .JPG |
|
490 |
self.base_name, self.ext = os.path.splitext(name) |
|
491 |
||
492 |
# the root-relative paths to the thumb and preview images |
|
493 |
self.thumb_path = self.dir.pathFor(THUMB_DIR, self.name) |
|
494 |
self.preview_path = self.dir.pathFor(PREVIEW_DIR, self.name) |
|
495 |
||
496 |
# our user-friendly title |
|
497 |
self.title = name |
|
498 |
||
499 |
# our long-winded description |
|
500 |
self.descr = '' |
|
501 |
||
502 |
# the image before and after us, both may be None |
|
503 |
self.prev = self.next = None |
|
504 |
||
505 |
# the name of the .html gallery view thing for this image, *always* self.name + ".html" |
|
506 |
self.html_name = self.name + ".html" |
|
507 |
||
508 |
# the root-relative path to the gallery view |
|
509 |
self.html_path = self.dir.pathFor(self.html_name) |
|
510 |
||
511 |
# |
|
512 |
# Figured out after prepare |
|
513 |
# |
|
514 |
||
515 |
# (w, h) tuple |
|
516 |
self.img_size = None |
|
517 |
||
518 |
# the ShortURL code for this image |
|
519 |
self.shorturl_code = None |
|
520 |
||
521 |
# what to use in the rendered templates, intended to be overridden by subclasses |
|
522 |
self.series_act = "add" |
|
523 |
self.series_verb = "Add to" |
|
524 |
||
525 |
def prepare (self) : |
|
526 |
""" |
|
527 |
Generate the thumbnail/preview views if needed, get the image info, and look for the title |
|
528 |
""" |
|
529 |
||
530 |
prepare.info("Preparing image %s", self.path) |
|
7 | 531 |
|
1 | 532 |
# stat the image file to get the filesize and mtime |
533 |
st = os.stat(self.path) |
|
534 |
||
535 |
self.filesize = st.st_size |
|
536 |
self.timestamp = st.st_mtime |
|
537 |
||
538 |
# open the image in PIL to get image attributes + generate thumbnails |
|
539 |
img = PIL.Image.open(self.path) |
|
540 |
||
541 |
self.img_size = img.size |
|
542 |
||
543 |
for out_path, geom in ((self.thumb_path, THUMB_GEOM), (self.preview_path, PREVIEW_GEOM)) : |
|
544 |
# if it doesn't exist, or it's older than the image itself, generate |
|
545 |
if not (os.path.exists(out_path) and os.stat(out_path).st_mtime > self.timestamp) : |
|
546 |
prepare.info("Create thumbnailed image at %s with geom %s", out_path, geom) |
|
547 |
||
548 |
# XXX: is this the most efficient way to do this? |
|
549 |
out_img = img.copy() |
|
550 |
out_img.thumbnail(geom, resample=True) |
|
551 |
out_img.save(out_path) |
|
552 |
||
553 |
# look for the metadata file |
|
554 |
title_path = self.dir.pathFor(self.base_name + '.txt') |
|
555 |
prepare.debug("Looking for title at %s", title_path) |
|
556 |
||
557 |
title, descr = readTitleDescr(title_path) |
|
558 |
||
559 |
if title : |
|
560 |
self.title = title |
|
561 |
self.descr = descr |
|
562 |
||
563 |
prepare.info("Found title `%s'", self.title) |
|
564 |
||
565 |
def getObjInfo (self) : |
|
566 |
""" |
|
567 |
Metadata for shorturl2.db |
|
568 |
""" |
|
569 |
return 'img', self.dir.path, self.name |
|
570 |
||
571 |
def thumbImgTag (self) : |
|
572 |
""" |
|
573 |
a <a><img /></a> of this image's thumbnail. Path relative to directory we are in |
|
574 |
""" |
|
5
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
575 |
return link(self.html_name, "<img src='%s' alt='%s' title='%s' />" % (os.path.join(THUMB_DIR, self.name), self.descr, self.title)) |
1 | 576 |
|
577 |
def previewImgTag (self) : |
|
578 |
""" |
|
579 |
a <a><img /></a> of this image's preview. Path relative to directory we are in |
|
580 |
""" |
|
5
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
581 |
return link(self.name, "<img src='%s' alt='%s' title='%s' />" % (os.path.join(PREVIEW_DIR, self.name), self.descr, self.title)) |
1 | 582 |
|
583 |
def linkTag (self) : |
|
584 |
""" |
|
585 |
a <a></a> text-link to this image |
|
586 |
""" |
|
587 |
return link(self.html_name, self.title) |
|
588 |
||
589 |
def breadcrumb (self) : |
|
590 |
""" |
|
591 |
Returns a [(fname, title)] list of this image's parents |
|
592 |
""" |
|
593 |
||
594 |
f = self.dir |
|
595 |
b = [(self.html_name, self.title)] |
|
596 |
d = 0 |
|
597 |
||
598 |
while f : |
|
599 |
b.insert(0, (dirUp(d), f.title)) |
|
600 |
||
601 |
d += 1 |
|
602 |
f = f.parent |
|
603 |
||
604 |
return b |
|
605 |
||
606 |
def render (self) : |
|
607 |
""" |
|
608 |
Write out the .html file |
|
609 |
""" |
|
610 |
||
611 |
render.info("Rendering image %s", self.path) |
|
612 |
||
613 |
image_tpl.renderTo(self.html_path, |
|
614 |
STYLE_URL=self.dir.inRoot('style.css'), |
|
615 |
UP_URL=('.'), |
|
616 |
PREV_URL=(self.prev and self.prev.html_name or ''), |
|
617 |
NEXT_URL=(self.next and self.next.html_name or ''), |
|
618 |
FILE=self.name, |
|
619 |
BREADCRUMB=" » ".join([link(u, t) for u, t in self.breadcrumb()]), |
|
620 |
TITLE=self.title, |
|
621 |
PREVIOUS_THUMB=(self.prev and self.prev.thumbImgTag() or ''), |
|
622 |
IMAGE=self.previewImgTag(), |
|
623 |
NEXT_THUMB=(self.next and self.next.thumbImgTag() or ''), |
|
624 |
DESCRIPTION=self.descr, |
|
625 |
IMGSIZE="%dx%d" % self.img_size, |
|
626 |
FILESIZE=fmtFilesize(self.filesize), |
|
627 |
TIMESTAMP=fmtTimestamp(self.timestamp), |
|
628 |
SHORTURL=self.dir.inRoot('s', self.shorturl_code), |
|
629 |
SHORTURL_CODE=self.shorturl_code, |
|
630 |
SERIES_URL=self.dir.inRoot('series/%s/%s' % (self.series_act, self.shorturl_code)), |
|
631 |
SERIES_VERB=self.series_verb, |
|
632 |
) |
|
633 |
||
634 |
def __str__ (self) : |
|
635 |
return "Image `%s' in `%s'" % (self.name, self.dir.path) |
|
636 |
||
637 |
def int2key (id) : |
|
638 |
""" |
|
639 |
Turn an integer into a short-as-possible url-safe string |
|
640 |
""" |
|
641 |
for type in ('B', 'H', 'I') : |
|
642 |
try : |
|
643 |
return base64.b64encode(struct.pack(type, id), '-_').rstrip('=') |
|
644 |
except struct.error : |
|
645 |
continue |
|
646 |
||
647 |
raise Exception("ID overflow: %s" % id) |
|
648 |
||
649 |
def updateShorturlDb (root) : |
|
650 |
""" |
|
651 |
DeGAL <= 0.2 used a simple key => path mapping, but now we use |
|
652 |
something more structured, key => (type, dirpath, fname), where |
|
653 |
||
654 |
type - one of 'img', 'dir' |
|
655 |
dirpath - the path to the directory, e.g. '.', './foobar', './foobar/quux' |
|
656 |
fname - the filename, one of '', 'DSC9839.JPG', 'this.png', etc. |
|
657 |
""" |
|
658 |
||
5
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
659 |
db = shelve.open('shorturls2', 'c', writeback=True) |
1 | 660 |
|
661 |
id = db.get('_id', 1) |
|
662 |
||
663 |
dirqueue = [root] |
|
664 |
||
665 |
# dict of path -> obj |
|
666 |
paths = {} |
|
667 |
||
5
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
668 |
index.info("Processing ShortURLs...") |
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
669 |
|
1 | 670 |
while dirqueue : |
671 |
dir = dirqueue.pop(0) |
|
672 |
||
673 |
dirqueue.extend(dir.subdirs.itervalues()) |
|
674 |
||
675 |
if dir.alive : |
|
676 |
paths[dir.path] = dir |
|
677 |
||
678 |
for img in dir.images.itervalues() : |
|
5
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
679 |
paths[img.path] = img |
1 | 680 |
|
681 |
for key in db.keys() : |
|
682 |
if key.startswith('_') : |
|
683 |
continue |
|
684 |
||
685 |
type, dirpath, fname = db[key] |
|
686 |
||
5
156cdfffef8e
fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents:
1
diff
changeset
|
687 |
path = os.path.join(dirpath, fname).rstrip('/') |
1 | 688 |
|
689 |
try : |
|
690 |
paths.pop(path).shorturl_code = key |
|
691 |
index.debug("Code for `%s' is %s", path, key) |
|
692 |
||
693 |
except KeyError : |
|
694 |
index.debug("Path `%s' in DB does not exist?", path) |
|
695 |
||
696 |
for obj in paths.itervalues() : |
|
697 |
key = int2key(id) |
|
698 |
id += 1 |
|
699 |
||
700 |
index.info("Alloc code `%s' for `%s'", key, obj.html_path) |
|
701 |
||
702 |
obj.shorturl_code = key |
|
703 |
||
704 |
db[key] = obj.getObjInfo() |
|
705 |
||
706 |
db['_id'] = id |
|
707 |
db.close() |
|
708 |
||
709 |
def main (targets=()) : |
|
710 |
root_filter = {} |
|
711 |
||
712 |
for target in targets : |
|
713 |
f = root_filter |
|
714 |
for path_part in target.split('/') : |
|
715 |
if path_part : |
|
716 |
if path_part not in f : |
|
717 |
f[path_part] = {} |
|
718 |
f = f[path_part] |
|
719 |
||
720 |
index.debug('Filter: %s', root_filter) |
|
721 |
||
722 |
root = Folder() |
|
723 |
root.index(root_filter) |
|
724 |
root.prepare() |
|
725 |
updateShorturlDb(root) |
|
726 |
root.render() |
|
727 |
||
728 |
def fmtFilesize (size) : |
|
729 |
return utils.formatbytes(size, forcekb=False, largestonly=True, kiloname='K', meganame='M', bytename='B', nospace=True) |
|
730 |
||
731 |
def fmtTimestamp (ts) : |
|
732 |
return datetime.fromtimestamp(ts).strftime("%Y/%m/%d %H:%M") |
|
733 |
||
734 |
if __name__ == '__main__' : |
|
735 |
from sys import argv |
|
736 |
argv.pop(0) |
|
737 |
||
738 |
main(argv) |
|
739 |