terom@14: # DeGAL - A pretty simple web image gallery terom@14: # Copyright (C) 2007 Tero Marttila terom@14: # http://marttila.de/~terom/degal/ terom@14: # terom@14: # This program is free software; you can redistribute it and/or modify terom@14: # it under the terms of the GNU General Public License as published by terom@14: # the Free Software Foundation; either version 2 of the License, or terom@14: # (at your option) any later version. terom@14: # terom@14: # This program is distributed in the hope that it will be useful, terom@14: # but WITHOUT ANY WARRANTY; without even the implied warranty of terom@14: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terom@14: # GNU General Public License for more details. terom@14: # terom@14: # You should have received a copy of the GNU General Public License terom@14: # along with this program; if not, write to the terom@14: # Free Software Foundation, Inc., terom@14: # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. terom@14: # terom@14: terom@12: import struct terom@12: import base64 terom@12: import shelve terom@12: import os.path terom@12: terom@12: from log import index terom@12: terom@12: def int2key (id) : terom@12: """ terom@12: Turn an integer into a short-as-possible url-safe string terom@12: """ terom@12: for type in ('B', 'H', 'I') : terom@12: try : terom@12: return base64.b64encode(struct.pack(type, id), '-_').rstrip('=') terom@12: except struct.error : terom@12: continue terom@12: terom@12: raise Exception("ID overflow: %s" % id) terom@12: terom@12: def updateDB (root) : terom@12: """ terom@12: DeGAL <= 0.2 used a simple key => path mapping, but now we use terom@12: something more structured, key => (type, dirpath, fname), where terom@12: terom@12: type - one of 'img', 'dir' terom@12: dirpath - the path to the directory, e.g. '.', './foobar', './foobar/quux' terom@12: fname - the filename, one of '', 'DSC9839.JPG', 'this.png', etc. terom@12: """ terom@12: terom@12: db = shelve.open('shorturls2', 'c', writeback=True) terom@12: terom@12: id = db.get('_id', 1) terom@12: terom@12: dirqueue = [root] terom@12: terom@12: # dict of path -> obj terom@12: paths = {} terom@12: terom@12: index.info("Processing ShortURLs...") terom@12: terom@12: while dirqueue : terom@12: dir = dirqueue.pop(0) terom@12: terom@12: dirqueue.extend(dir.subdirs.itervalues()) terom@12: terom@12: if dir.alive : terom@12: paths[dir.path] = dir terom@12: terom@12: for img in dir.images.itervalues() : terom@12: paths[img.path] = img terom@12: terom@12: for key in db.keys() : terom@12: if key.startswith('_') : terom@12: continue terom@12: terom@12: type, dirpath, fname = db[key] terom@12: terom@12: path = os.path.join(dirpath, fname).rstrip(os.sep) terom@12: terom@12: try : terom@12: paths.pop(path).shorturl_code = key terom@12: index.debug("Code for `%s' is %s", path, key) terom@12: terom@12: except KeyError : terom@12: index.debug("Path `%s' in DB does not exist?", path) terom@12: terom@12: for obj in paths.itervalues() : terom@12: key = int2key(id) terom@12: id += 1 terom@12: terom@12: index.info("Alloc code `%s' for `%s'", key, obj.html_path) terom@12: terom@12: obj.shorturl_code = key terom@12: terom@12: db[key] = obj.getObjInfo() terom@12: terom@12: db['_id'] = id terom@12: db.close() terom@19: terom@19: class DB (object) : terom@19: def __init__ (self, read_only=True) : terom@19: self.db = shelve.open('shorturls2', read_only and 'r' or 'c') terom@19: terom@19: def html_path (self, key, index) : terom@19: type, dirpath, fname = self.db[key] terom@19: terom@19: if type == 'img' : terom@19: fname += '.html' terom@19: elif type == 'dir' : terom@19: fname = '' terom@19: terom@19: if index : terom@19: dirpath = '../%s' % dirpath terom@19: terom@19: if type == 'dir' and index > 1 : terom@19: fname = 'index_%s.html' % (index - 1) terom@19: terom@19: return os.path.join(dirpath, fname) terom@19: terom@19: def image_info (self, key) : terom@19: type, dirpath, fname = self.db[key] terom@19: terom@19: if type != 'img' : terom@19: raise ValueError("%s is not an img" % key) terom@19: terom@19: return dirpath, fname terom@20: terom@20: def shorturls_for (self, paths) : terom@20: ret = [] terom@19: terom@20: for key in self.db.keys() : terom@20: if key.startswith('_') : terom@20: continue terom@20: terom@20: type, dir, fname = self.db[key] terom@20: path = os.path.join(dir.lstrip('.').lstrip('/'), fname) terom@20: if path in paths : terom@20: ret.append(key) terom@20: paths.remove(path) terom@20: terom@20: if paths : terom@20: raise ValueError("Paths not found: %s" % " ".join(paths)) terom@20: terom@20: return ret terom@20: