degal.py
author terom
Tue, 20 Nov 2007 23:47:00 +0000
changeset 10 e8143d784b77
parent 7 235ae238f694
child 11 27dac27d1a58
permissions -rwxr-xr-x
SI units/ISO date-time format
1
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
     1
#!/usr/bin/env python2.4
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
     2
#
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
     3
# DeGAL - A pretty simple web image gallery
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
     4
# Copyright (C) 2007 Tero Marttila
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
     5
# http://marttila.de/~terom/degal/
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
     6
#
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
     7
# This program is free software; you can redistribute it and/or modify
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
     8
# it under the terms of the GNU General Public License as published by
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
     9
# the Free Software Foundation; either version 2 of the License, or
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    10
# (at your option) any later version.
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    11
#
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    12
# This program is distributed in the hope that it will be useful,
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    15
# GNU General Public License for more details.
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    16
#
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    17
# You should have received a copy of the GNU General Public License
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    18
# along with this program; if not, write to the
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    19
# Free Software Foundation, Inc.,
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    20
# 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    21
#
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    22
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    23
import os.path, os
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    24
import urllib
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    25
import logging
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    26
import string
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    27
from datetime import datetime
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    28
import struct
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    29
import base64
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    30
import shelve
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    31
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    32
import PIL
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    33
import PIL.Image
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    34
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    35
import utils
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    36
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    37
__version__ = '0.2'
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    38
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    39
TEMPLATE_DIR='templates'
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    40
TEMPLATE_EXT='html'
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    41
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    42
logging.basicConfig(
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    43
    level=logging.INFO,
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    44
    format="%(name)8s %(levelname)8s   %(lineno)3d %(message)s",
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    45
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    46
)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    47
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    48
tpl = logging.getLogger('tpl')
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    49
index = logging.getLogger('index')
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    50
prepare = logging.getLogger('prepare')
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    51
render = logging.getLogger('render')
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    52
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    53
tpl.setLevel(logging.WARNING)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    54
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    55
#for l in (tpl, index, prepare, render) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    56
#    l.setLevel(logging.DEBUG)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    57
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    58
def readFile (path) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    59
    fo = open(path, 'r')
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    60
    data = fo.read()
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    61
    fo.close()
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    62
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    63
    return data
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    64
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    65
class Template (object) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    66
    GLOBALS = dict(
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    67
        VERSION=__version__,
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    68
    )
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    69
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    70
    def __init__ (self, name) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    71
        self.name = name
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    72
        self.path = os.path.join(TEMPLATE_DIR, "%s.%s" % (name, TEMPLATE_EXT))
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    73
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    74
        tpl.debug("Template %s at %s", name, self.path)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    75
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    76
        self.content = readFile(self.path)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    77
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    78
    def render (self, **vars) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    79
        content = self.content
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    80
        
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    81
        vars.update(self.GLOBALS)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    82
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    83
        for name, value in vars.iteritems() :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    84
            content = content.replace('<!-- %s -->' % name, str(value))
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    85
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    86
        return content
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    87
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    88
    def renderTo (self, path, **vars) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    89
        tpl.info("Render %s to %s", self.name, path)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    90
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    91
        fo = open(path, 'w')
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    92
        fo.write(self.render(**vars))
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    93
        fo.close()
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    94
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    95
gallery_tpl = Template('gallery')
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    96
image_tpl = Template('image')
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    97
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    98
IMAGE_EXTS = ('jpg', 'jpeg', 'png', 'gif', 'bmp')
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    99
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   100
THUMB_DIR = 'thumbs'
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   101
PREVIEW_DIR = 'previews'
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   102
TITLE_FILE = 'title.txt'
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   103
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   104
THUMB_GEOM = (160, 120)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   105
PREVIEW_GEOM = (640, 480)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   106
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   107
DEFAULT_TITLE = 'Image gallery'
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   108
5
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   109
# how many image/page
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   110
IMAGE_COUNT = 50
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   111
1
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   112
def isImage (fname) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   113
    """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   114
        Is the given filename likely to be an image file?
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   115
    """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   116
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   117
    fname = fname.lower()
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   118
    base, ext = os.path.splitext(fname)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   119
    ext = ext.lstrip('.')
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   120
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   121
    return ext in IMAGE_EXTS
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   122
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   123
def link (url, title) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   124
    """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   125
        Returns an <a href=""></a> link tag with the given values
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   126
    """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   127
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   128
    return "<a href='%s'>%s</a>" % (urllib.quote(url), title)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   129
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   130
def dirUp (count=1) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   131
    """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   132
        Returns a relative path to the directly count levels above the current one
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   133
    """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   134
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   135
    if not count :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   136
        return '.'
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   137
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   138
    return os.path.join(*(['..']*count))
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   139
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   140
def readTitleDescr (path) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   141
    """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   142
        Read a title.txt or <imgname>.txt file
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   143
    """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   144
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   145
    if os.path.exists(path) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   146
        content = readFile(path)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   147
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   148
        if '---' in content :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   149
            title, descr = content.split('---', 1)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   150
        else :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   151
            title, descr = content, ''
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   152
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   153
        return title.strip(), descr.strip()
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   154
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   155
    return None, None
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   156
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   157
class Folder (object) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   158
    def __init__ (self, name='.', parent=None) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   159
        # the directory name
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   160
        self.name = name
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   161
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   162
        # our parent Folder, or None
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   163
        self.parent = parent
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   164
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   165
        # the path to this dir, as a relative path to the root of the image gallery, always starts with .
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   166
        if parent and name :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   167
            self.path = parent.pathFor(name)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   168
        else :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   169
            self.path = name
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   170
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   171
        # the url-path to the index.html file
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   172
        self.html_path = self.path
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   173
        
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   174
        # dict of fname -> Folder
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   175
        self.subdirs = {}
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   176
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   177
        # dict of fname -> Image
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   178
        self.images = {}
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   179
        
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   180
        # our human-friendly title
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   181
        self.title = None
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   182
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   183
        # our long-winded description
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   184
        self.descr = ''
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   185
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   186
        # is this folder non-empty?
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   187
        self.alive = None
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   188
        
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   189
        # self.images.values(), but sorted by filename
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   190
        self.sorted_images = []
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   191
        
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   192
        # the ShortURL key to this dir
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   193
        self.shorturl_code = None
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   194
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   195
        # were we filtered out?
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   196
        self.filtered = False
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   197
   
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   198
    def pathFor (self, *fnames) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   199
        """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   200
            Return a root-relative path to the given path inside this dir
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   201
        """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   202
        return os.path.join(self.path, *fnames)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   203
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   204
    def index (self, filters=None) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   205
        """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   206
            Look for other dirs and images inside this dir. Filters must be either None,
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   207
            whereupon all files will be included, or a dict of {filename -> next_filter}.
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   208
            If given, only filenames that are present in the dict will be indexed, and in
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   209
            the case of dirs, the next_filter will be passed on to that Folder's index
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   210
            method.
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   211
        """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   212
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   213
        index.info("Indexing %s", self.path)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   214
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   215
        if filters :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   216
            self.filtered = True
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   217
        
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   218
        # iterate through listdir
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   219
        for fname in os.listdir(self.path) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   220
            # the full filesystem path to it
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   221
            fpath = self.pathFor(fname)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   222
            
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   223
            # ignore dotfiles
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   224
            if fname.startswith('.') :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   225
                index.debug("Skipping dotfile %s", fname)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   226
                continue
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   227
            
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   228
            # apply filters
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   229
            if filters :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   230
                if fname in filters :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   231
                    next_filter = filters[fname]
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   232
                else :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   233
                    index.debug("Skip `%s' as we have a filter", fname)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   234
                    continue
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   235
            else :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   236
                next_filter = None
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   237
                
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   238
            # recurse into subdirs, but not thumbs/previews
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   239
            if os.path.isdir(fpath) and fname not in (THUMB_DIR, PREVIEW_DIR) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   240
                index.debug("Found subdir %s", fpath)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   241
                f = self.subdirs[fname] = Folder(fname, self)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   242
                f.index(next_filter)   # recursion
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   243
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   244
            # handle images
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   245
            elif os.path.isfile(fpath) and isImage(fname) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   246
                index.debug("Found image %s", fname)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   247
                self.images[fname] = Image(self, fname)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   248
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   249
            # ignore everything else
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   250
            else :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   251
                index.debug("Ignoring file %s", fname)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   252
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   253
    def prepare (self) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   254
        """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   255
            Prepare the dir, i.e. sort+prepare the images, as well as recurse
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   256
            into subdirs
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   257
        """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   258
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   259
        prepare.info("Preparing dir %s", self.path)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   260
        
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   261
        # is this folder non-empty?
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   262
        alive = False
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   263
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   264
        # only create thumbs/previews dirs if we have images in here
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   265
        if self.images :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   266
            # folder is non-empty
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   267
            alive = True
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   268
            prepare.info("Have %d images", len(self.images))
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   269
            
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   270
            # create the thumb/preview dirs if needed
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   271
            for dir in (THUMB_DIR, PREVIEW_DIR) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   272
                prepare.debug("Checking for existance of %s dir", dir)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   273
                path = self.pathFor(dir)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   274
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   275
                if not os.path.isdir(path) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   276
                    prepare.info("Creating dir %s", path)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   277
                    os.mkdir(path)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   278
            
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   279
            # sort the images
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   280
            fnames = self.images.keys()
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   281
            fnames.sort()
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   282
            
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   283
            prev = None
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   284
            
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   285
            # link them together and prepare them
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   286
            for fname in fnames :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   287
                img = self.images[fname]
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   288
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   289
                prepare.debug("Linking %s <-> %s", prev, img)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   290
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   291
                img.prev = prev
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   292
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   293
                if prev :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   294
                    prev.next = img
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   295
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   296
                prev = img
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   297
                
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   298
                img.prepare()
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   299
                
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   300
                # add to the sorted images list
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   301
                self.sorted_images.append(img)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   302
        
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   303
        # prepare subdirs
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   304
        if self.subdirs :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   305
            prepare.info("Have %d subdirs", len(self.subdirs))
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   306
            
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   307
            # just recurse into them, we're alive if one of them is
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   308
            for dir in self.subdirs.itervalues() :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   309
                if dir.prepare() :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   310
                    alive = True
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   311
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   312
        # figure out our title
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   313
        title_path = self.pathFor(TITLE_FILE)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   314
        
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   315
        title, descr = readTitleDescr(title_path)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   316
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   317
        if title :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   318
            prepare.info("Found title/descr")
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   319
            self.title = title
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   320
            self.descr = descr
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   321
            alive = True
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   322
        
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   323
        # default title for the root dir
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   324
        elif self.name == '.' :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   325
            self.title = 'Index'
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   326
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   327
        else :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   328
            self.title = self.name
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   329
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   330
        prepare.debug("Our title is '%s'", self.title)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   331
        
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   332
        # lol ded
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   333
        if not alive :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   334
            prepare.info("Dir %s is not alive", self.path)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   335
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   336
        self.alive = alive
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   337
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   338
        return alive
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   339
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   340
    def getObjInfo (self) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   341
        """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   342
            Metadata for shorturls2.db
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   343
        """
5
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   344
        return 'dir', self.path, 'index'
1
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   345
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   346
    def linkTag (self) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   347
        """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   348
            A text-link to this dir
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   349
        """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   350
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   351
        return link(self.path, self.title)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   352
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   353
    def breadcrumb (self) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   354
        """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   355
            Returns a [(fname, title)] list of this dir's parent dirs
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   356
        """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   357
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   358
        f = self
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   359
        b = []
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   360
        d = 0
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   361
        
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   362
        while f :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   363
            b.insert(0, (dirUp(d), f.title))
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   364
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   365
            d += 1
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   366
            f = f.parent
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   367
        
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   368
        return b
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   369
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   370
    def inRoot (self, *fnames) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   371
        """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   372
            Return a relative URL from this dir to the given path in the root dir
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   373
        """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   374
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   375
        c = len(self.path.split('/')) - 1
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   376
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   377
        return os.path.join(*((['..']*c) + list(fnames)))
5
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   378
    
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   379
    def _page_fname (self, page) :
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   380
        assert page >= 0
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   381
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   382
        if page > 0 :
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   383
            return  'index_%d.html' % page
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   384
        else :
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   385
            return 'index.html'
1
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   386
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   387
    def render (self) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   388
        """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   389
            Render the index.html, Images, and recurse into subdirs
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   390
        """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   391
        
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   392
        # ded folders are skipped
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   393
        if not self.alive :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   394
            render.info("Skipping dir %s", self.path)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   395
            return
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   396
        
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   397
        # if this dir's contents were filtered out, then we can't render the index.html, as we aren't aware of all the images in here
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   398
        if self.filtered :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   399
            render.warning("Dir `%s' contents were filtered, so we won't render the gallery index again", self.path)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   400
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   401
        else :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   402
            # sort the subdirs
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   403
            subdirs = self.subdirs.items()
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   404
            subdirs.sort()
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   405
            
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   406
            # generate the <a href=""></a>'s for the subdirs
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   407
            subdir_linkTags = [link(f.name, f.title) for fname, f in subdirs if f.alive]
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   408
            
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   409
            # stick them into a list
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   410
            if subdir_linkTags :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   411
                directories = "<ul>\n\t<li>%s</li>\n</ul>" % "</li>\n\t<li>".join(subdir_linkTags)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   412
            else :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   413
                directories = ''
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   414
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   415
            render.info("Rendering %s", self.path)
5
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   416
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   417
            # paginate!
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   418
            images = self.sorted_images
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   419
            image_count = len(images)
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   420
            pages = []
1
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   421
            
5
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   422
            while images :
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   423
                pages.append(images[:IMAGE_COUNT])
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   424
                images = images[IMAGE_COUNT:]
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   425
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   426
            pagination_required = len(pages) > 1
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   427
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   428
            if pagination_required :
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   429
                render.info("Index split into %d pages of %d images each", len(pages), IMAGE_COUNT)
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   430
            
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   431
            for cur_page, page in enumerate(pages) :
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   432
                if pagination_required :
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   433
                    pagination = "<ul>" # <li>Showing Images %d - %d of %d</li>" % (cur_page*IMAGE_COUNT, cur_page*IMAGE_COUNT+len(page), image_count)
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   434
                    
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   435
                    if cur_page > 0 :
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   436
                        pagination += '<li><a href="%s">&laquo; Prev</a></li>' % (self._page_fname(cur_page - 1))
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   437
                    else :
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   438
                        pagination += '<li><span>&laquo; Prev</span></li>'
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   439
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   440
                    for x in xrange(0, len(pages)) :
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   441
                        if x == cur_page :
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   442
                            pagination += '<li><strong>%s</strong></li>' % (x + 1)
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   443
                        else :
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   444
                            pagination += '<li><a href="%s">%s</a></li>' % (self._page_fname(x), x+1)
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   445
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   446
                    if cur_page < len(pages) - 1 :
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   447
                        pagination += '<li><a href="%s">Next &raquo;</a></li>' % (self._page_fname(cur_page + 1))
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   448
                    else :
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   449
                        pagination += '<li><span>Next &raquo;</span></li>'
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   450
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   451
                    pagination += "</ul>"
6
d9d1f8e5f384 fix alignment of teh paginator, and make shorturl dirs better (no index.html + pagination)
terom
parents: 5
diff changeset
   452
                    shorturl = "%s/%s" % (self.shorturl_code, cur_page+1)
5
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   453
                else :
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   454
                    pagination = ''
6
d9d1f8e5f384 fix alignment of teh paginator, and make shorturl dirs better (no index.html + pagination)
terom
parents: 5
diff changeset
   455
                    shorturl = self.shorturl_code
5
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   456
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   457
                # render to index.html
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   458
                gallery_tpl.renderTo(self.pathFor(self._page_fname(cur_page)), 
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   459
                    STYLE_URL=self.inRoot('style.css'),
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   460
                    BREADCRUMB=" &raquo; ".join([link(u, t) for (u, t) in self.breadcrumb()]),
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   461
                    TITLE=self.title,
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   462
                    DIRECTORIES=directories,
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   463
                    PAGINATION=pagination,
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   464
                    CONTENT="".join([i.thumbImgTag() for i in page]),
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   465
                    DESCR=self.descr,
6
d9d1f8e5f384 fix alignment of teh paginator, and make shorturl dirs better (no index.html + pagination)
terom
parents: 5
diff changeset
   466
                    SHORTURL=self.inRoot('s', shorturl),
d9d1f8e5f384 fix alignment of teh paginator, and make shorturl dirs better (no index.html + pagination)
terom
parents: 5
diff changeset
   467
                    SHORTURL_CODE=shorturl,
5
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   468
                )
1
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   469
        
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   470
        # render images
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   471
        for img in self.images.itervalues() :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   472
            img.render()
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   473
        
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   474
        # recurse into subdirs
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   475
        for dir in self.subdirs.itervalues() :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   476
            dir.render()
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   477
                    
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   478
class Image (object) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   479
    def __init__ (self, dir, name) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   480
        # the image filename, e.g. DSC3948.JPG
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   481
        self.name = name
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   482
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   483
        # the Folder object that we are in
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   484
        self.dir = dir
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   485
        
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   486
        # the relative path from the root to us
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   487
        self.path = dir.pathFor(name)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   488
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   489
        # the basename+ext, e.g. DSCR3948, .JPG
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   490
        self.base_name, self.ext = os.path.splitext(name)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   491
        
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   492
        # the root-relative paths to the thumb and preview images
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   493
        self.thumb_path = self.dir.pathFor(THUMB_DIR, self.name)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   494
        self.preview_path = self.dir.pathFor(PREVIEW_DIR, self.name)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   495
        
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   496
        # our user-friendly title
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   497
        self.title = name
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   498
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   499
        # our long-winded description
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   500
        self.descr = ''
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   501
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   502
        # the image before and after us, both may be None
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   503
        self.prev = self.next = None
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   504
        
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   505
        # the name of the .html gallery view thing for this image, *always* self.name + ".html"
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   506
        self.html_name = self.name + ".html"
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   507
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   508
        # the root-relative path to the gallery view
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   509
        self.html_path = self.dir.pathFor(self.html_name)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   510
        
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   511
        #
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   512
        # Figured out after prepare
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   513
        #
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   514
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   515
        # (w, h) tuple
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   516
        self.img_size = None
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   517
        
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   518
        # the ShortURL code for this image
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   519
        self.shorturl_code = None
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   520
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   521
        # what to use in the rendered templates, intended to be overridden by subclasses
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   522
        self.series_act = "add"
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   523
        self.series_verb = "Add to"
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   524
    
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   525
    def prepare (self) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   526
        """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   527
            Generate the thumbnail/preview views if needed, get the image info, and look for the title
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   528
        """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   529
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   530
        prepare.info("Preparing image %s", self.path)
7
235ae238f694 add missing utils.py
terom
parents: 6
diff changeset
   531
        
1
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   532
        # stat the image file to get the filesize and mtime
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   533
        st = os.stat(self.path)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   534
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   535
        self.filesize = st.st_size
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   536
        self.timestamp = st.st_mtime
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   537
        
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   538
        # open the image in PIL to get image attributes + generate thumbnails
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   539
        img = PIL.Image.open(self.path)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   540
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   541
        self.img_size = img.size
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   542
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   543
        for out_path, geom in ((self.thumb_path, THUMB_GEOM), (self.preview_path, PREVIEW_GEOM)) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   544
            # if it doesn't exist, or it's older than the image itself, generate
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   545
            if not (os.path.exists(out_path) and os.stat(out_path).st_mtime > self.timestamp) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   546
                prepare.info("Create thumbnailed image at %s with geom %s", out_path, geom)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   547
                
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   548
                # XXX: is this the most efficient way to do this?
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   549
                out_img = img.copy()
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   550
                out_img.thumbnail(geom, resample=True)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   551
                out_img.save(out_path)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   552
        
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   553
        # look for the metadata file
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   554
        title_path = self.dir.pathFor(self.base_name + '.txt')
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   555
        prepare.debug("Looking for title at %s", title_path)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   556
        
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   557
        title, descr = readTitleDescr(title_path)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   558
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   559
        if title :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   560
            self.title = title
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   561
            self.descr = descr
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   562
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   563
            prepare.info("Found title `%s'", self.title)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   564
    
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   565
    def getObjInfo (self) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   566
        """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   567
            Metadata for shorturl2.db
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   568
        """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   569
        return 'img', self.dir.path, self.name
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   570
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   571
    def thumbImgTag (self) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   572
        """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   573
            a <a><img /></a> of this image's thumbnail. Path relative to directory we are in
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   574
        """
5
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   575
        return link(self.html_name, "<img src='%s' alt='%s' title='%s' />" % (os.path.join(THUMB_DIR, self.name), self.descr, self.title))
1
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   576
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   577
    def previewImgTag (self) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   578
        """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   579
            a <a><img /></a> of this image's preview. Path relative to directory we are in
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   580
        """
5
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   581
        return link(self.name, "<img src='%s' alt='%s' title='%s' />" % (os.path.join(PREVIEW_DIR, self.name), self.descr, self.title))
1
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   582
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   583
    def linkTag (self) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   584
        """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   585
            a <a></a> text-link to this image
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   586
        """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   587
        return link(self.html_name, self.title)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   588
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   589
    def breadcrumb (self) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   590
        """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   591
            Returns a [(fname, title)] list of this image's parents
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   592
        """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   593
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   594
        f = self.dir
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   595
        b = [(self.html_name, self.title)]
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   596
        d = 0
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   597
        
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   598
        while f :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   599
            b.insert(0, (dirUp(d), f.title))
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   600
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   601
            d += 1
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   602
            f = f.parent
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   603
        
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   604
        return b
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   605
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   606
    def render (self) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   607
        """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   608
            Write out the .html file
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   609
        """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   610
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   611
        render.info("Rendering image %s", self.path)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   612
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   613
        image_tpl.renderTo(self.html_path,
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   614
            STYLE_URL=self.dir.inRoot('style.css'),
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   615
            UP_URL=('.'),
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   616
            PREV_URL=(self.prev and self.prev.html_name or ''),
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   617
            NEXT_URL=(self.next and self.next.html_name or ''),
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   618
            FILE=self.name,
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   619
            BREADCRUMB=" &raquo; ".join([link(u, t) for u, t in self.breadcrumb()]),
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   620
            TITLE=self.title,
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   621
            PREVIOUS_THUMB=(self.prev and self.prev.thumbImgTag() or ''),
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   622
            IMAGE=self.previewImgTag(),
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   623
            NEXT_THUMB=(self.next and self.next.thumbImgTag() or ''),
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   624
            DESCRIPTION=self.descr,
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   625
            IMGSIZE="%dx%d" % self.img_size,
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   626
            FILESIZE=fmtFilesize(self.filesize),
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   627
            TIMESTAMP=fmtTimestamp(self.timestamp),
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   628
            SHORTURL=self.dir.inRoot('s', self.shorturl_code),
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   629
            SHORTURL_CODE=self.shorturl_code,
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   630
            SERIES_URL=self.dir.inRoot('series/%s/%s' % (self.series_act, self.shorturl_code)),
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   631
            SERIES_VERB=self.series_verb,
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   632
        )   
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   633
    
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   634
    def __str__ (self) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   635
        return "Image `%s' in `%s'" % (self.name, self.dir.path)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   636
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   637
def int2key (id) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   638
    """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   639
        Turn an integer into a short-as-possible url-safe string
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   640
    """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   641
    for type in ('B', 'H', 'I') :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   642
        try :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   643
            return base64.b64encode(struct.pack(type, id), '-_').rstrip('=')
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   644
        except struct.error :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   645
            continue
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   646
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   647
    raise Exception("ID overflow: %s" % id)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   648
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   649
def updateShorturlDb (root) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   650
    """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   651
        DeGAL <= 0.2 used a simple key => path mapping, but now we use
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   652
        something more structured, key => (type, dirpath, fname), where
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   653
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   654
        type    - one of 'img', 'dir'
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   655
        dirpath - the path to the directory, e.g. '.', './foobar', './foobar/quux'
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   656
        fname   - the filename, one of '', 'DSC9839.JPG', 'this.png', etc.
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   657
    """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   658
5
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   659
    db = shelve.open('shorturls2', 'c', writeback=True)
1
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   660
    
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   661
    id = db.get('_id', 1)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   662
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   663
    dirqueue = [root]
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   664
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   665
    # dict of path -> obj
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   666
    paths = {}
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   667
5
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   668
    index.info("Processing ShortURLs...")
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   669
1
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   670
    while dirqueue :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   671
        dir = dirqueue.pop(0)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   672
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   673
        dirqueue.extend(dir.subdirs.itervalues())
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   674
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   675
        if dir.alive :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   676
            paths[dir.path] = dir
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   677
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   678
        for img in dir.images.itervalues() :
5
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   679
            paths[img.path] = img
1
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   680
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   681
    for key in db.keys() :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   682
        if key.startswith('_') :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   683
            continue
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   684
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   685
        type, dirpath, fname = db[key]
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   686
        
5
156cdfffef8e fix shorturls to dirs, and a new feature: pagination for index pages
terom
parents: 1
diff changeset
   687
        path = os.path.join(dirpath, fname).rstrip('/')
1
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   688
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   689
        try :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   690
            paths.pop(path).shorturl_code = key
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   691
            index.debug("Code for `%s' is %s", path, key)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   692
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   693
        except KeyError :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   694
            index.debug("Path `%s' in DB does not exist?", path)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   695
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   696
    for obj in paths.itervalues() :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   697
        key = int2key(id)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   698
        id += 1
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   699
        
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   700
        index.info("Alloc code `%s' for `%s'", key, obj.html_path)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   701
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   702
        obj.shorturl_code = key
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   703
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   704
        db[key] = obj.getObjInfo()
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   705
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   706
    db['_id'] = id
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   707
    db.close()
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   708
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   709
def main (targets=()) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   710
    root_filter = {}
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   711
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   712
    for target in targets :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   713
        f = root_filter
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   714
        for path_part in target.split('/') :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   715
            if path_part :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   716
                if path_part not in f :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   717
                    f[path_part] = {}
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   718
                f = f[path_part]
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   719
    
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   720
    index.debug('Filter: %s', root_filter)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   721
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   722
    root = Folder()
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   723
    root.index(root_filter)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   724
    root.prepare()
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   725
    updateShorturlDb(root)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   726
    root.render()
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   727
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   728
def fmtFilesize (size) :
10
e8143d784b77 SI units/ISO date-time format
terom
parents: 7
diff changeset
   729
    return utils.formatbytes(size, forcekb=False, largestonly=True, kiloname='KiB', meganame='MiB', bytename='B', nospace=False)
1
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   730
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   731
def fmtTimestamp (ts) :
10
e8143d784b77 SI units/ISO date-time format
terom
parents: 7
diff changeset
   732
    return datetime.fromtimestamp(ts).strftime("%Y-%m-%d %H:%M:%S")
1
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   733
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   734
if __name__ == '__main__' :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   735
    from sys import argv
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   736
    argv.pop(0)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   737
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   738
    main(argv)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   739