degal/commands/main.py
author Tero Marttila <terom@fixme.fi>
Wed, 17 Jun 2009 17:02:24 +0300
changeset 131 7021d949222c
parent 123 31c4a328ef96
child 132 c2b2f4b6fe6d
permissions -rw-r--r--
fix commands.main to render HTML for folders that only have subfolders, and no direct images
from degal.command import command
from degal.concurrent import task
from degal import templates

def render_image_html (ctx, image) :
    """
        Render the thumbnails and .html for one image
    """

    ctx.log_debug("%s", image.html)

    # render output
    tpl = templates.master(ctx.gallery, image, 
        templates.image_page(image)
    )
    
    # write output
    tpl.render_file(image.html)

def update_image_thumbs (image) :
    """
        Render the thubmnails for the given image, returning the image.

        This /should/ be threadsafe.
    """
    
    # this will unconditionally update the image
    image.update()

    return image

def render_folder_images (ctx, images, for_update=True) :
    """
        Render the given series of images
    """

    # XXX: handle this thumb-update/html-update stuff better

    # render all HTML
    for image in images :
        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()
    ) :
        # log image path
        ctx.log_info("%s", image)

        # release large objects that are not needed anymore
        image.cleanup()

def render_folder_html (ctx, folder) :
    """
        Render the .html output for one folder
    """

    # render each page separately
    for page in xrange(folder.page_count) :
        # output .html page
        html = folder.html_page(page)
    
        ctx.log_debug("%s", html)
        
        # render template
        tpl = templates.master(ctx.gallery, folder,
            templates.folder_page(folder, page)
        )

        # write output
        tpl.render_file(html)

def render_folder (ctx, folder) :
    """
        Render the HTML/images for this folder if needed, and recrurse into subfolders.

        Returns True if any sub-images were found, False if this folder and all subfolders were completely empty and no
        HTML was generated.
        
        XXX: this logic is too complicated for one function. For one module?
    """

    # 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
    for subfolder in folder.subfolders :
        if render_folder(ctx, subfolder) :
            # positive assertion
            empty = False
    
    if folder.images :
        empty = False

    # only render HTML if needed
    if not empty  :
        # update folder index
        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)

        # update images
        render_folder_images(ctx, new_images, for_update)

    else :
        ctx.log_info("%s - up to date", folder)
    
    # return flag
    return empty

@command
def main (ctx) :
    """
        Scan the gallery for new folders/images, and render updated ones
    """

    # render the gallery
    render_folder(ctx, ctx.gallery)