-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathalbumlist.py
executable file
·88 lines (69 loc) · 2.48 KB
/
albumlist.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python
from modules import db
from modules import flags
from modules import items
from modules import utils
import getopt
import sys
import getpass
FLAGS = flags.FLAGS
FLAGS.AddFlag('u', 'username', 'The username to use for the database')
FLAGS.AddFlag('p', 'password', 'The password to use for the database (interactive prompt if unspecified)', '')
FLAGS.AddFlag('d', 'database', 'The database to use', 'gallery2')
FLAGS.AddFlag('h', 'hostname', 'The hostname to use', 'localhost')
FLAGS.AddFlag('t', 'table_prefix', 'The table prefix to use', 'g2_')
FLAGS.AddFlag('f', 'field_prefix', 'The field prefix to use', 'g_')
if sys.stdout.isatty():
default_encoding = sys.stdout.encoding
else:
import locale
default_encoding = locale.getpreferredencoding()
def printAlbum(album, subalbums, photos, level=0):
print level*'\t' + '%s' % unicode(album).encode(default_encoding, 'replace')
try:
for sub in subalbums[album.id()]:
printAlbum(sub, subalbums, photos, level+1)
except KeyError:
pass
try:
for photo in photos[album.id()]:
print level*'\t' + '\t%s' % unicode(photo).encode(default_encoding, 'replace')
except KeyError:
pass
def main(argv):
appname = argv[0]
try:
argv = FLAGS.Parse(argv[1:])
except flags.FlagParseError, e:
utils.Usage(appname, e.usage(), e.message())
sys.exit(1)
if FLAGS.password == '':
FLAGS.password = getpass.getpass('DB Password:')
gdb = db.Database(FLAGS.username, FLAGS.password, FLAGS.database,
FLAGS.hostname, FLAGS.table_prefix, FLAGS.field_prefix)
try:
rootalbum = None
albums = []
album_ids = gdb.ItemIdsForTable(items.AlbumItem.TABLE_NAME)
subalbums_by_album = {}
for id in album_ids:
album = items.AlbumItem(gdb,id)
albums.append(items.AlbumItem(gdb, id))
if album.parent_id():
if album.parent_id() not in subalbums_by_album:
subalbums_by_album[album.parent_id()] = []
subalbums_by_album[album.parent_id()].append(album)
else:
rootalbum = album
photos_by_album = {}
photo_ids = gdb.ItemIdsForTable(items.PhotoItem.TABLE_NAME)
for id in photo_ids:
photo = items.PhotoItem(gdb, id)
if photo.parent_id() not in photos_by_album:
photos_by_album[photo.parent_id()] = []
photos_by_album[photo.parent_id()].append(photo)
printAlbum(rootalbum, subalbums_by_album, photos_by_album)
finally:
gdb.close()
if __name__ == '__main__':
main(sys.argv)