lib/shorturl.py
author terom
Sat, 22 Dec 2007 21:31:01 +0000
changeset 18 46536daf9e04
parent 14 4b5478da5850
child 19 8d3ffd87cb0b
permissions -rw-r--r--
fix bug with shorturls in gallery.html, and some horrible, horrible crap for the CGI stuff - I need to drop everything now and take a fresh look tomorrow before it gets even worse
# DeGAL - A pretty simple web image gallery
# Copyright (C) 2007 Tero Marttila
# http://marttila.de/~terom/degal/
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the
# Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#

import struct
import base64
import shelve
import os.path

from log import index

def int2key (id) :
    """
        Turn an integer into a short-as-possible url-safe string
    """
    for type in ('B', 'H', 'I') :
        try :
            return base64.b64encode(struct.pack(type, id), '-_').rstrip('=')
        except struct.error :
            continue

    raise Exception("ID overflow: %s" % id)

def updateDB (root) :
    """
        DeGAL <= 0.2 used a simple key => path mapping, but now we use
        something more structured, key => (type, dirpath, fname), where

        type    - one of 'img', 'dir'
        dirpath - the path to the directory, e.g. '.', './foobar', './foobar/quux'
        fname   - the filename, one of '', 'DSC9839.JPG', 'this.png', etc.
    """

    db = shelve.open('shorturls2', 'c', writeback=True)
    
    id = db.get('_id', 1)

    dirqueue = [root]

    # dict of path -> obj
    paths = {}

    index.info("Processing ShortURLs...")

    while dirqueue :
        dir = dirqueue.pop(0)

        dirqueue.extend(dir.subdirs.itervalues())

        if dir.alive :
            paths[dir.path] = dir

        for img in dir.images.itervalues() :
            paths[img.path] = img

    for key in db.keys() :
        if key.startswith('_') :
            continue

        type, dirpath, fname = db[key]
        
        path = os.path.join(dirpath, fname).rstrip(os.sep)

        try :
            paths.pop(path).shorturl_code = key
            index.debug("Code for `%s' is %s", path, key)

        except KeyError :
            index.debug("Path `%s' in DB does not exist?", path)

    for obj in paths.itervalues() :
        key = int2key(id)
        id += 1
        
        index.info("Alloc code `%s' for `%s'", key, obj.html_path)

        obj.shorturl_code = key

        db[key] = obj.getObjInfo()

    db['_id'] = id
    db.close()