author | terom |
Mon, 17 Mar 2008 23:39:03 +0000 | |
changeset 31 | 09776792a91c |
parent 29 | 990300aa8010 |
permissions | -rw-r--r-- |
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 |
|
28 | 27 |
import utils, db, helpers, folder, image, log |
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
|
28 |
|
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
|
29 |
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
|
30 |
""" |
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 |
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
|
32 |
""" |
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 |
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
|
34 |
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
|
35 |
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
|
36 |
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
|
37 |
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
|
38 |
|
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 |
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
|
40 |
|
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
|
41 |
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
|
42 |
# 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
|
43 |
# 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
|
44 |
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
|
45 |
|
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 |
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
|
47 |
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
|
48 |
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
|
49 |
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
|
50 |
}[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
|
51 |
|
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 |
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
|
53 |
|
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
|
54 |
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
|
55 |
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
|
56 |
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
|
57 |
|
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 |
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
|
59 |
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
|
60 |
|
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 |
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
|
62 |
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
|
63 |
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
|
64 |
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
|
65 |
|
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 |
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
|
67 |
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
|
68 |
|
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 |
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
|
70 |
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
|
71 |
|
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 |
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
|
73 |
|
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 |
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
|
75 |
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
|
76 |
|
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 |
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
|
78 |
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
|
79 |
|
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 |
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
|
81 |
|
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 |
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
|
83 |
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
|
84 |
|
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
|
85 |
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
|
86 |
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
|
87 |
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
|
88 |
|
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 |
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
|
90 |
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
|
91 |
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
|
92 |
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
|
93 |
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
|
94 |
|
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 |
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
|
96 |
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
|
97 |
|
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 |
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
|
99 |
|
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
|
100 |
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
|
101 |
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
|
102 |
|
26
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
103 |
if fname : |
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
104 |
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
|
105 |
else : |
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
106 |
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
|
107 |
|
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
108 |
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
|
109 |
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
|
110 |
|
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
111 |
if res : |
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
112 |
return res |
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
113 |
|
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
114 |
else : |
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
115 |
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
|
116 |
|
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 |
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
|
118 |
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
|
119 |
|
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 |
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
|
121 |
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
|
122 |
|
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 |
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
|
124 |
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
|
125 |
|
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 |
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
|
127 |
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
|
128 |
|
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 |
# 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
|
130 |
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
|
131 |
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
|
132 |
|
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 |
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
|
134 |
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
|
135 |
|
26
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
136 |
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
|
137 |
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
|
138 |
|
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
139 |
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
|
140 |
|
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
141 |
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
|
142 |
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
|
143 |
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
|
144 |
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
|
145 |
else : |
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
146 |
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
|
147 |
|
29 | 148 |
log.info("%6s -> %s/%s", key, dir, fname) |
26
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
149 |
|
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
150 |
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
|
151 |
""" |
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
152 |
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
|
153 |
|
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
154 |
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
|
155 |
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
|
156 |
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
|
157 |
""" |
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 |
dirqueue = [root] |
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
160 |
|
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
161 |
# 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
|
162 |
paths = {} |
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
163 |
|
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
164 |
while dirqueue : |
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
165 |
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
|
166 |
|
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
167 |
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
|
168 |
|
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
169 |
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
|
170 |
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
|
171 |
|
28 | 172 |
log.debug("dir %50s", pathtuple[0]) |
26
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
173 |
|
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
174 |
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
|
175 |
|
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
176 |
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
|
177 |
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
|
178 |
|
28 | 179 |
log.debug("img %50s %15s", *pathtuple) |
26
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
180 |
|
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
181 |
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
|
182 |
|
28 | 183 |
log.info("we have %d nodes", len(paths)) |
26
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
184 |
|
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
185 |
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
|
186 |
try : |
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
187 |
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
|
188 |
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
|
189 |
|
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
190 |
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
|
191 |
|
28 | 192 |
log.debug("%s %50s %15s -> %d %s", dir and "img" or "dir", dir, fname, id, key) |
26
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
193 |
|
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
194 |
except KeyError : |
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
195 |
pass |
28 | 196 |
# log.warning("non-existant node (%d, %s, %s) in db", id, dir, fname) |
26
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
197 |
|
28 | 198 |
if paths : |
199 |
log.info("allocating shorturls for %d new nodes:", len(paths)) |
|
26
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
200 |
|
28 | 201 |
db.insert_many( |
202 |
_got_obj_key, |
|
203 |
"INSERT INTO nodes (dirpath, filename) VALUES (?, ?)", |
|
204 |
((obj, (path, fname)) for ((path, fname), obj) in paths.iteritems()) |
|
205 |
) |
|
206 |
else : |
|
207 |
log.info("no new images") |
|
26
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
208 |