lib/shorturl.py
author terom
Thu, 31 Jan 2008 17:28:02 +0000
changeset 26 81d6679d50d0
parent 22 72696ca68c34
child 28 70b6c13d084f
permissions -rw-r--r--
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
14
4b5478da5850 remove hanging utils.py in root, and add GPL license header to all .py files
terom
parents: 12
diff changeset
     1
# DeGAL - A pretty simple web image gallery
4b5478da5850 remove hanging utils.py in root, and add GPL license header to all .py files
terom
parents: 12
diff changeset
     2
# Copyright (C) 2007 Tero Marttila
4b5478da5850 remove hanging utils.py in root, and add GPL license header to all .py files
terom
parents: 12
diff changeset
     3
# http://marttila.de/~terom/degal/
4b5478da5850 remove hanging utils.py in root, and add GPL license header to all .py files
terom
parents: 12
diff changeset
     4
#
4b5478da5850 remove hanging utils.py in root, and add GPL license header to all .py files
terom
parents: 12
diff changeset
     5
# This program is free software; you can redistribute it and/or modify
4b5478da5850 remove hanging utils.py in root, and add GPL license header to all .py files
terom
parents: 12
diff changeset
     6
# it under the terms of the GNU General Public License as published by
4b5478da5850 remove hanging utils.py in root, and add GPL license header to all .py files
terom
parents: 12
diff changeset
     7
# the Free Software Foundation; either version 2 of the License, or
4b5478da5850 remove hanging utils.py in root, and add GPL license header to all .py files
terom
parents: 12
diff changeset
     8
# (at your option) any later version.
4b5478da5850 remove hanging utils.py in root, and add GPL license header to all .py files
terom
parents: 12
diff changeset
     9
#
4b5478da5850 remove hanging utils.py in root, and add GPL license header to all .py files
terom
parents: 12
diff changeset
    10
# This program is distributed in the hope that it will be useful,
4b5478da5850 remove hanging utils.py in root, and add GPL license header to all .py files
terom
parents: 12
diff changeset
    11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
4b5478da5850 remove hanging utils.py in root, and add GPL license header to all .py files
terom
parents: 12
diff changeset
    12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4b5478da5850 remove hanging utils.py in root, and add GPL license header to all .py files
terom
parents: 12
diff changeset
    13
# GNU General Public License for more details.
4b5478da5850 remove hanging utils.py in root, and add GPL license header to all .py files
terom
parents: 12
diff changeset
    14
#
4b5478da5850 remove hanging utils.py in root, and add GPL license header to all .py files
terom
parents: 12
diff changeset
    15
# You should have received a copy of the GNU General Public License
4b5478da5850 remove hanging utils.py in root, and add GPL license header to all .py files
terom
parents: 12
diff changeset
    16
# along with this program; if not, write to the
4b5478da5850 remove hanging utils.py in root, and add GPL license header to all .py files
terom
parents: 12
diff changeset
    17
# Free Software Foundation, Inc.,
4b5478da5850 remove hanging utils.py in root, and add GPL license header to all .py files
terom
parents: 12
diff changeset
    18
# 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
4b5478da5850 remove hanging utils.py in root, and add GPL license header to all .py files
terom
parents: 12
diff changeset
    19
#
4b5478da5850 remove hanging utils.py in root, and add GPL license header to all .py files
terom
parents: 12
diff changeset
    20
12
c2d8e9a754a1 Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff changeset
    21
import struct
c2d8e9a754a1 Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff changeset
    22
import base64
c2d8e9a754a1 Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff changeset
    23
import shelve
c2d8e9a754a1 Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff changeset
    24
import os.path
c2d8e9a754a1 Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff changeset
    25
c2d8e9a754a1 Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff changeset
    26
from log import index
c2d8e9a754a1 Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff changeset
    27
26
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
    28
import utils, db, helpers, folder, image
22
72696ca68c34 use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents: 20
diff changeset
    29
12
c2d8e9a754a1 Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff changeset
    30
def int2key (id) :
c2d8e9a754a1 Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff changeset
    31
    """
c2d8e9a754a1 Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff changeset
    32
        Turn an integer into a short-as-possible url-safe string
c2d8e9a754a1 Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff changeset
    33
    """
c2d8e9a754a1 Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff changeset
    34
    for type in ('B', 'H', 'I') :
c2d8e9a754a1 Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff changeset
    35
        try :
c2d8e9a754a1 Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff changeset
    36
            return base64.b64encode(struct.pack(type, id), '-_').rstrip('=')
c2d8e9a754a1 Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff changeset
    37
        except struct.error :
c2d8e9a754a1 Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff changeset
    38
            continue
c2d8e9a754a1 Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff changeset
    39
c2d8e9a754a1 Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff changeset
    40
    raise Exception("ID overflow: %s" % id)
c2d8e9a754a1 Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff changeset
    41
22
72696ca68c34 use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents: 20
diff changeset
    42
def key2int (key) :
72696ca68c34 use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents: 20
diff changeset
    43
    # base64 ignores extra padding, but if it doesn't, it's (4 - len%4), if len%4 != 0
26
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
    44
    # and it breaks on unicode strings
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
    45
    bytes = base64.b64decode(str(key + '='*6), '-_')
22
72696ca68c34 use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents: 20
diff changeset
    46
    
72696ca68c34 use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents: 20
diff changeset
    47
    type = {
72696ca68c34 use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents: 20
diff changeset
    48
        1: 'B',
72696ca68c34 use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents: 20
diff changeset
    49
        2: 'H',
72696ca68c34 use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents: 20
diff changeset
    50
        4: 'I',
72696ca68c34 use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents: 20
diff changeset
    51
    }[len(bytes)]
72696ca68c34 use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents: 20
diff changeset
    52
72696ca68c34 use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents: 20
diff changeset
    53
    return struct.unpack(type, bytes)[0]
72696ca68c34 use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents: 20
diff changeset
    54
19
8d3ffd87cb0b * move cgi-bin to de-cgi-bin so it doesn't conflict with my default alias... need to come up with a real solution to this
terom
parents: 14
diff changeset
    55
class DB (object) :
8d3ffd87cb0b * move cgi-bin to de-cgi-bin so it doesn't conflict with my default alias... need to come up with a real solution to this
terom
parents: 14
diff changeset
    56
    def __init__ (self, read_only=True) :
8d3ffd87cb0b * move cgi-bin to de-cgi-bin so it doesn't conflict with my default alias... need to come up with a real solution to this
terom
parents: 14
diff changeset
    57
        self.db = shelve.open('shorturls2', read_only and 'r' or 'c')
8d3ffd87cb0b * move cgi-bin to de-cgi-bin so it doesn't conflict with my default alias... need to come up with a real solution to this
terom
parents: 14
diff changeset
    58
8d3ffd87cb0b * move cgi-bin to de-cgi-bin so it doesn't conflict with my default alias... need to come up with a real solution to this
terom
parents: 14
diff changeset
    59
    def html_path (self, key, index) :
8d3ffd87cb0b * move cgi-bin to de-cgi-bin so it doesn't conflict with my default alias... need to come up with a real solution to this
terom
parents: 14
diff changeset
    60
        type, dirpath, fname = self.db[key]
8d3ffd87cb0b * move cgi-bin to de-cgi-bin so it doesn't conflict with my default alias... need to come up with a real solution to this
terom
parents: 14
diff changeset
    61
8d3ffd87cb0b * move cgi-bin to de-cgi-bin so it doesn't conflict with my default alias... need to come up with a real solution to this
terom
parents: 14
diff changeset
    62
        if type == 'img' :
8d3ffd87cb0b * move cgi-bin to de-cgi-bin so it doesn't conflict with my default alias... need to come up with a real solution to this
terom
parents: 14
diff changeset
    63
            fname += '.html'
8d3ffd87cb0b * move cgi-bin to de-cgi-bin so it doesn't conflict with my default alias... need to come up with a real solution to this
terom
parents: 14
diff changeset
    64
        elif type == 'dir' :
8d3ffd87cb0b * move cgi-bin to de-cgi-bin so it doesn't conflict with my default alias... need to come up with a real solution to this
terom
parents: 14
diff changeset
    65
            fname = ''
8d3ffd87cb0b * move cgi-bin to de-cgi-bin so it doesn't conflict with my default alias... need to come up with a real solution to this
terom
parents: 14
diff changeset
    66
8d3ffd87cb0b * move cgi-bin to de-cgi-bin so it doesn't conflict with my default alias... need to come up with a real solution to this
terom
parents: 14
diff changeset
    67
        if index :
8d3ffd87cb0b * move cgi-bin to de-cgi-bin so it doesn't conflict with my default alias... need to come up with a real solution to this
terom
parents: 14
diff changeset
    68
            dirpath = '../%s' % dirpath
8d3ffd87cb0b * move cgi-bin to de-cgi-bin so it doesn't conflict with my default alias... need to come up with a real solution to this
terom
parents: 14
diff changeset
    69
            
8d3ffd87cb0b * move cgi-bin to de-cgi-bin so it doesn't conflict with my default alias... need to come up with a real solution to this
terom
parents: 14
diff changeset
    70
            if type == 'dir' and index > 1 : 
8d3ffd87cb0b * move cgi-bin to de-cgi-bin so it doesn't conflict with my default alias... need to come up with a real solution to this
terom
parents: 14
diff changeset
    71
                fname = 'index_%s.html' % (index - 1)
8d3ffd87cb0b * move cgi-bin to de-cgi-bin so it doesn't conflict with my default alias... need to come up with a real solution to this
terom
parents: 14
diff changeset
    72
8d3ffd87cb0b * move cgi-bin to de-cgi-bin so it doesn't conflict with my default alias... need to come up with a real solution to this
terom
parents: 14
diff changeset
    73
        return os.path.join(dirpath, fname)
8d3ffd87cb0b * move cgi-bin to de-cgi-bin so it doesn't conflict with my default alias... need to come up with a real solution to this
terom
parents: 14
diff changeset
    74
   
8d3ffd87cb0b * move cgi-bin to de-cgi-bin so it doesn't conflict with my default alias... need to come up with a real solution to this
terom
parents: 14
diff changeset
    75
    def image_info (self, key) :
8d3ffd87cb0b * move cgi-bin to de-cgi-bin so it doesn't conflict with my default alias... need to come up with a real solution to this
terom
parents: 14
diff changeset
    76
        type, dirpath, fname = self.db[key]
8d3ffd87cb0b * move cgi-bin to de-cgi-bin so it doesn't conflict with my default alias... need to come up with a real solution to this
terom
parents: 14
diff changeset
    77
8d3ffd87cb0b * move cgi-bin to de-cgi-bin so it doesn't conflict with my default alias... need to come up with a real solution to this
terom
parents: 14
diff changeset
    78
        if type != 'img' :
8d3ffd87cb0b * move cgi-bin to de-cgi-bin so it doesn't conflict with my default alias... need to come up with a real solution to this
terom
parents: 14
diff changeset
    79
            raise ValueError("%s is not an img" % key)
8d3ffd87cb0b * move cgi-bin to de-cgi-bin so it doesn't conflict with my default alias... need to come up with a real solution to this
terom
parents: 14
diff changeset
    80
8d3ffd87cb0b * move cgi-bin to de-cgi-bin so it doesn't conflict with my default alias... need to come up with a real solution to this
terom
parents: 14
diff changeset
    81
        return dirpath, fname
20
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents: 19
diff changeset
    82
    
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents: 19
diff changeset
    83
    def shorturls_for (self, paths) :
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents: 19
diff changeset
    84
        ret = []
19
8d3ffd87cb0b * move cgi-bin to de-cgi-bin so it doesn't conflict with my default alias... need to come up with a real solution to this
terom
parents: 14
diff changeset
    85
20
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents: 19
diff changeset
    86
        for key in self.db.keys() :
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents: 19
diff changeset
    87
            if key.startswith('_') :
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents: 19
diff changeset
    88
                continue
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents: 19
diff changeset
    89
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents: 19
diff changeset
    90
            type, dir, fname = self.db[key]
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents: 19
diff changeset
    91
            path = os.path.join(dir.lstrip('.').lstrip('/'), fname) 
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents: 19
diff changeset
    92
            if path in paths :
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents: 19
diff changeset
    93
                ret.append(key)
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents: 19
diff changeset
    94
                paths.remove(path)
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents: 19
diff changeset
    95
        
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents: 19
diff changeset
    96
        if paths :
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents: 19
diff changeset
    97
            raise ValueError("Paths not found: %s" % " ".join(paths))
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents: 19
diff changeset
    98
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents: 19
diff changeset
    99
        return ret
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents: 19
diff changeset
   100
22
72696ca68c34 use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents: 20
diff changeset
   101
def html_path (key, index=None) :
26
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   102
    dir, fname = node_info(key)
22
72696ca68c34 use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents: 20
diff changeset
   103
26
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   104
    if fname :
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   105
        return utils.url(dir, fname + '.html')
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   106
    else :
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   107
        return utils.url(dir, helpers.url_for_page(index or 0))
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   108
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   109
def node_info (key) :
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   110
    res = db.select("""SELECT dirpath, filename FROM nodes WHERE id=?""", key2int(key)).fetchone()
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   111
    
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   112
    if res :
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   113
        return res
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   114
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   115
    else :
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   116
        raise KeyError(key)
22
72696ca68c34 use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents: 20
diff changeset
   117
72696ca68c34 use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents: 20
diff changeset
   118
def image_info (key) :
72696ca68c34 use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents: 20
diff changeset
   119
    res = db.select("""SELECT dirpath, filename FROM images WHERE id=?""", key2int(key)).fetchone()
72696ca68c34 use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents: 20
diff changeset
   120
    
72696ca68c34 use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents: 20
diff changeset
   121
    if res :
72696ca68c34 use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents: 20
diff changeset
   122
        return res
72696ca68c34 use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents: 20
diff changeset
   123
72696ca68c34 use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents: 20
diff changeset
   124
    else :
72696ca68c34 use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents: 20
diff changeset
   125
        raise KeyError(key)
72696ca68c34 use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents: 20
diff changeset
   126
   
72696ca68c34 use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents: 20
diff changeset
   127
def get_images (keys) :
72696ca68c34 use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents: 20
diff changeset
   128
    res = [db.select("""SELECT dirpath, filename FROM images WHERE id=?""", key2int(key)).fetchone() for key in keys]
72696ca68c34 use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents: 20
diff changeset
   129
72696ca68c34 use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents: 20
diff changeset
   130
    # don't mind if we don't get as many as we asked for?
72696ca68c34 use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents: 20
diff changeset
   131
    if res :
72696ca68c34 use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents: 20
diff changeset
   132
        return res
72696ca68c34 use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents: 20
diff changeset
   133
72696ca68c34 use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents: 20
diff changeset
   134
    else :
72696ca68c34 use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents: 20
diff changeset
   135
        raise KeyError(keys)
72696ca68c34 use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents: 20
diff changeset
   136
26
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   137
def _got_obj_key (obj, id) :
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   138
    key = int2key(id)
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   139
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   140
    obj.shorturl_code = key
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   141
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   142
    if isinstance(obj, folder.Folder) :
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   143
        dir, fname = utils.strip_path(obj.path), ''
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   144
    elif isinstance(obj, image.Image) :
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   145
        dir, fname = utils.strip_path(obj.dir.path), obj.name
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   146
    else :
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   147
        assert(False, "%r %r" % (obj, id))
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   148
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   149
    index.info("img %50s %15s = %d %s", dir, fname, id, key)
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   150
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   151
def updateDB (root) :
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   152
    """
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   153
        Update the SQL database
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   154
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   155
        type    - one of 'img', 'dir'
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   156
        dirpath - the path to the directory, e.g. '.', './foobar', './foobar/quux'
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   157
        fname   - the filename, one of '', 'DSC9839.JPG', 'this.png', etc.
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   158
    """
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   159
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   160
    dirqueue = [root]
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   161
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   162
    # dict of (dir, fname) -> obj
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   163
    paths = {}
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   164
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   165
    index.info("Processing ShortURLs...")
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   166
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   167
    while dirqueue :
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   168
        dir = dirqueue.pop(0)
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   169
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   170
        dirqueue.extend(dir.subdirs.itervalues())
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   171
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   172
        if dir.alive :
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   173
            pathtuple = (utils.strip_path(dir.path), '')
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   174
            
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   175
            index.debug("dir %50s", pathtuple[0])
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   176
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   177
            paths[pathtuple] = dir
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   178
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   179
        for img in dir.images.itervalues() :
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   180
            pathtuple = (utils.strip_path(img.dir.path), img.name)
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   181
            
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   182
            index.debug("img %50s %15s", *pathtuple)
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   183
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   184
            paths[pathtuple] = img
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   185
    
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   186
    print "%d nodes:" % (len(paths))
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   187
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   188
    for (id, dir, fname) in db.select("SELECT id, dirpath, filename FROM nodes") :
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   189
        try :
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   190
            obj = paths.pop((dir, fname))
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   191
            key = int2key(id)
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   192
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   193
            obj.shorturl_code = key
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   194
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   195
            index.debug("%s %50s %15s -> %d %s", dir and "img" or "dir", dir, fname, id, key)
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   196
        
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   197
        except KeyError :
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   198
            pass
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   199
#            index.warning("non-existant node (%d, %s, %s) in db", id, dir, fname)
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   200
    
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   201
    print "%d NEW nodes:" % (len(paths))
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   202
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   203
    db.insert_many(
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   204
        _got_obj_key,
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   205
        "INSERT INTO nodes (dirpath, filename) VALUES (?, ?)",
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   206
        ((obj, (path, fname)) for ((path, fname), obj) in paths.iteritems())
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   207
    )
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 22
diff changeset
   208