degal/folder.py
author Tero Marttila <terom@fixme.fi>
Wed, 01 Jul 2009 20:40:00 +0300
changeset 141 9387da0dc183
parent 138 130fb8a8dbb9
permissions -rw-r--r--
move .config from filesystem to gallery/folder/image, rename degal_dir to app_dir
"""
    Per-directory gallery state
"""

import filesystem, image, html

from utils import lazy_load, lazy_load_iter

import math

class Folder (filesystem.Directory) :
    """
        A Folder is a filesystem Directory that:
            * has a title/description (XXX: Page)
            * contains any number of other sub-Folders and Images.
            * maintains a hierarchial Configuration that can be overrided for sub-nodes
            * holds sub-dirs for previews/thumbs
            * has a HTML view
    """

    def iter_config_files (self) :
        """
            Iterate over the possible config files for this dir

            XXX: abstract this
        """

        yield self.subfile('degal.cfg')
    
    def _init_config (self) :
        """
            Initialize .config for ourselves.
        """

        # use parent's config
        return self.parent.config

    def __init__ (self, *args, **kwargs) :
        super(Folder, self).__init__(*args, **kwargs)
        
        # load .config
        self.config = self._init_config()
 
        # find custom config?
        for file in self.iter_config_files() :
            if file.exists() :
                # yay! More configuration!
                self.config = self.config.load(file.path)

                break

        # load some info for title?
        if self.config and self.config.title :
            # override our default title
            self.title = self.config.title

            # disable it so it won't be used by children
            # XXX: figure out a better way of doing this
            self.config.title = None
   
    @lazy_load
    def title (self) :
        """
            Find the default title for this dir.
        """

        # default
        return self.name.title()
    
    @lazy_load
    def description (self) :
        """
            Find the descriptive text for this dir
        """

        # default
        return None

    @lazy_load
    def preview_dir (self) :
        """
            Load and return the Directory used for for preview Thumbnails
        """
        
        return self.subdir(self.config.preview_dir, create=True)
    
    @lazy_load
    def thumb_dir (self) :
        """
            Load and return the Directory used for thumb Thumbnails
        """
        
        return self.subdir(self.config.thumb_dir, create=True)
   
    @lazy_load_iter
    def subnodes (self) :
        """
            Load and return an ordered list of child-Nodes
        """

        return super(Folder, self).subnodes(skip_dotfiles=True, sort=True)
    
    @lazy_load_iter
    def subfolders (self) :
        """
            Load and return an ordered list of sub-Folders
        """
        
        # filter out valid subfolders
        for node in self.subnodes :
            # skip non-dirs
            if not node.is_dir() :
                continue

            # skip the previews/thumbs dirs
            if node.name in (self.config.preview_dir, self.config.thumb_dir) :
                continue

            # ok
            yield Folder(node)

    @lazy_load_iter
    def images (self) :
        """
            Load and return an ordered/linked list of sub-Images
        """

        prev = None

        for node in self.subnodes :
            # skip non-relevant ones
            if not node.is_file() or not self.config.is_image(filesystem.File(node)) :
                continue
            
            # create new
            img = image.Image(node)

            # link up
            if prev :
                img.prev = prev
                prev.next = img

                # yield the linked-up prev
                yield prev
            
            # continue
            prev = img
        
        # 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) :
        """
            Returns the number of pages needed to show this folder's images or subfolders. May be zero, one or more.
        """
        
        if self.images :
            return int(math.ceil(len(self.images) / float(self.config.images_per_page)))
        
        elif self.subfolders :
            # paginate these?
            return 1

        else :
            # nothing to render, really
            return 0

    def images_for_page (self, page) :
        """
            Returns the list of Images to be displayed for the given page, if any
        """
        
        # offset to first image
        offset = page * self.config.images_per_page
        
        # slice
        return self.images[offset : offset + self.config.images_per_page]
    
    @property
    def html (self) :
        """
            Path to default page
        """

        return self.html_page()
    
    def html_page (self, page=0) :
        """
            Returns the File representing the .html for the given page
        """
        
        if page :
            return self.subfile("index_%d.html" % page)
        
        else :
            return self.subfile("index.html")
    
    def index_images (self, for_update=True) :
        """
            Return a series of Images inside of this folder.

            If `for_update` is given, only images that are stale will be returned.
        """
        
        for image in self.images :
            if for_update and not image.stale() :
                # skip 
                continue

            yield image