equal
deleted
inserted
replaced
23 import shelve |
23 import shelve |
24 import os.path |
24 import os.path |
25 |
25 |
26 from log import index |
26 from log import index |
27 |
27 |
|
28 import utils, db |
|
29 |
28 def int2key (id) : |
30 def int2key (id) : |
29 """ |
31 """ |
30 Turn an integer into a short-as-possible url-safe string |
32 Turn an integer into a short-as-possible url-safe string |
31 """ |
33 """ |
32 for type in ('B', 'H', 'I') : |
34 for type in ('B', 'H', 'I') : |
34 return base64.b64encode(struct.pack(type, id), '-_').rstrip('=') |
36 return base64.b64encode(struct.pack(type, id), '-_').rstrip('=') |
35 except struct.error : |
37 except struct.error : |
36 continue |
38 continue |
37 |
39 |
38 raise Exception("ID overflow: %s" % id) |
40 raise Exception("ID overflow: %s" % id) |
|
41 |
|
42 def key2int (key) : |
|
43 # base64 ignores extra padding, but if it doesn't, it's (4 - len%4), if len%4 != 0 |
|
44 bytes = base64.b64decode(key + '='*6, '-_') |
|
45 |
|
46 type = { |
|
47 1: 'B', |
|
48 2: 'H', |
|
49 4: 'I', |
|
50 }[len(bytes)] |
|
51 |
|
52 return struct.unpack(type, bytes)[0] |
39 |
53 |
40 def updateDB (root) : |
54 def updateDB (root) : |
41 """ |
55 """ |
42 DeGAL <= 0.2 used a simple key => path mapping, but now we use |
56 DeGAL <= 0.2 used a simple key => path mapping, but now we use |
43 something more structured, key => (type, dirpath, fname), where |
57 something more structured, key => (type, dirpath, fname), where |
141 if paths : |
155 if paths : |
142 raise ValueError("Paths not found: %s" % " ".join(paths)) |
156 raise ValueError("Paths not found: %s" % " ".join(paths)) |
143 |
157 |
144 return ret |
158 return ret |
145 |
159 |
|
160 def html_path (key, index=None) : |
|
161 dir, fname = image_info(key) |
|
162 |
|
163 return utils.url(dir, fname + '.html') |
|
164 |
|
165 def image_info (key) : |
|
166 res = db.select("""SELECT dirpath, filename FROM images WHERE id=?""", key2int(key)).fetchone() |
|
167 |
|
168 if res : |
|
169 return res |
|
170 |
|
171 else : |
|
172 raise KeyError(key) |
|
173 |
|
174 def get_images (keys) : |
|
175 res = [db.select("""SELECT dirpath, filename FROM images WHERE id=?""", key2int(key)).fetchone() for key in keys] |
|
176 |
|
177 # don't mind if we don't get as many as we asked for? |
|
178 if res : |
|
179 return res |
|
180 |
|
181 else : |
|
182 raise KeyError(keys) |
|
183 |