18 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
18 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
19 # |
19 # |
20 |
20 |
21 import os, os.path |
21 import os, os.path |
22 |
22 |
23 import settings, image, utils, helpers |
23 import settings, image, utils, helpers, log |
24 from log import index, render |
|
25 from template import gallery as gallery_tpl |
24 from template import gallery as gallery_tpl |
26 from helpers import url_for_page |
25 from helpers import url_for_page |
27 |
26 |
28 def dirUp (count=1) : |
27 def dirUp (count=1) : |
29 """ |
28 """ |
89 If given, only filenames that are present in the dict will be indexed, and in |
88 If given, only filenames that are present in the dict will be indexed, and in |
90 the case of dirs, the next_filter will be passed on to that Folder's index |
89 the case of dirs, the next_filter will be passed on to that Folder's index |
91 method. |
90 method. |
92 """ |
91 """ |
93 |
92 |
94 index.info("Indexing %s", self.path) |
|
95 |
|
96 if filters : |
93 if filters : |
97 self.filtered = True |
94 self.filtered = True |
98 |
95 |
99 # iterate through listdir |
96 # iterate through listdir |
100 for fname in os.listdir(self.path) : |
97 for fname in os.listdir(self.path) : |
101 # the full filesystem path to it |
98 # the full filesystem path to it |
102 fpath = self.pathFor(fname) |
99 fpath = self.pathFor(fname) |
103 |
100 |
104 # ignore dotfiles |
101 # ignore dotfiles |
105 if fname.startswith('.') : |
102 if fname.startswith('.') : |
106 index.debug("Skipping dotfile %s", fname) |
103 log.debug("Skipping dotfile %s", fname) |
107 continue |
104 continue |
108 |
105 |
109 # apply filters |
106 # apply filters |
110 if filters : |
107 if filters : |
111 if fname in filters : |
108 if fname in filters : |
112 next_filter = filters[fname] |
109 next_filter = filters[fname] |
113 else : |
110 else : |
114 index.debug("Skip `%s' as we have a filter", fname) |
111 log.debug("Skip `%s' as we have a filter", fname) |
115 continue |
112 continue |
116 else : |
113 else : |
117 next_filter = None |
114 next_filter = None |
118 |
115 |
119 # recurse into subdirs, but not thumbs/previews |
116 # recurse into subdirs, but not thumbs/previews |
120 if (os.path.isdir(fpath) |
117 if (os.path.isdir(fpath) |
121 and (fname not in (settings.THUMB_DIR, settings.PREVIEW_DIR)) |
118 and (fname not in (settings.THUMB_DIR, settings.PREVIEW_DIR)) |
122 and (self.parent or fname not in settings.ROOT_IGNORE) |
119 and (self.parent or fname not in settings.ROOT_IGNORE) |
123 ) : |
120 ) : |
124 index.debug("Found subdir %s", fpath) |
121 log.down(fname) |
|
122 |
125 f = Folder(fname, self) |
123 f = Folder(fname, self) |
|
124 |
126 if f.index(next_filter) : # recursion |
125 if f.index(next_filter) : # recursion |
127 # if a subdir is alive, we are alive as well |
126 # if a subdir is alive, we are alive as well |
128 self.subdirs[fname] = f |
127 self.subdirs[fname] = f |
129 self.alive = True |
128 self.alive = True |
130 |
129 |
|
130 log.up() |
|
131 |
131 # handle images |
132 # handle images |
132 elif os.path.isfile(fpath) and utils.isImage(fname) : |
133 elif os.path.isfile(fpath) and utils.isImage(fname) : |
133 index.debug("Found image %s", fname) |
134 log.next(fname) |
134 self.images[fname] = image.Image(self, fname) |
135 self.images[fname] = image.Image(self, fname) |
135 |
136 |
136 # ignore everything else |
137 # ignore everything else |
137 else : |
138 else : |
138 index.debug("Ignoring file %s", fname) |
139 log.debug("Ignoring file %s", fname) |
139 |
140 |
140 # sort and link the images |
141 # sort and link the images |
141 if self.images : |
142 if self.images : |
142 self.alive = True |
143 self.alive = True |
143 |
144 |
238 Render the index.html, Images, and recurse into subdirs |
239 Render the index.html, Images, and recurse into subdirs |
239 """ |
240 """ |
240 |
241 |
241 # ded folders are skipped |
242 # ded folders are skipped |
242 if not self.alive : |
243 if not self.alive : |
243 render.info("Skipping dir %s (no images)", self.path) |
244 # dead, skip, no output |
244 return |
245 return |
245 |
246 |
|
247 index_mtime = utils.mtime(self.pathFor("index.html")) |
|
248 dir_mtime = utils.mtime(self.path) |
|
249 |
246 # 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 |
250 # 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 |
247 if self.filtered : |
251 if self.filtered : |
248 render.warning("Dir `%s' contents were filtered, so we won't render the gallery index again", self.path) |
252 log.warning("Dir `%s' contents were filtered, so we won't render the gallery index again", self.path) |
|
253 |
|
254 elif index_mtime > dir_mtime : |
|
255 # no changes, pass, ignored |
|
256 pass |
249 |
257 |
250 else : |
258 else : |
251 # create the thumb/preview dirs if needed |
259 # create the thumb/preview dirs if needed |
252 for dir in (settings.THUMB_DIR, settings.PREVIEW_DIR) : |
260 for dir in (settings.THUMB_DIR, settings.PREVIEW_DIR) : |
253 path = self.pathFor(dir) |
261 path = self.pathFor(dir) |
254 |
262 |
255 if not os.path.isdir(path) : |
263 if not os.path.isdir(path) : |
256 render.info("Creating dir %s", path) |
264 log.info("mkdir %s", dir) |
257 os.mkdir(path) |
265 os.mkdir(path) |
258 |
266 |
259 # sort the subdirs |
267 # sort the subdirs |
260 subdirs = self.subdirs.values() |
268 subdirs = self.subdirs.values() |
261 subdirs.sort(key=lambda d: d.name) |
269 subdirs.sort(key=lambda d: d.name) |
262 |
270 |
263 render.info("Rendering %s", self.path) |
|
264 |
|
265 # paginate! |
271 # paginate! |
266 images = self.sorted_images |
272 images = self.sorted_images |
267 image_count = len(images) |
273 image_count = len(images) |
268 pages = [] |
274 pages = [] |
269 |
275 |
272 images = images[settings.IMAGE_COUNT:] |
278 images = images[settings.IMAGE_COUNT:] |
273 |
279 |
274 pagination_required = len(pages) > 1 |
280 pagination_required = len(pages) > 1 |
275 |
281 |
276 if pagination_required : |
282 if pagination_required : |
277 render.info("Index split into %d pages of %d images each", len(pages), settings.IMAGE_COUNT) |
283 log.info("%d pages @ %d images", len(pages), settings.IMAGE_COUNT) |
278 elif not pages : |
284 elif not pages : |
279 render.info("Dir with no images, render for subdirs") |
285 log.info("no images, render for subdirs") |
280 pages = [[]] |
286 pages = [[]] |
281 |
287 |
282 for cur_page, images in enumerate(pages) : |
288 for cur_page, images in enumerate(pages) : |
283 if pagination_required and cur_page > 0 : |
289 if pagination_required and cur_page > 0 : |
284 shorturl = "%s/%s" % (self.shorturl_code, cur_page+1) |
290 shorturl = "%s/%s" % (self.shorturl_code, cur_page+1) |
300 description = self.descr, |
306 description = self.descr, |
301 |
307 |
302 shorturl = self.inRoot('s', shorturl), |
308 shorturl = self.inRoot('s', shorturl), |
303 shorturl_code = shorturl, |
309 shorturl_code = shorturl, |
304 ) |
310 ) |
305 |
311 |
306 # render images |
312 # render images |
307 for img in self.images.itervalues() : |
313 image_count = len(self.sorted_images) |
|
314 for i, img in enumerate(self.images.itervalues()) : |
|
315 log.next("[%-4d/%4d] %s", i, image_count, img.name) |
|
316 |
308 img.render() |
317 img.render() |
309 |
318 |
310 # recurse into subdirs |
319 # recurse into subdirs |
311 for dir in self.subdirs.itervalues() : |
320 for dir in self.subdirs.itervalues() : |
|
321 log.down(dir.name) |
|
322 |
312 dir.render() |
323 dir.render() |
313 |
324 |
|
325 log.up() |
|
326 |