lib/shorturl.py
author terom
Wed, 16 Jan 2008 16:28:00 +0000
changeset 20 6c774496bb00
parent 19 8d3ffd87cb0b
child 22 72696ca68c34
permissions -rw-r--r--
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
# 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()

class DB (object) :
    def __init__ (self, read_only=True) :
        self.db = shelve.open('shorturls2', read_only and 'r' or 'c')

    def html_path (self, key, index) :
        type, dirpath, fname = self.db[key]

        if type == 'img' :
            fname += '.html'
        elif type == 'dir' :
            fname = ''

        if index :
            dirpath = '../%s' % dirpath
            
            if type == 'dir' and index > 1 : 
                fname = 'index_%s.html' % (index - 1)

        return os.path.join(dirpath, fname)
   
    def image_info (self, key) :
        type, dirpath, fname = self.db[key]

        if type != 'img' :
            raise ValueError("%s is not an img" % key)

        return dirpath, fname
    
    def shorturls_for (self, paths) :
        ret = []

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

            type, dir, fname = self.db[key]
            path = os.path.join(dir.lstrip('.').lstrip('/'), fname) 
            if path in paths :
                ret.append(key)
                paths.remove(path)
        
        if paths :
            raise ValueError("Paths not found: %s" % " ".join(paths))

        return ret