clear up commands.main logic a bit
authorTero Marttila <terom@fixme.fi>
Wed, 17 Jun 2009 17:56:02 +0300
changeset 132 c2b2f4b6fe6d
parent 131 7021d949222c
child 133 0c1c092bbc5d
clear up commands.main logic a bit
degal/commands/main.py
degal/folder.py
--- a/degal/commands/main.py	Wed Jun 17 17:02:24 2009 +0300
+++ b/degal/commands/main.py	Wed Jun 17 17:56:02 2009 +0300
@@ -4,13 +4,14 @@
 
 def render_image_html (ctx, image) :
     """
-        Render the thumbnails and .html for one image
+        Render and write out the static .html file for the give image.
     """
 
     ctx.log_debug("%s", image.html)
 
-    # render output
+    # render full-xhtml-document
     tpl = templates.master(ctx.gallery, image, 
+        # with content
         templates.image_page(image)
     )
     
@@ -19,9 +20,10 @@
 
 def update_image_thumbs (image) :
     """
-        Render the thubmnails for the given image, returning the image.
+        Update/render and write out the thumbnails (thumb+preview) for the given image, returning the image object
+        itself.
 
-        This /should/ be threadsafe.
+        This /should/ be threadsafe. XXX: but it probably isn't entirely.
     """
     
     # this will unconditionally update the image
@@ -29,28 +31,23 @@
 
     return image
 
-def render_folder_images (ctx, images, for_update=True) :
+def render_folder_images (ctx, images) :
     """
-        Render the given series of images
+        Render the given series of images (html+thumbnails) as required based on the settings.
+
+        This is capable of rendering the given set of images in parallel.
     """
 
-    # XXX: handle this thumb-update/html-update stuff better
-
-    # render all HTML
+    # first, update HTML
     for image in images :
-        render_image_html(ctx, image)
-
+        if ctx.config.force_html or image.stale() :
+            render_image_html(ctx, image)
     
-    # render the thumbnails concurrently
-    for image in ctx.concurrent.execute(
-        task(update_image_thumbs, image) 
-
-            for image in images 
-
-            # only test if not already filtered for update
-            # XXX: externalize logic
-            if for_update or ctx.config.force_thumb or image.stale()
-    ) :
+    # define the render-tasks
+    tasks = (task(update_image_thumbs, image) for image in images if ctx.config.force_thumb or image.stale())
+    
+    # render the thumbnails themselves concurrently, returning the rendered Image objects
+    for image in ctx.concurrent.execute(tasks) :
         # log image path
         ctx.log_info("%s", image)
 
@@ -59,18 +56,22 @@
 
 def render_folder_html (ctx, folder) :
     """
-        Render the .html output for one folder
+        Render and write out the required static .html files for the given folder.
+
+        This will paginate large numbers of images, handle Folders with only subfolders within as a single page, and as
+        a bonus, will not render anything for (non-recursively) empty folders.
     """
 
     # render each page separately
     for page in xrange(folder.page_count) :
-        # output .html page
+        # output .html path
         html = folder.html_page(page)
     
         ctx.log_debug("%s", html)
         
-        # render template
+        # render full-html template
         tpl = templates.master(ctx.gallery, folder,
+            # content
             templates.folder_page(folder, page)
         )
 
@@ -79,57 +80,41 @@
 
 def render_folder (ctx, folder) :
     """
-        Render the HTML/images for this folder if needed, and recrurse into subfolders.
+        Recursively render a folder, with render_folder_images and render_folder.
 
-        Returns True if any sub-images were found, False if this folder and all subfolders were completely empty and no
-        HTML was generated.
+        This does a depth-first search of subfolders.
         
-        XXX: this logic is too complicated for one function. For one module?
+        Updates the Images as needed (based on config.force_thumbs/config.force_html).
+
+        Currently, this will always update the .html for non-empty Folders.
     """
 
-    # flag to track if we've rendered any contents.
-    empty = True
-    
-    if ctx.config.force_html or ctx.config.force_thumb :
-        # index all
-        for_update = False
-
-    else :
-        # only new images
-        for_update = True
-    
-    # full count of images
-    image_count = len(folder.images)
-
-    # index selected images
-    new_images = list(folder.index_images(for_update=for_update))
-
-    # index subfolders
+    # do depth-first recursion
     for subfolder in folder.subfolders :
-        if render_folder(ctx, subfolder) :
-            # positive assertion
-            empty = False
-    
-    if folder.images :
-        empty = False
+        render_folder(ctx, subfolder)
 
-    # only render HTML if needed
-    if not empty  :
-        # update folder index
-        render_folder_html(ctx, folder)
+    if folder.empty :
+        # warn
+        ctx.log_debug("%s - empty, skipping", folder)
+        
+        return
+ 
+    # force-update HTML, every time
+    render_folder_html(ctx, folder)
     
-    # only render new images if needed   
-    if new_images or ctx.config.force_html :
-        ctx.log_info("%s - render %d/%d images", folder, len(new_images), image_count)
+    # get the list of images that we are going to update, only those that are stale unless any force_update
+    update_images = list(folder.index_images(for_update=(not ctx.config.force_update)))
+    
+    if update_images :
+        # status
+        ctx.log_info("%s - rendering %d/%d images", folder, len(update_images), len(folder.images))
 
-        # update images
-        render_folder_images(ctx, new_images, for_update)
-
+        # update images as needed
+        render_folder_images(ctx, folder.images)
+    
     else :
-        ctx.log_info("%s - up to date", folder)
-    
-    # return flag
-    return empty
+        # nothing to do
+        ctx.log_info("%s - up-to-date", folder)
 
 @command
 def main (ctx) :
@@ -137,6 +122,6 @@
         Scan the gallery for new folders/images, and render updated ones
     """
 
-    # render the gallery
+    # render the gallery root as a folder
     render_folder(ctx, ctx.gallery)
     
--- a/degal/folder.py	Wed Jun 17 17:02:24 2009 +0300
+++ b/degal/folder.py	Wed Jun 17 17:56:02 2009 +0300
@@ -131,6 +131,17 @@
         # and the last one
         if prev :
             yield prev
+    
+    @lazy_load
+    def empty (self) :
+        """
+            A Folder is empty if it does not contain any Images, and all of its sub-Folders (if any) are also empty.
+
+            This is implemented recursively.
+        """
+        
+        # simple logic, my dear friend Watson
+        return not self.images and all(subfolder.empty for subfolder in self.subfolders)
 
     @property
     def page_count (self) :