degal/commands/main.py
author Tero Marttila <terom@fixme.fi>
Sun, 14 Jun 2009 22:59:29 +0300
changeset 119 e7855eefb4c7
parent 118 60b126ff0b74
child 123 31c4a328ef96
permissions -rw-r--r--
fix breadcrumb/title stuff
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 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)
        
    # then render all HTML
    for image in images :
        render_image_html(ctx, image)

        # release large objects that are not needed anymore
        # XXX: verify that this works
        del image.img

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.
    """
    
    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))

    if new_images or ctx.config.force_html :
        # update folder index
        render_folder_html(ctx, folder)
        
        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)

    # index subfolders
    for subfolder in folder.subfolders :
        render_folder(ctx, subfolder)

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

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