degal/commands/main.py
author Tero Marttila <terom@fixme.fi>
Thu, 11 Jun 2009 20:39:59 +0300
branchthreaded-tasks
changeset 88 b1b0939517e7
parent 87 a7a18893730d
permissions -rw-r--r--
initial implementation of threaded rendering of a folder's images
76
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 66
diff changeset
     1
from degal.command import command
66
322e5cd0cb1f implement new main() command, remove old one
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
     2
from degal import templates
88
b1b0939517e7 initial implementation of threaded rendering of a folder's images
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
     3
from degal.task import threadable_task
48
20355dd2e61a new structure for bin/degal, adding degal/commands, degal/main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
88
b1b0939517e7 initial implementation of threaded rendering of a folder's images
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
     5
def render_image_html (ctx, image) :
66
322e5cd0cb1f implement new main() command, remove old one
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
     6
    """
322e5cd0cb1f implement new main() command, remove old one
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
     7
        Render the thumbnails and .html for one image
322e5cd0cb1f implement new main() command, remove old one
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
     8
    """
87
a7a18893730d implement up-to-date checking for commands.main, and add --force-update option
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
     9
66
322e5cd0cb1f implement new main() command, remove old one
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    10
    # render output
322e5cd0cb1f implement new main() command, remove old one
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    11
    tpl = templates.master(ctx.gallery, image.title, image.html, 
322e5cd0cb1f implement new main() command, remove old one
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    12
        templates.image_page(image)
322e5cd0cb1f implement new main() command, remove old one
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    13
    )
322e5cd0cb1f implement new main() command, remove old one
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    14
    
322e5cd0cb1f implement new main() command, remove old one
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    15
    # write output
322e5cd0cb1f implement new main() command, remove old one
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    16
    tpl.render_file(image.html)
322e5cd0cb1f implement new main() command, remove old one
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    17
88
b1b0939517e7 initial implementation of threaded rendering of a folder's images
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    18
def render_image_task (image) :
b1b0939517e7 initial implementation of threaded rendering of a folder's images
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    19
    """
b1b0939517e7 initial implementation of threaded rendering of a folder's images
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    20
        Render the image output for the given image
b1b0939517e7 initial implementation of threaded rendering of a folder's images
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    21
    """
b1b0939517e7 initial implementation of threaded rendering of a folder's images
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    22
    
b1b0939517e7 initial implementation of threaded rendering of a folder's images
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    23
    # XXX: is this really threadsafe?
b1b0939517e7 initial implementation of threaded rendering of a folder's images
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    24
    image.update()
b1b0939517e7 initial implementation of threaded rendering of a folder's images
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    25
    
b1b0939517e7 initial implementation of threaded rendering of a folder's images
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    26
    # return value is the image itself
b1b0939517e7 initial implementation of threaded rendering of a folder's images
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    27
    return image
b1b0939517e7 initial implementation of threaded rendering of a folder's images
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    28
b1b0939517e7 initial implementation of threaded rendering of a folder's images
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    29
def render_folder_images (ctx, images) :
b1b0939517e7 initial implementation of threaded rendering of a folder's images
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    30
    """
b1b0939517e7 initial implementation of threaded rendering of a folder's images
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    31
        Render the given series of images
b1b0939517e7 initial implementation of threaded rendering of a folder's images
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    32
    """
b1b0939517e7 initial implementation of threaded rendering of a folder's images
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    33
    
b1b0939517e7 initial implementation of threaded rendering of a folder's images
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    34
    # render them in parallel as required
b1b0939517e7 initial implementation of threaded rendering of a folder's images
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    35
    for image in ctx.tasks.execute([
b1b0939517e7 initial implementation of threaded rendering of a folder's images
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    36
        threadable_task(render_image_task, image) for image in images
b1b0939517e7 initial implementation of threaded rendering of a folder's images
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    37
    ]) :
b1b0939517e7 initial implementation of threaded rendering of a folder's images
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    38
        # log image path
b1b0939517e7 initial implementation of threaded rendering of a folder's images
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    39
        ctx.log_info("%s", image)
b1b0939517e7 initial implementation of threaded rendering of a folder's images
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    40
b1b0939517e7 initial implementation of threaded rendering of a folder's images
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    41
        render_image_html(ctx, image)
b1b0939517e7 initial implementation of threaded rendering of a folder's images
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    42
87
a7a18893730d implement up-to-date checking for commands.main, and add --force-update option
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
    43
def render_folder_page (ctx, folder) :
48
20355dd2e61a new structure for bin/degal, adding degal/commands, degal/main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    44
    """
87
a7a18893730d implement up-to-date checking for commands.main, and add --force-update option
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
    45
        Render the .html output for one folder
48
20355dd2e61a new structure for bin/degal, adding degal/commands, degal/main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    46
    """
20355dd2e61a new structure for bin/degal, adding degal/commands, degal/main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    47
87
a7a18893730d implement up-to-date checking for commands.main, and add --force-update option
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
    48
    # render each page separately
66
322e5cd0cb1f implement new main() command, remove old one
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    49
    for page in xrange(folder.page_count) :
322e5cd0cb1f implement new main() command, remove old one
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    50
        # output .html page
322e5cd0cb1f implement new main() command, remove old one
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    51
        html_file = folder.html_file(page)
322e5cd0cb1f implement new main() command, remove old one
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    52
        
322e5cd0cb1f implement new main() command, remove old one
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    53
        # render template
322e5cd0cb1f implement new main() command, remove old one
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    54
        tpl = templates.master(ctx.gallery, folder.title, html_file,
322e5cd0cb1f implement new main() command, remove old one
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    55
            templates.folder_page(folder, page)
322e5cd0cb1f implement new main() command, remove old one
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    56
        )
322e5cd0cb1f implement new main() command, remove old one
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    57
322e5cd0cb1f implement new main() command, remove old one
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    58
        # write output
322e5cd0cb1f implement new main() command, remove old one
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    59
        tpl.render_file(html_file)
322e5cd0cb1f implement new main() command, remove old one
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    60
87
a7a18893730d implement up-to-date checking for commands.main, and add --force-update option
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
    61
def render_folder (ctx, folder) :
88
b1b0939517e7 initial implementation of threaded rendering of a folder's images
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    62
    """
b1b0939517e7 initial implementation of threaded rendering of a folder's images
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    63
        Render the HTML/images for this folder if needed, and recrurse into subfolders.
b1b0939517e7 initial implementation of threaded rendering of a folder's images
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    64
    """
b1b0939517e7 initial implementation of threaded rendering of a folder's images
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    65
87
a7a18893730d implement up-to-date checking for commands.main, and add --force-update option
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
    66
    # index images that require updating
a7a18893730d implement up-to-date checking for commands.main, and add --force-update option
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
    67
    image_count = len(folder.images)
a7a18893730d implement up-to-date checking for commands.main, and add --force-update option
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
    68
    new_images = list(folder.index_images(for_update=(not ctx.config.force_update)))
a7a18893730d implement up-to-date checking for commands.main, and add --force-update option
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
    69
    
a7a18893730d implement up-to-date checking for commands.main, and add --force-update option
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
    70
    if new_images or ctx.config.force_update:
a7a18893730d implement up-to-date checking for commands.main, and add --force-update option
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
    71
        # update folder index
a7a18893730d implement up-to-date checking for commands.main, and add --force-update option
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
    72
        render_folder_page(ctx, folder)
a7a18893730d implement up-to-date checking for commands.main, and add --force-update option
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
    73
        
a7a18893730d implement up-to-date checking for commands.main, and add --force-update option
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
    74
        ctx.log_info("%s - render %d/%d images", folder, len(new_images), image_count)
a7a18893730d implement up-to-date checking for commands.main, and add --force-update option
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
    75
a7a18893730d implement up-to-date checking for commands.main, and add --force-update option
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
    76
        # update images
88
b1b0939517e7 initial implementation of threaded rendering of a folder's images
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    77
        render_folder_images(ctx, new_images)
87
a7a18893730d implement up-to-date checking for commands.main, and add --force-update option
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
    78
    
a7a18893730d implement up-to-date checking for commands.main, and add --force-update option
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
    79
    else :
a7a18893730d implement up-to-date checking for commands.main, and add --force-update option
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
    80
        ctx.log_info("%s - up to date", folder)
a7a18893730d implement up-to-date checking for commands.main, and add --force-update option
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
    81
a7a18893730d implement up-to-date checking for commands.main, and add --force-update option
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
    82
    # index subfolders
a7a18893730d implement up-to-date checking for commands.main, and add --force-update option
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
    83
    for subfolder in folder.subfolders :
a7a18893730d implement up-to-date checking for commands.main, and add --force-update option
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
    84
        render_folder(ctx, subfolder)
66
322e5cd0cb1f implement new main() command, remove old one
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    85
322e5cd0cb1f implement new main() command, remove old one
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    86
@command
322e5cd0cb1f implement new main() command, remove old one
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    87
def main (ctx) :
322e5cd0cb1f implement new main() command, remove old one
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    88
    """
322e5cd0cb1f implement new main() command, remove old one
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    89
        Scan the gallery for new folders/images, and render updated ones
322e5cd0cb1f implement new main() command, remove old one
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    90
    """
322e5cd0cb1f implement new main() command, remove old one
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    91
322e5cd0cb1f implement new main() command, remove old one
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    92
    # render the gallery
322e5cd0cb1f implement new main() command, remove old one
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    93
    render_folder(ctx, ctx.gallery)
48
20355dd2e61a new structure for bin/degal, adding degal/commands, degal/main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    94