begin implementation of folder, gallery
authorTero Marttila <terom@fixme.fi>
Fri, 05 Jun 2009 21:50:49 +0300
changeset 64 4ebd563214d2
parent 63 0db16d6b3617
child 65 97e1bc208574
begin implementation of folder, gallery
degal/folder.py
degal/gallery.py
--- a/degal/folder.py	Fri Jun 05 21:50:33 2009 +0300
+++ b/degal/folder.py	Fri Jun 05 21:50:49 2009 +0300
@@ -1,19 +1,128 @@
-import os, os.path
+"""
+    Per-directory gallery state
+"""
 
-import settings, image, utils, helpers, log
-from template import gallery as gallery_tpl
-from helpers import url_for_page
+import filesystem, image, html
 
-def dirUp (count=1) :
+from utils import lazy_load
+
+class Folder (filesyste.Directory) :
     """
-        Returns a relative path to the directly count levels above the current one
+        A Folder is a filesystem Directory that contains any number of other Folders and Images.
     """
 
-    if not count :
-        return '.'
+    def __init__ (self, node) :
+        super(Folder, self).__init__(node)
 
-    return os.path.join(*(['..']*count))
+        # info
+        self.title = None
+        self.description = None
+
+    @lazy_load
+    def preview_dir (self) :
+        """
+            Load and return the Directory for previews
+        """
+        
+        return self.subdir(self.config.preview_dir, create=True)
     
+    @lazy_load
+    def thumb_dir (self) :
+        """
+            Load and return the Directory for thumbs
+        """
+        
+        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, sorted=True)
+    
+    @lazy_load_iter
+    def subfolders (self) :
+        """
+            Load and return an ordered list of sub-Folders
+        """
+
+        return (Folder(node) for node in self.subnodes if isinstance(node, filesystem.Directory))
+
+    @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
+            # XXX: node should need to be a File
+            if not isinstance(node, filesystem.File) or not self.config.is_image(node) :
+                continue
+            
+            # create new
+            img = image.Image(node, prev)
+
+            # link up
+            if prev :
+                prev.next = img
+
+                # yield the linked-up prev
+                yield prev
+            
+            # continue
+            prev = img
+        
+        # and the last one
+        if prev :
+            yield prev
+
+    @property
+    def page_count (self) :
+        """
+            Returns the number of pages needed to show this folder's images
+        """
+
+        return math.ceil(len(self.images) / float(self.config.images_per_page))
+
+    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]
+
+    def html_file (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 (self) :
+        """
+            Recursively index this Folder, yielding a series of all Folder objects inside.
+        """
+        
+        # and subfolders
+        for subfolder in self.subfolders :
+            yield subfolder
+
+            for item in subfolder.index_subfolders() :
+                yield item
+   
 class Folder (object) :
     def __init__ (self, name='.', parent=None) :
         # the directory name, no trailing /
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/degal/gallery.py	Fri Jun 05 21:50:49 2009 +0300
@@ -0,0 +1,48 @@
+"""
+    Top-level gallery state
+"""
+
+import filesystem, folder, resources
+
+from utils import lazy_load
+
+class Gallery (filesystem.Root, folder.Folder) :
+    """
+        A Gallery is a single place in the filesystem which acts as a Folder of Images, but had some additional stuff
+    """
+
+    def __init__ (self, path, config) :
+        """
+            Build a Gallery rooted at the given filesystem path, and using the given user configuration.
+
+            XXX: replace path with config.gallery_path?
+        """
+
+        super(Gallery, self).__init__(path, config)
+
+    @lazy_load
+    def degal_dir (self) :
+        """
+            The dir containing the degal configuration.
+
+            It will be created if it does not exist.
+        """
+
+        return self.subdir('degal', create=True)
+
+    @lazy_load
+    def stylesheet (self) :
+        """
+            The stylesheet file.
+
+            It will be copied from internal resources if it does not exist.
+        """
+
+        stylesheet = self.degal_dir.subfile('style.css')
+        
+        if not stylesheet :
+            # copy it from resources
+            stylesheet.copy_from(resources.STATIC_DIR.subfile('style.css'))
+
+        return stylesheet
+