degal.py
author terom
Thu, 08 Nov 2007 17:06:03 +0000
changeset 1 740133ab6353
child 5 156cdfffef8e
permissions -rwxr-xr-x
initial code, somewhere between 0.2 and 0.5
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
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   109
def isImage (fname) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   110
    """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   111
        Is the given filename likely to be an image file?
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   112
    """
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
    fname = fname.lower()
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   115
    base, ext = os.path.splitext(fname)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   116
    ext = ext.lstrip('.')
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   117
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   118
    return ext in IMAGE_EXTS
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   119
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   120
def link (url, title) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   121
    """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   122
        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
   123
    """
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
    return "<a href='%s'>%s</a>" % (urllib.quote(url), title)
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
def dirUp (count=1) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   128
    """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   129
        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
   130
    """
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
    if not count :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   133
        return '.'
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
    return os.path.join(*(['..']*count))
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   136
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   137
def readTitleDescr (path) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   138
    """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   139
        Read a title.txt or <imgname>.txt file
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   140
    """
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
    if os.path.exists(path) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   143
        content = readFile(path)
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 '---' in content :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   146
            title, descr = content.split('---', 1)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   147
        else :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   148
            title, descr = content, ''
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   149
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   150
        return title.strip(), descr.strip()
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   151
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   152
    return None, None
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   153
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   154
class Folder (object) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   155
    def __init__ (self, name='.', parent=None) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   156
        # the directory name
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   157
        self.name = name
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   158
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   159
        # our parent Folder, or None
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   160
        self.parent = parent
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
        # 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
   163
        if parent and name :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   164
            self.path = parent.pathFor(name)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   165
        else :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   166
            self.path = name
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   167
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   168
        # the url-path to the index.html file
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   169
        self.html_path = self.path
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
        # dict of fname -> Folder
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   172
        self.subdirs = {}
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 -> Image
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   175
        self.images = {}
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
        # our human-friendly title
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   178
        self.title = None
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 long-winded description
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   181
        self.descr = ''
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
        # is this folder non-empty?
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   184
        self.alive = None
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
        # self.images.values(), but sorted by filename
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   187
        self.sorted_images = []
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
        # the ShortURL key to this dir
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   190
        self.shorturl_code = None
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
        # were we filtered out?
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   193
        self.filtered = False
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
    def pathFor (self, *fnames) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   196
        """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   197
            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
   198
        """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   199
        return os.path.join(self.path, *fnames)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   200
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   201
    def index (self, filters=None) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   202
        """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   203
            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
   204
            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
   205
            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
   206
            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
   207
            method.
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   208
        """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   209
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   210
        index.info("Indexing %s", self.path)
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
        if filters :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   213
            self.filtered = True
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
        # iterate through listdir
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   216
        for fname in os.listdir(self.path) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   217
            # the full filesystem path to it
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   218
            fpath = self.pathFor(fname)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   219
            
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   220
            # ignore dotfiles
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   221
            if fname.startswith('.') :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   222
                index.debug("Skipping dotfile %s", fname)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   223
                continue
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   224
            
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   225
            # apply filters
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   226
            if filters :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   227
                if fname in filters :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   228
                    next_filter = filters[fname]
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   229
                else :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   230
                    index.debug("Skip `%s' as we have a filter", fname)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   231
                    continue
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
                next_filter = None
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   234
                
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   235
            # recurse into subdirs, but not thumbs/previews
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   236
            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
   237
                index.debug("Found subdir %s", fpath)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   238
                f = self.subdirs[fname] = Folder(fname, self)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   239
                f.index(next_filter)   # recursion
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   240
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   241
            # handle images
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   242
            elif os.path.isfile(fpath) and isImage(fname) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   243
                index.debug("Found image %s", fname)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   244
                self.images[fname] = Image(self, fname)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   245
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   246
            # ignore everything else
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   247
            else :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   248
                index.debug("Ignoring file %s", fname)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   249
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   250
    def prepare (self) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   251
        """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   252
            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
   253
            into subdirs
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
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   256
        prepare.info("Preparing dir %s", self.path)
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
        # is this folder non-empty?
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   259
        alive = False
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
        # 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
   262
        if self.images :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   263
            # folder is non-empty
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   264
            alive = True
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   265
            prepare.info("Have %d images", len(self.images))
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   266
            
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   267
            # create the thumb/preview dirs if needed
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   268
            for dir in (THUMB_DIR, PREVIEW_DIR) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   269
                prepare.debug("Checking for existance of %s dir", dir)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   270
                path = self.pathFor(dir)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   271
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   272
                if not os.path.isdir(path) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   273
                    prepare.info("Creating dir %s", path)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   274
                    os.mkdir(path)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   275
            
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   276
            # sort the images
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   277
            fnames = self.images.keys()
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   278
            fnames.sort()
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   279
            
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   280
            prev = None
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   281
            
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   282
            # link them together and prepare them
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   283
            for fname in fnames :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   284
                img = self.images[fname]
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   285
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   286
                prepare.debug("Linking %s <-> %s", prev, img)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   287
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   288
                img.prev = prev
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   289
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   290
                if prev :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   291
                    prev.next = img
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
                prev = img
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   294
                
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   295
                img.prepare()
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   296
                
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   297
                # add to the sorted images list
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   298
                self.sorted_images.append(img)
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
        # prepare subdirs
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   301
        if self.subdirs :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   302
            prepare.info("Have %d subdirs", len(self.subdirs))
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   303
            
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   304
            # 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
   305
            for dir in self.subdirs.itervalues() :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   306
                if dir.prepare() :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   307
                    alive = True
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   308
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   309
        # figure out our title
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   310
        title_path = self.pathFor(TITLE_FILE)
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
        title, descr = readTitleDescr(title_path)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   313
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   314
        if title :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   315
            prepare.info("Found title/descr")
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   316
            self.title = title
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   317
            self.descr = descr
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   318
            alive = True
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   319
        
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   320
        # default title for the root dir
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   321
        elif self.name == '.' :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   322
            self.title = 'Index'
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   323
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   324
        else :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   325
            self.title = self.name
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
        prepare.debug("Our title is '%s'", self.title)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   328
        
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   329
        # lol ded
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   330
        if not alive :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   331
            prepare.info("Dir %s is not alive", self.path)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   332
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   333
        self.alive = alive
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   334
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   335
        return alive
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   336
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   337
    def getObjInfo (self) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   338
        """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   339
            Metadata for shorturls2.db
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   340
        """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   341
        return 'dir', self.path, ''
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   342
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   343
    def linkTag (self) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   344
        """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   345
            A text-link to this dir
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   346
        """
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
        return link(self.path, self.title)
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
    def breadcrumb (self) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   351
        """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   352
            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
   353
        """
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
        f = self
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   356
        b = []
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   357
        d = 0
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   358
        
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   359
        while f :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   360
            b.insert(0, (dirUp(d), f.title))
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
            d += 1
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   363
            f = f.parent
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
        return b
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   366
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   367
    def inRoot (self, *fnames) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   368
        """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   369
            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
   370
        """
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
        c = len(self.path.split('/')) - 1
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
        return os.path.join(*((['..']*c) + list(fnames)))
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   375
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   376
    def render (self) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   377
        """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   378
            Render the index.html, Images, and recurse into subdirs
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   379
        """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   380
        
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   381
        # ded folders are skipped
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   382
        if not self.alive :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   383
            render.info("Skipping dir %s", self.path)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   384
            return
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   385
        
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   386
        # 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
   387
        if self.filtered :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   388
            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
   389
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   390
        else :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   391
            # sort the subdirs
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   392
            subdirs = self.subdirs.items()
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   393
            subdirs.sort()
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   394
            
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   395
            # generate the <a href=""></a>'s for the subdirs
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   396
            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
   397
            
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   398
            # stick them into a list
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   399
            if subdir_linkTags :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   400
                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
   401
            else :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   402
                directories = ''
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   403
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   404
            render.info("Rendering %s", self.path)
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
            # render to index.html
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   407
            gallery_tpl.renderTo(self.pathFor('index.html'), 
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   408
                STYLE_URL=self.inRoot('style.css'),
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   409
                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
   410
                TITLE=self.title,
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   411
                DIRECTORIES=directories,
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   412
                CONTENT="".join([i.thumbImgTag() for i in self.sorted_images]),
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   413
                DESCR=self.descr,
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   414
                SHORTURL=self.inRoot('s', self.shorturl_code),
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   415
                SHORTURL_CODE=self.shorturl_code,
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   416
            )
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   417
        
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   418
        # render images
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   419
        for img in self.images.itervalues() :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   420
            img.render()
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   421
        
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   422
        # recurse into subdirs
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   423
        for dir in self.subdirs.itervalues() :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   424
            dir.render()
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   425
                    
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   426
class Image (object) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   427
    def __init__ (self, dir, name) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   428
        # the image filename, e.g. DSC3948.JPG
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   429
        self.name = name
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   430
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   431
        # the Folder object that we are in
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   432
        self.dir = dir
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   433
        
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   434
        # the relative path from the root to us
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   435
        self.path = dir.pathFor(name)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   436
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   437
        # the basename+ext, e.g. DSCR3948, .JPG
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   438
        self.base_name, self.ext = os.path.splitext(name)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   439
        
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   440
        # the root-relative paths to the thumb and preview images
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   441
        self.thumb_path = self.dir.pathFor(THUMB_DIR, self.name)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   442
        self.preview_path = self.dir.pathFor(PREVIEW_DIR, self.name)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   443
        
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   444
        # our user-friendly title
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   445
        self.title = name
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   446
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   447
        # our long-winded description
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   448
        self.descr = ''
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   449
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   450
        # the image before and after us, both may be None
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   451
        self.prev = self.next = None
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   452
        
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   453
        # 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
   454
        self.html_name = self.name + ".html"
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   455
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   456
        # the root-relative path to the gallery view
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   457
        self.html_path = self.dir.pathFor(self.html_name)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   458
        
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   459
        #
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   460
        # Figured out after prepare
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   461
        #
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   462
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   463
        # (w, h) tuple
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   464
        self.img_size = None
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   465
        
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   466
        # the ShortURL code for this image
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   467
        self.shorturl_code = None
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   468
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   469
        # 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
   470
        self.series_act = "add"
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   471
        self.series_verb = "Add to"
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   472
    
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   473
    def prepare (self) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   474
        """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   475
            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
   476
        """
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
        prepare.info("Preparing image %s", self.path)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   479
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   480
        # stat the image file to get the filesize and mtime
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   481
        st = os.stat(self.path)
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
        self.filesize = st.st_size
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   484
        self.timestamp = st.st_mtime
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
        # 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
   487
        img = PIL.Image.open(self.path)
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
        self.img_size = img.size
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   490
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   491
        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
   492
            # 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
   493
            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
   494
                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
   495
                
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   496
                # XXX: is this the most efficient way to do this?
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   497
                out_img = img.copy()
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   498
                out_img.thumbnail(geom, resample=True)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   499
                out_img.save(out_path)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   500
        
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   501
        # look for the metadata file
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   502
        title_path = self.dir.pathFor(self.base_name + '.txt')
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   503
        prepare.debug("Looking for title at %s", title_path)
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
        title, descr = readTitleDescr(title_path)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   506
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   507
        if title :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   508
            self.title = title
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   509
            self.descr = descr
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
            prepare.info("Found title `%s'", self.title)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   512
    
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   513
    def getObjInfo (self) :
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
            Metadata for shorturl2.db
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   516
        """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   517
        return 'img', self.dir.path, self.name
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   518
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   519
    def thumbImgTag (self) :
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
            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
   522
        """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   523
        return link(self.html_name, "<img src='%s' alt='%s' title='%s'>" % (os.path.join(THUMB_DIR, self.name), self.descr, self.title))
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 previewImgTag (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
            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
   528
        """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   529
        return link(self.name, "<img src='%s' alt='%s' title='%s'>" % (os.path.join(PREVIEW_DIR, self.name), self.descr, self.title))
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   530
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   531
    def linkTag (self) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   532
        """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   533
            a <a></a> text-link to this image
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
        return link(self.html_name, self.title)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   536
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   537
    def breadcrumb (self) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   538
        """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   539
            Returns a [(fname, title)] list of this image's parents
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
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   542
        f = self.dir
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   543
        b = [(self.html_name, self.title)]
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   544
        d = 0
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   545
        
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   546
        while f :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   547
            b.insert(0, (dirUp(d), f.title))
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   548
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   549
            d += 1
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   550
            f = f.parent
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   551
        
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   552
        return b
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   553
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   554
    def render (self) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   555
        """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   556
            Write out the .html file
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   557
        """
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
        render.info("Rendering image %s", self.path)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   560
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   561
        image_tpl.renderTo(self.html_path,
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   562
            STYLE_URL=self.dir.inRoot('style.css'),
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   563
            UP_URL=('.'),
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   564
            PREV_URL=(self.prev and self.prev.html_name or ''),
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   565
            NEXT_URL=(self.next and self.next.html_name or ''),
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   566
            FILE=self.name,
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   567
            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
   568
            TITLE=self.title,
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   569
            PREVIOUS_THUMB=(self.prev and self.prev.thumbImgTag() or ''),
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   570
            IMAGE=self.previewImgTag(),
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   571
            NEXT_THUMB=(self.next and self.next.thumbImgTag() or ''),
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   572
            DESCRIPTION=self.descr,
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   573
            IMGSIZE="%dx%d" % self.img_size,
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   574
            FILESIZE=fmtFilesize(self.filesize),
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   575
            TIMESTAMP=fmtTimestamp(self.timestamp),
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   576
            SHORTURL=self.dir.inRoot('s', self.shorturl_code),
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   577
            SHORTURL_CODE=self.shorturl_code,
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   578
            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
   579
            SERIES_VERB=self.series_verb,
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   580
        )   
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   581
    
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   582
    def __str__ (self) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   583
        return "Image `%s' in `%s'" % (self.name, self.dir.path)
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
def int2key (id) :
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
        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
   588
    """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   589
    for type in ('B', 'H', 'I') :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   590
        try :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   591
            return base64.b64encode(struct.pack(type, id), '-_').rstrip('=')
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   592
        except struct.error :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   593
            continue
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   594
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   595
    raise Exception("ID overflow: %s" % id)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   596
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   597
def updateShorturlDb (root) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   598
    """
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   599
        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
   600
        something more structured, key => (type, dirpath, fname), where
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   601
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   602
        type    - one of 'img', 'dir'
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   603
        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
   604
        fname   - the filename, one of '', 'DSC9839.JPG', 'this.png', etc.
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
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   607
    db = shelve.open('shorturls2', 'c')
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   608
    
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   609
    id = db.get('_id', 1)
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
    dirqueue = [root]
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
    # dict of path -> obj
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   614
    paths = {}
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   615
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   616
    while dirqueue :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   617
        dir = dirqueue.pop(0)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   618
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   619
        dirqueue.extend(dir.subdirs.itervalues())
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   620
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   621
        if dir.alive :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   622
            paths[dir.path] = dir
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   623
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   624
        for img in dir.images.itervalues() :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   625
            paths[img.html_path] = img
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   626
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   627
    for key in db.keys() :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   628
        if key.startswith('_') :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   629
            continue
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   630
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   631
        type, dirpath, fname = db[key]
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
        path = os.path.join(dirpath, fname)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   634
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   635
        try :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   636
            paths.pop(path).shorturl_code = key
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   637
            index.debug("Code for `%s' is %s", path, key)
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
        except KeyError :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   640
            index.debug("Path `%s' in DB does not exist?", path)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   641
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   642
    for obj in paths.itervalues() :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   643
        key = int2key(id)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   644
        id += 1
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   645
        
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   646
        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
   647
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   648
        obj.shorturl_code = key
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   649
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   650
        db[key] = obj.getObjInfo()
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   651
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   652
    db['_id'] = id
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   653
    db.close()
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   654
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   655
def main (targets=()) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   656
    root_filter = {}
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
    for target in targets :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   659
        f = root_filter
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   660
        for path_part in target.split('/') :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   661
            if path_part :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   662
                if path_part not in f :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   663
                    f[path_part] = {}
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   664
                f = f[path_part]
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   665
    
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   666
    index.debug('Filter: %s', root_filter)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   667
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   668
    root = Folder()
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   669
    root.index(root_filter)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   670
    root.prepare()
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   671
    updateShorturlDb(root)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   672
    root.render()
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   673
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   674
def fmtFilesize (size) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   675
    return utils.formatbytes(size, forcekb=False, largestonly=True, kiloname='K', meganame='M', bytename='B', nospace=True)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   676
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   677
def fmtTimestamp (ts) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   678
    return datetime.fromtimestamp(ts).strftime("%Y/%m/%d %H:%M")
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   679
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   680
if __name__ == '__main__' :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   681
    from sys import argv
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   682
    argv.pop(0)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   683
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   684
    main(argv)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   685