author | Tero Marttila <terom@fixme.fi> |
Sun, 14 Jun 2009 18:24:14 +0300 | |
changeset 116 | 2d3721b9ffd0 |
parent 44 | 533b7e8b5d3b |
permissions | -rw-r--r-- |
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
|
1 |
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
|
2 |
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
|
3 |
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
|
4 |
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
|
5 |
|
44
533b7e8b5d3b
strip copyright/license boilerplate from modules, except dexif and formatbytes
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
6 |
import utils, db, helpers, folder, image, log |
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
|
7 |
|
44
533b7e8b5d3b
strip copyright/license boilerplate from modules, except dexif and formatbytes
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
8 |
""" |
533b7e8b5d3b
strip copyright/license boilerplate from modules, except dexif and formatbytes
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
9 |
Methods for generating/using ShortURLs |
533b7e8b5d3b
strip copyright/license boilerplate from modules, except dexif and formatbytes
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
10 |
""" |
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
|
11 |
|
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
|
12 |
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
|
13 |
""" |
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
|
14 |
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
|
15 |
""" |
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
|
16 |
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
|
17 |
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
|
18 |
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
|
19 |
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
|
20 |
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
|
21 |
|
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 |
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
|
23 |
|
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
|
24 |
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
|
25 |
# 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
|
26 |
# 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
|
27 |
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
|
28 |
|
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 |
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
|
30 |
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
|
31 |
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
|
32 |
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
|
33 |
}[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
|
34 |
|
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
|
35 |
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
|
36 |
|
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
|
37 |
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
|
38 |
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
|
39 |
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
|
40 |
|
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
|
41 |
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
|
42 |
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
|
43 |
|
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
|
44 |
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
|
45 |
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
|
46 |
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
|
47 |
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
|
48 |
|
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
|
49 |
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
|
50 |
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
|
51 |
|
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
|
52 |
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
|
53 |
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
|
54 |
|
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 |
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
|
56 |
|
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 |
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
|
58 |
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
|
59 |
|
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 |
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
|
61 |
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
|
62 |
|
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 |
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
|
64 |
|
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
19
diff
changeset
|
65 |
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
|
66 |
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
|
67 |
|
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
|
68 |
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
|
69 |
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
|
70 |
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
|
71 |
|
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
19
diff
changeset
|
72 |
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
|
73 |
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
|
74 |
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
|
75 |
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
|
76 |
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
|
77 |
|
6c774496bb00
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
19
diff
changeset
|
78 |
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
|
79 |
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
|
80 |
|
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 |
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
|
82 |
|
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
|
83 |
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
|
84 |
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
|
85 |
|
26
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
86 |
if fname : |
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
87 |
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
|
88 |
else : |
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
89 |
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
|
90 |
|
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
91 |
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
|
92 |
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
|
93 |
|
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
94 |
if res : |
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
95 |
return res |
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
96 |
|
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
97 |
else : |
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
98 |
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
|
99 |
|
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 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
|
101 |
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
|
102 |
|
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 |
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
|
104 |
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
|
105 |
|
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
|
106 |
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
|
107 |
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
|
108 |
|
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
|
109 |
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
|
110 |
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
|
111 |
|
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
|
112 |
# 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
|
113 |
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
|
114 |
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
|
115 |
|
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 |
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
|
117 |
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
|
118 |
|
26
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
119 |
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
|
120 |
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
|
121 |
|
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
122 |
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
|
123 |
|
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
124 |
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
|
125 |
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
|
126 |
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
|
127 |
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
|
128 |
else : |
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
129 |
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
|
130 |
|
29 | 131 |
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
|
132 |
|
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
133 |
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
|
134 |
""" |
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
135 |
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
|
136 |
|
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
137 |
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
|
138 |
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
|
139 |
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
|
140 |
""" |
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 |
dirqueue = [root] |
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
143 |
|
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
144 |
# 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
|
145 |
paths = {} |
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
146 |
|
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
147 |
while dirqueue : |
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
148 |
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
|
149 |
|
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
150 |
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
|
151 |
|
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
152 |
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
|
153 |
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
|
154 |
|
28 | 155 |
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
|
156 |
|
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
157 |
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
|
158 |
|
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
159 |
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
|
160 |
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
|
161 |
|
28 | 162 |
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
|
163 |
|
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
164 |
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
|
165 |
|
28 | 166 |
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
|
167 |
|
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
168 |
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
|
169 |
try : |
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
170 |
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
|
171 |
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
|
172 |
|
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
173 |
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
|
174 |
|
28 | 175 |
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
|
176 |
|
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
177 |
except KeyError : |
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
22
diff
changeset
|
178 |
pass |
28 | 179 |
# 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
|
180 |
|
28 | 181 |
if paths : |
182 |
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
|
183 |
|
28 | 184 |
db.insert_many( |
185 |
_got_obj_key, |
|
186 |
"INSERT INTO nodes (dirpath, filename) VALUES (?, ?)", |
|
187 |
((obj, (path, fname)) for ((path, fname), obj) in paths.iteritems()) |
|
188 |
) |
|
189 |
else : |
|
190 |
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
|
191 |